void fast_sort(T a[], int begin, int end) { //recursion exit //when not a valid array, or begin > end or only 1 element (begin==end), we don't do anything, just return if (!a || begin >= end) { return; } //select the first item as axis, partition the array T x = a[begin]; int i = begin, j= end; while (i<j) { while(i<j && a[j]>=x) j--; if(i<j) { a[i]=a[j]; i++; } while (i<j && a[i]<x) { i++; } if(i<j) { a[j]=a[i]; j--; } } a[i]=x; fast_sort(a, begin, i-1); fast_sort(a, i+1, end); }
void fast_sort(int arr[],int low,int high) { int keylocation; if(low<high) { keylocation=find_key(arr,low,high); fast_sort(arr,low,keylocation); fast_sort(arr,keylocation+1,high); } }
void fast_sort(int a[], int left, int right) { if(left < right) { int i = left, j = right, tmp = a[left]; while(i<j) { while(i<j&&a[j]>=tmp)j--; if(i<j)a[i++] = a[j]; while(i<j&&a[i]<tmp)i++; if(i<j)a[j--] = a[i]; } a[i] = tmp; fast_sort(a,left,i-1); fast_sort(a,i+1,right); } }
void fast_sort(int *a, int start, int end) { int i, j, key, temp; if (start >= end) { return; } key = a[start]; i = start; j = end; while (i<j) { while (j>i) { if (a[j] < key) break; j--; } while (i<j) { if (a[i] > key) break; i++; } if (i != j) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } a[start] = a[i]; a[i] = key; fast_sort(a, start, i-1); fast_sort(a, i+1, end); my_print2(a, start, end); }
void testSort() { int x[] = {7,3,4,8,8,5,1,2,4}; int a[9]; printArray(x,9); memcpy(a,x,sizeof(int)*9); cout<<"merge sort:"<<endl; merge_sort(a, 0, 8); printArray(a, 9); memcpy(a,x,sizeof(int)*9); cout<<"fast sort:"<<endl; fast_sort(a, 0, 8); printArray(a, 9); memcpy(a,x,sizeof(int)*9); cout<<"bubble sort:"<<endl; bubble_sort(a, 9); printArray(a, 9); memcpy(a,x,sizeof(int)*9); cout<<"select sort:"<<endl; select_sort(a, 9); printArray(a, 9); memcpy(a,x,sizeof(int)*9); cout<<"insert sort:"<<endl; insert_sort(a, 9); printArray(a, 9); memcpy(a,x,sizeof(int)*9); cout<<"count sort:"<<endl; count_sort(a, 9); printArray(a, 9); }
void Qsort(int arr[],int length) { fast_sort(arr,0,length-1); }
void mySort(int a[], int n) { assert(a && n>0); fast_sort(a, 0, n-1); }
void testFast_sort() { int a[] = {7,3,4,8,8,5,1,2,4}; fast_sort(a, 0, 8); printArray(a, 9); }