Sort Algorithms
Insertion Sort
Straight Insertion Sort best: O (n) average: O (n^2) worse: O (n^2)
It is efficient for the almost sorted array
Binary Insertion Sort best: O (n log n) average: O (n^2) worse: O (n^2)
Exchange Sort
Bubble Sort always: O (n^2)
Quick Sort best: O (n log n) average: O (n log n) worse: O (n^2)
Recursive implementation
We can sort the array to be almost sorted using quick sort and then sort it using insertion sort
Selection Sort
Straight Selection Sort always: O (n^2)
Heap Sort always: O (n log n) build heap phase: O (n) sort phase: O (n log n)
Merge Sort
Two-way Merge Sort always: O (n log n)
Distribution Sort
Distribution Sorting Algorithms rely on the priori knowledge about the universal set from which the data to be sorted are drawn. It does not make use the comparisons to do the sorting.
Bucket Sort O (n)
Radix Sort O (n)
Radix Sort can be used when each element of the universal set can be viewed as a sequence of digits or the cross product of a finite number of finite set.
Example: Sort the playing cards
A Lower Bound on Comparison-Based Sorting
O (n log n) a binary decision tree with n! leaves and n log n height
Code:
Straight Insertion Sort
package
com.allenwu.sorter;
import
com.allenwu.Comparable;
public
class
StraightInsertionSorter
extends
Sorter
...
{ public void sort(Comparable[] array) ... { this .array = array; for ( int i = 0 ;i < array.length;i ++ ) ... { for ( int j = i;j > 0 && array[j - 1 ].isGT(array[j]);j -- ) ... { swap(j, j - 1 ); } } return ; } }
Binary Insertion Sort
package com.allenwu.sorter; import com.allenwu.Comparable;
public
class
BinaryInsertionSorter extends Sorter
...
{ public void sort(Comparable[] array) ... { this .array = array; for ( int i = 1 ;i < array.length;i ++ ) ... { int left = 0 ; int right = i; Comparable item = array[i]; while (left < right) ... { int middle = (left + right) / 2 ; if (array[middle].isGT(item)) ... { right = middle; } else ... { left = middle + 1 ; } } for ( int j = i;j > left;j -- ) ... { swap(j,j - 1 ); } for ( int j = 0 ;j < array.length;j ++ ) ... { if (i == j) System. out .print( " * " ); System. out .print(array[j].getValue() + " " ); } System.out .println( "" ); } } }
Bubble Sort
package com.allenwu.sorter; import com.allenwu.Comparable;
public
class
BubbleSorter extends Sorter
...
{ public void sort(Comparable[] array) ... { // TODO Auto-generated method stub this .array = array; int length = array.length; for ( int i = 0 ;i < length;i ++ ) ... { for ( int j = 0 ;j < length - 1 - i;j ++ ) ... { if (array[j].isGT(array[j + 1 ])) ... { swap(j,j + 1 ); } } } } }
Quick Sort
package com.allenwu.sorter; import com.allenwu.Comparable;
public
class
QuickSorter extends Sorter
...
{ public void sort(Comparable[] array) ... { // TODO Auto-generated method stub this .array = array; sort( 0 ,array.length - 1 ); } private void sort( int left, int right) ... { if (left == right) ... { return ; } int pivot = selectPivot(left, right); swap(right, pivot); int i = left; int j = right - 1 ; Comparable item = array[right]; while ( true ) ... { while (i < j && array[i].isLT(item))i ++ ; while (i < j && array[j].isGT(item))j -- ; if (i >= j) break ; swap(i ++ ,j -- ); } if (array[i].isGT(item)) ... { swap(i, right); } if (i > left) ... { sort(left,i - 1 ); } if (i < right) ... { sort(i + 1 ,right); } } private int selectPivot( int left, int right) ... { return right; } }
Straight Selection Sort
package
com.allenwu.sorter;
import
com.allenwu.Comparable;
public
class
StraightSelectionSorter
extends
Sorter
...
{ public void sort(Comparable[] array) ... { // TODO Auto-generated method stub this .array = array; int length = array.length; for ( int i = 0 ;i < length;i ++ ) ... { int min = i; for ( int j = i + 1 ;j < length;j ++ ) ... { if (array[j].isLT(array[min])) ... { min = j; } } swap(i,min); } } }
Heap Sort
package com.allenwu.sorter; import com.allenwu.Comparable; import com.allenwu.container.Heap;
public
class
HeapSorter extends Sorter
...
{ private Heap heap; public void sort(Comparable[] array) ... { // TODO Auto-generated method stub Heap heap = Heap.buildHeap(array); for ( int i = 0 ;i < array.length;i ++ ) ... { array[i] = heap. get (); } } }
//
==================Heap===================
package com.allenwu.container; import com.allenwu.Comparable;
public
class
Heap
...
{ private Comparable[] items = null ; private int maxSize = 16 ; private int size = 0 ; private Heap() ... { } public Comparable get () ... { Comparable min = items[ 0 ]; items[ 0 ] = items[size - 1 ]; size -- ; modify( 0 ); return min; } public static Heap buildHeap(Comparable[] items) ... { Heap heap = new Heap(); heap.items = (Comparable[])items.clone(); heap.maxSize = items.length; heap.size = items.length; for ( int i = heap.size / 2 ;i >= 0 ;i -- ) ... { heap.modify(i); } return heap; } public static Heap buildHeap() ... { Heap heap = new Heap(); heap.items = new Comparable[heap.maxSize]; return heap; } private void swap( int i, int j) ... { Comparable temp = items[i]; items[i] = items[j]; items[j] = temp; } private void modify( int i) ... { int max = i; if ( 2 * i < size && items[ 2 * i].isLT(items[max])) ... { max = 2 * i; } if ( 2 * i + 1 < size && items[ 2 * i + 1 ].isLT(items[max])) ... { max = 2 * i + 1 ; } if (max != i) ... { swap(i,max); modify(max); } } public void insert(Comparable item) ... { if (items.length >= maxSize) ... { maxSize *= 2 ; Comparable[] tmpItems = new Comparable[maxSize]; for ( int i = 0 ;i < items.length;i ++ ) ... { tmpItems[i] = items[i]; } items= tmpItems; } } }
Merge Sort
package
com.allenwu.sorter;
import
com.allenwu.Comparable;
public
class
MergeSorter
extends
Sorter
...
{ public void sort(Comparable[] array) ... { // TODO Auto-generated method stub this .array = array; sort( 0 , array.length - 1 ); } private void sort( int left, int right) ... { if (left < right) ... { int middle = (left + right) / 2 ; sort(left, middle); sort(middle + 1 , right); merge(left, middle, right); } } private void merge( int left, int middle, int right) ... { int i = left; int j = 0 ; int k = middle + 1 ; Comparable[] tmp = new Comparable[right - left + 1 ]; while (i <= middle && k <= right) ... { if (array[i].isLT(array[k])) ... { tmp[j ++ ] = array[i ++ ]; } else ... { tmp[j ++ ] = array[k ++ ]; } } while (i <= middle) ... { tmp[j ++ ] = array[i ++ ]; } for (i = 0 ;i < j;i ++ ) ... { array[left + i] = tmp[i]; } } }