import java.util.*;
public class AllSort {
// I Insert Sort
//1.1 InsertSort
public static void InsertSort(Comparable[] a){
if(a.length == 0 ) return;
for(int i=1;i<a.length;++i){
for(int j=i;j>0 && less(a[j],a[j-1]);--j){
exchange(a,j-1,j);
}
}
}
//1.2 BinaryInsertSort
public static void BinaryInsertSort(Comparable[] a){
if(a.length == 0 ) return;
for(int i=1;i<a.length;++i){
int low = 0;
int high = i-1;
int mid = -1;
while(low<=high){
mid = (low+high)/2;
if(less(a[mid],a[i])){
low = mid+1;
}else high = mid-1;
}
Comparable temp = a[i];
for(int j=i-1;j>=low;--j){
a[j+1] = a[j];
}
a[low] = temp;
}
}
//1.3 ShellSort
public static void ShellSort(Comparable[] a){
int N = a.length;
if(N<=1) return;
int h=1;
while(h<(N/3)) h=3*h + 1;
while(h>=1){
for(int i=h;i<N;++i){
for(int j=i;j>=h && less(a[j],a[j-h]);j=j-h){
exchange(a,j-h,j);
}
}
h = h/3;
}
}
// II SelectSort
//2.1 SelectSort
public static void SelectSort(Comparable[] a){
int N = a.length;
if(N<=1) return;
int min = -1;
for(int i=0;i<N;++i){
min = i;
for(int j=i+1;j<N;++j) if(less(a[j],a[min])) min=j;
exchange(a,i,min);
}
}
//2.2 HeapSort
private static void Sift(Comparable[] a,int low,int high){
int i = low,j = 2*i;
while(j<=high){
if(j<high && less(a[j],a[j+1])) ++j;
if(less(a[i],a[j])) exchange(a,i,j);
else break;
i = j;
j = 2*i;
}
}
public static void HeapSort(Comparable[] a){
if(a.length<=1) return;
Comparable[] new_a = new Comparable[a.length+1];
for(int i=1;i<new_a.length;++i){
new_a[i] = a[i-1];
}
for(int i=a.length/2;i>=1;--i){
Sift(new_a,i,a.length);
}
for(int i=a.length;i>=2;--i){
exchange(new_a,1,i);
Sift(new_a,1,i-1);
}
for(int i=0;i<a.length;++i){
a[i] = new_a[i+1];
}
}
//III ExchangeSort
//3.1 BubbleSort
public static void BubbleSort(Comparable[] a){
int N = a.length;
if(N<=1) return;
for(int i=N-1;i>0;--i){
for(int j=0;j<i;++j)
if(less(a[j+1],a[j])) exchange(a,j,j+1);
}
}
//3.2 QuickSort
public static void QuickSort(Comparable[] a){
List<Comparable> list = Arrays.asList(a);
Collections.shuffle(list);
list.toArray(a);
QuickSort(a,0,a.length-1);
}
private static void QuickSort(Comparable[] a,int low,int high){
if(high<=low) return;
int p = Partition(a,low,high);
QuickSort(a,low,p-1);
QuickSort(a,p+1,high);
}
private static int Partition(Comparable[] a,int low,int high){
int left=low;
int right = high+1;
Comparable temp = a[low];
while(true){
while(less(a[++left],temp)) if(left==high) break;
while(less(temp,a[--right]));
if(left>=right) break;
exchange(a,left,right);
}
exchange(a,low,right);
return right;
}
public static void show(Comparable[] a){
System.out.println("list:");
for(int i=0;i<a.length;++i){
System.out.print(a[i] + " ");
}
System.out.println();
}
private static boolean less(Comparable v, Comparable w)
{
return v.compareTo(w) < 0;
}
private static void exchange(Comparable[] a,int i,int j)
{
Comparable temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static void main(String[] args) {
Random rd = new Random(10);
Integer[] ints = new Integer[10];
for(int i=0;i<ints.length;++i){
ints[i] = rd.nextInt(20);
}
show(ints);
String select = "G";
StdOut.println("SortAll......");
if (select.equals("A")) InsertSort(ints);
if (select.equals("B")) BinaryInsertSort(ints);
if (select.equals("C")) ShellSort(ints);
if (select.equals("D")) SelectSort(ints);
if (select.equals("E")) HeapSort(ints);
if (select.equals("F")) BubbleSort(ints);
if (select.equals("G")) QuickSort(ints);
show(ints);
}
}