
Sorting Algorithm

This is a sorting algorithm.   It may be applied to a set of data in order to sort it.    
For comparing various sorts, see compare sorts.  
For other sorting algorithms,   see sorting algorithms,   or:

O(n logn) sorts
Heap sort |
Merge sort |
Patience sort |
Quick sort
O(n log2n) sorts
Shell Sort
O(n2) sorts
Bubble sort |
Cocktail sort |
Cocktail sort with shifting bounds |
Comb sort |
Cycle sort |
Gnome sort |
Insertion sort |
Selection sort |
Strand sort
other sorts
Bead sort |
Bogo sort |
Common sorted list |
Composite structures sort |
Custom comparator sort |
Counting sort |
Disjoint sublist sort |
External sort |
Jort sort |
Lexicographical sort |
Natural sorting |
Order by pair comparisons |
Order disjoint list items |
Order two numerical lists |
Object identifier (OID) sort |
Pancake sort |
Quickselect |
Permutation sort |
Radix sort |
Ranking methods |
Remove duplicate elements |
Sleep sort |
Stooge sort |
[Sort letters of a string] |
Three variable sort |
Topological sort |
Tree sort


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:

Before:
6 7 8 9 2 5 3 4 1
After:
1 4 3 5 2 9 8 7 6

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:

 function circlesort (index lo, index hi, swaps)
 {
   if lo == hi return (swaps)
   high := hi
   low := lo
   mid := int((hi-lo)/2)
   while lo < hi {
     if  (value at lo) > (value at hi) {
        swap.values (lo,hi)
        swaps++
     }
     lo++
     hi--
   }
   if lo == hi
     if (value at lo) > (value at hi+1) {
         swap.values (lo,hi+1)
         swaps++
     }
   swaps := circlesort(low,low+mid,swaps)
   swaps := circlesort(low+mid+1,high,swaps)
   return(swaps)
 }
 while circlesort (0, sizeof(array)-1, 0)



See also

 For more information on Circle sorting, see Sourceforge.


class CircleSort {
  function : Main(args : String[]) ~ Nil {
    circleSort([2, 14, 4, 6, 8, 1, 3, 5, 7, 11, 0, 13, 12, -1]);
  }
 
  function : circleSort(arr : Int[]) ~ Nil {
    if(arr->Size() > 0) {
      do {
        arr->ToString()->PrintLine();
      } 
      while(CircleSort(arr, 0, arr->Size() - 1, 0) <> 0);
    };
  }
 
  function : CircleSort( arr : Int[], lo : Int, hi : Int, num_swaps : Int) ~ Int {
    if(lo = hi) {
      return num_swaps;
    };
 
 
    high := hi;
    low := lo;
    mid := (hi - lo) / 2;
 
    while (lo < hi) {
      if(arr[lo] > arr[hi]) {
        Swap(arr, lo, hi);
        num_swaps++;
      };
      lo++;
      hi--;
    };
 
    if(lo = hi & arr[lo] > arr[hi + 1]) {
      Swap(arr, lo, hi + 1);
      num_swaps++;
    };
 
    num_swaps := CircleSort(arr, low, low + mid, num_swaps);
    num_swaps := CircleSort(arr, low + mid + 1, high, num_swaps);
 
    return num_swaps;
  }
 
  function : Swap(arr : Int[], idx1 : Int, idx2 : Int) ~ Nil {
    tmp := arr[idx1];
    arr[idx1] := arr[idx2];
    arr[idx2] := tmp;
  }
}
 