How to resolve the algorithm Sorting Algorithms/Circle Sort step by step in the C# programming language
How to resolve the algorithm Sorting Algorithms/Circle Sort step by step in the C# programming language
Table of Contents
Problem Statement
Sort an array of integers (of any convenient size) into ascending order using Circlesort. In short, compare the first element to the last element, then the second element to the second last element, etc. Then split the array in two and recurse until there is only one single element in the array, like this: Repeat this procedure until quiescence (i.e. until there are no swaps). Show both the initial, unsorted list and the final sorted list. (Intermediate steps during sorting are optional.) Optimizations (like doing 0.5 log2(n) iterations and then continue with an Insertion sort) are optional. Pseudo code:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sorting Algorithms/Circle Sort step by step in the C# programming language
Overview:
The provided C# program implements a "Circle Sort" algorithm to sort an input array of integers in ascending order. The algorithm is a variation of the Merge Sort algorithm, optimized for circular arrays, where the last element wraps around to the beginning.
CircleSort Method:
- Accepts an array of integers as input.
- Repeatedly calls the
CircleSortR
method to sort the array until no swaps are needed. - Returns the sorted array.
CircleSortR Method (Recursive):
- Accepts the input array, start index, end index, and number of swaps.
- Recursively divides the array into two halves and sorts them separately.
- Merges the sorted halves and counts the number of swaps during merging.
- Returns the updated number of swaps.
Implementation Details:
- The
CircleSortR
method uses a divide-and-conquer approach to sort the array in a circular fashion. - It finds the middle index
mid
for each recursive call. - The first
while
loop sorts the left half of the array (fromlow
tolow + mid
) and the right half (fromlow + mid + 1
tohigh
). - If the element at the merge point (
arr[lo]
) is greater than the element to its right (arr[hi + 1]
), it swaps them. - The
numSwaps
variable keeps track of the number of swaps performed during each recursive call. - After sorting the left and right halves, the method recursively calls itself on those halves to complete the merging process.
Example Usage:
In the Main
method, three test cases are used to demonstrate the sorting algorithm:
{ 6, 7, 8, 9, 2, 5, 3, 4, 1 }
{ 2, 14, 4, 6, 8, 1, 3, 5, 7, 11, 0, 13, 12, -1 }
{ 2, 3, 3, 5, 5, 1, 1, 7, 7, 6, 6, 4, 4, 0, 0 }
The sorted arrays are printed to the console.
Time Complexity:
- The algorithm resembles the Merge Sort, which has a time complexity of O(n log n) in the average and worst cases.
Advantages:
- Optimized for circular arrays, where the end wraps around to the beginning.
- Relatively simple and efficient implementation.
Disadvantages:
- Not as efficient as other sorting algorithms (e.g., Quick Sort, Heap Sort) for very large arrays.
Source code in the csharp programming language
using System;
using System.Linq;
namespace CircleSort
{
internal class Program
{
public static int[] CircleSort(int[] array)
{
if (array.Length > 0)
while (CircleSortR(array, 0, array.Length - 1, 0) != 0)
continue;
return array;
}
private static int CircleSortR(int[] arr, int lo, int hi, int numSwaps)
{
if (lo == hi)
return numSwaps;
var high = hi;
var low = lo;
var mid = (hi - lo) / 2;
while (lo < hi)
{
if (arr[lo] > arr[hi])
{
(arr[lo], arr[hi]) = (arr[hi], arr[lo]);
numSwaps++;
}
lo++;
hi--;
}
if (lo == hi && arr[lo] > arr[hi + 1])
{
(arr[lo], arr[hi + 1]) = (arr[hi + 1], arr[lo]);
numSwaps++;
}
numSwaps = CircleSortR(arr, low, low + mid, numSwaps);
numSwaps = CircleSortR(arr, low + mid + 1, high, numSwaps);
return numSwaps;
}
private static void Main(string[] args)
{
var sortedArray = CircleSort(new int[] { 6, 7, 8, 9, 2, 5, 3, 4, 1 });
sortedArray.ToList().ForEach(i => Console.Write(i.ToString() + " "));
Console.WriteLine();
var sortedArray2 = CircleSort(new int[] { 2, 14, 4, 6, 8, 1, 3, 5, 7, 11, 0, 13, 12, -1 });
sortedArray2.ToList().ForEach(i => Console.Write(i.ToString() + " "));
Console.WriteLine();
var sortedArray3 = CircleSort(new int[] { 2, 3, 3, 5, 5, 1, 1, 7, 7, 6, 6, 4, 4, 0, 0 });
sortedArray3.ToList().ForEach(i => Console.Write(i.ToString() + " "));
Console.ReadKey();
}
}
}
You may also check:How to resolve the algorithm Cartesian product of two or more lists step by step in the Scala programming language
You may also check:How to resolve the algorithm Sequence: smallest number greater than previous term with exactly n divisors step by step in the Wren programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the NLP++ programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the Ursala programming language
You may also check:How to resolve the algorithm Sierpinski triangle step by step in the Unlambda programming language