花费了一天,复习了一下排序算法,写了下面的这个代码,也给大家分享一下,转载请转出处。
void selectsort(int[]);//选择排序
void insertsort(int[]);//插入排序
void bubblesort(int []);//冒泡排序
void shellsort(int []);//希尔排序
void shakersort(int []);//shaker排序
void creatminheap(int []);
void heapsort(int []);//堆排序
void quicksort(int s[], int l, int r);//快速排序
void mergearray(int a[], int first, int mid, int last, int temp[]);//归并排序
void stl_heap(int[],int i);//STL库 heap堆排序
void c_qsort(int[],int i); //C库 快速排序
void c_sort(int [],int i); //调用C库 sort函数排序
void RadixSort(int *array, int length);//基数排序
//http://blog.csdn.net/sunboyiris #include "stdafx.h" #include "iostream" #include "time.h" #include <algorithm> using namespace std; #define MAX 20000 #define SWAP(x,y) {int t;t=x;x=y;y=t;} void selectsort(int[]);//选择排序 void insertsort(int[]);//插入排序 void bubblesort(int []);//冒泡排序 void shellsort(int []);//希尔排序 void shakersort(int []);//shaker排序 void creatminheap(int []); void heapsort(int []);//堆排序 void quicksort(int s[], int l, int r);//快速排序 void mergearray(int a[], int first, int mid, int last, int temp[]);//归并排序 void stl_heap(int[],int i);//STL库 heap堆排序 void c_qsort(int[],int i); //C库 快速排序 void c_sort(int [],int i); //调用C库 sort函数排序 void RadixSort(int *array, int length);//基数排序
//------------------------基数排序----------------------------// //http://blog.csdn.net/sunboyiris int AbsoluteValue(int value) { return ((value >= 0) ? value : 0 - value); } int TenExponentiation(int exponent) { int i, value = 1; for (i = 0; i < exponent; i++) { value *= 10; } return value; } /****链式队列的定义,用于箱排序中的装箱和收集过程*/ typedef struct tagQueueNode{ int number; struct tagQueueNode *next; } QueueNode; typedef struct tagQueue{ QueueNode *front, *rear; } Queue; int IsQueueEmpty(Queue *q) { return (!q->front->next); } void EnQueue(Queue *q, int num) { QueueNode *temp = (QueueNode *)malloc(sizeof(QueueNode)); temp->number = num; temp->next = NULL; q->rear->next = temp; q->rear = temp; } int DeQueue(Queue *q) { int num; QueueNode *temp = q->front->next; if (IsQueueEmpty(q)) { exit(1); } num = temp->number; q->front->next = temp->next; if (q->rear == temp) { q->rear = q->front; } free(temp); return num; } #define BucketSort BinSort void BinSort(int *array, int length, int bit) { int i, j = 0; int key; int temp; Queue *queue[10]; for (i = 0; i < 10; i++) { queue[i] = (Queue *)malloc(sizeof(Queue)); queue[i]->front = queue[i]->rear = (QueueNode *)malloc(sizeof(QueueNode)); queue[i]->front->next = NULL; } if (bit >= 0) { for (i = 0; i < length; i++) { key = (int)(AbsoluteValue(array[i]) / TenExponentiation(bit)) - (int)(AbsoluteValue(array[i]) / TenExponentiation(bit + 1)) * 10; EnQueue(queue[key], array[i]); } for (i = 0; i < 10; i++) { while (!IsQueueEmpty(queue[i])) { array[j++] = DeQueue(queue[i]); } } } else { for (i = 0; i < length; i++) { key = (array[i] >= 0) ? 1 : 0; EnQueue(queue[key], array[i]); } while (!IsQueueEmpty(queue[0])) { array[j++] = DeQueue(queue[0]); } for (i = 0; i < (int)(j / 2); i++) { temp = array[i]; array[i] = array[j-i-1]; array[j-i-1] = temp; } while (!IsQueueEmpty(queue[1])) { array[j++] = DeQueue(queue[1]); } } for (i = 0; i < 10; i++) { free(queue[i]); } } void RadixSort(int *array, int length) { int i; int max = 0, key_num = 0; for (i = 0; i < length; i++) { if (AbsoluteValue(array[i]) > max) { max = array[i]; } } while ((int)(AbsoluteValue(max) / TenExponentiation(key_num)) >= 10) { key_num++; } for (i = 0; i <= key_num; i++) { BinSort(array, length, i); } BinSort(array, length, -1); } //------------------------C库 sort函数快速排序----------------------------// void c_sort(int num[],int i) { sort(num,num+i); } //------------------------C库 qsort函数快速排序----------------------------// int cmp(const void *a,const void *b) { return *(int*)a-*(int*)b; } void c_qsort(int num[],int i) { qsort(num,i,sizeof(int),cmp); } //------------------------STL库 堆排序----------------------------// void stl_heap(int num[],int i) { make_heap(num, num + i); sort_heap(num, num + i); } //------------------------快速排序----------------------------// void quicksort(int s[], int l, int r) { //s[l]---s[r]; if (l < r) { int i = l, j = r, x = s[l]; while (i < j) { while(i < j && s[j] >= x) // 从右向左找第一个小于x的数 j--; if(i < j) s[i++] = s[j]; while(i < j && s[i] < x) // 从左向右找第一个大于等于x的数 i++; if(i < j) s[j--] = s[i]; } s[i] = x; quicksort(s, l, i - 1); // 递归调用 quicksort(s, i + 1, r); } } //------------------------归并排序----------------------------// //将有二个有序数列a[first...mid]和a[mid...last]合并。 void mergearray(int a[], int first, int mid, int last, int temp[]) { int i = first, j = mid + 1; int m = mid, n = last; int k = 0; while (i <= m && j <= n) { if (a[i] < a[j]) temp[k++] = a[i++]; else temp[k++] = a[j++]; } while (i <= m) temp[k++] = a[i++]; while (j <= n) temp[k++] = a[j++]; for (i = 0; i < k; i++) a[first + i] = temp[i]; } void mergesort(int a[], int first, int last, int temp[]) { if (first < last) { int mid = (first + last) / 2; mergesort(a, first, mid, temp); //左边有序 mergesort(a, mid + 1, last, temp); //右边有序 mergearray(a, first, mid, last, temp); //再将二个有序数列合并 } } bool MergeSort(int a[], int n) { int *p = new int[n]; if (p == NULL) return false; mergesort(a, 0, n - 1, p); return true; } //------------------------堆排序----------------------------// // creatminheap(num); //heapsort(num); void creatminheap(int num[]) { int i,s,p; int heap[MAX+1]={-1}; for(i=1;i<=MAX;i++) { heap[i]=num[i]; s=i; p=i/2; while((s>=2)&&(heap[p]>heap[s])) { SWAP(heap[p],heap[s]); s=p; p=s/2; } } for(i=1;i<MAX;i++) num[i]=heap[i]; } void heapsort(int num[]) { int i,j,p,s; j=MAX; while(j>1) { SWAP(num[1],num[j]); j--; p=1; s=2*p; while(s<=j) { if((s<j)&&(num[s+1]<num[s])) s++; if(num[p]<=num[s]) break; SWAP(num[p],num[s]); p=s; s=2*p; } } } //------------------------shaker排序----------------------------// void shakersort(int num[]) { int i, left=0,right=MAX-1,k=0; while(left<right) { //向右起泡排序 for(i=left;i<right;i++) { if(num[i]>num[i+1]) {SWAP(num[i],num[i+1]);k=i;} } right=k; //向左气泡排序 for(i=right;i>left;i--) { if(num[i]<num[i-1]) {SWAP(num[i],num[i-1]);k=i;} } left=k; } } //------------------------shell排序----------------------------// void shellsort(int num[]) { int i,j,k,t,gap; gap=MAX/2; //间隔区间 排序 while(gap>0) { for(k=0;k<gap;k++) { for(i=k+gap;i<MAX;i+=gap) { for(j=i-gap;j>=k;j-=gap) { if(num[j]>num[j+gap]) {SWAP(num[j],num[j+gap]);} else {break;} } } } gap/=2; } } //------------------------冒泡排序----------------------------// void bubblesort(int num[]) { int i,j,k,f=1; for(i=0;(i<MAX-1)&&(f==1);i++) { f=0; for(j=0;j<MAX-i-1;j++) { if(num[j+1]<num[j]) { SWAP(num[j+1],num[j]); f=1; } } } } //------------------------选择排序----------------------------// void selectsort(int num[]) { int i,j,m; for(i=0;i<MAX-1;i++)\ { m=i; for(j=i+1;j<MAX;j++) { if(num[j]<num[m]) m=j; if(i!=m) SWAP(num[i],num[m]); } } } //------------------------插入排序----------------------------// void insertsort(int num[]) { int i,j,k,t; for(j=1;j<MAX;j++) { t=num[j]; i=j-1; while(t<num[i]) { num[i+1]=num[i]; i--; if(i<0) break; } num[i+1]=t; } }
int _tmain(int argc, _TCHAR* argv[]) { int num[MAX]={0}; int num1[MAX]={0}; int num2[MAX]={0}; int i; srand(time(NULL)); clock_t ibegin, iend; for(i=0;i<MAX;i++) { num1[i]=rand()%100; } for(i=0;i<MAX;i++) { num[i]=num1[i]; } printf("--当前数据量为%d--By vicent(http://blog.csdn.net/sunboyiris)--\n", MAX); printf("选择排序: "); ibegin = clock(); selectsort(num); iend = clock(); printf("%d毫秒\n", iend - ibegin); for(i=0;i<MAX;i++) { num[i]=num1[i]; } printf("--当前数据量为%d--By vicent(http://blog.csdn.net/sunboyiris)--\n", MAX); printf("插入排序: "); ibegin = clock(); insertsort(num); iend = clock(); printf("%d毫秒\n", iend - ibegin); for(i=0;i<MAX;i++) { num[i]=num1[i]; } printf("--当前数据量为%d--By vicent(http://blog.csdn.net/sunboyiris)--\n", MAX); printf("冒泡排序: "); ibegin = clock(); bubblesort(num); iend = clock(); printf("%d毫秒\n", iend - ibegin); for(i=0;i<MAX;i++) { num[i]=num1[i]; } printf("--当前数据量为%d--By vicent(http://blog.csdn.net/sunboyiris)--\n", MAX); printf("希尔排序: "); ibegin = clock(); shellsort(num); iend = clock(); printf("%d毫秒\n", iend - ibegin); for(i=0;i<MAX;i++) { num[i]=num1[i]; } printf("--当前数据量为%d--By vicent(http://blog.csdn.net/sunboyiris)--\n", MAX); printf("shaker排序: "); ibegin = clock(); shakersort(num); iend = clock(); printf("%d毫秒\n", iend - ibegin); for(i=0;i<MAX;i++) { num2[i]=num1[i]; } printf("--当前数据量为%d--By vicent(http://blog.csdn.net/sunboyiris)--\n", MAX); printf("堆排序: "); ibegin = clock(); creatminheap(num2); heapsort(num2); iend = clock(); printf("%d毫秒\n", iend - ibegin); for(i=0;i<MAX;i++) { num2[i]=num1[i]; } printf("--当前数据量为%d--By vicent(http://blog.csdn.net/sunboyiris)--\n", MAX); printf("快速排序: "); ibegin = clock(); quicksort(num2,0,MAX-1); iend = clock(); printf("%d毫秒\n", iend - ibegin); for(i=0;i<MAX;i++) { num[i]=num1[i]; } printf("--当前数据量为%d--By vicent(http://blog.csdn.net/sunboyiris)--\n", MAX); printf("希尔排序: "); ibegin = clock(); MergeSort(num,MAX); iend = clock(); printf("%d毫秒\n", iend - ibegin); for(i=0;i<MAX;i++) { num[i]=num1[i]; } printf("--当前数据量为%d--By vicent(http://blog.csdn.net/sunboyiris)--\n", MAX); printf("STL 堆排序: "); ibegin = clock(); stl_heap(num,MAX); iend = clock(); printf("%d毫秒\n", iend - ibegin); for(i=0;i<MAX;i++) { num[i]=num1[i]; } /*void mergearray(int a[], int first, int mid, int last, int temp[]);//归并排序 void stl_heap(int[],int i);//STL库 heap堆排序 void c_qsort(int[],int i); //C库 快速排序 void c_sort(int [],int i); //调用C库 sort函数排序 void RadixSort(int *array, int length);//基数排序 */ printf("--当前数据量为%d--By vicent(http://blog.csdn.net/sunboyiris)--\n", MAX); printf("C qsort排序: "); ibegin = clock(); c_qsort(num,MAX); iend = clock(); printf("%d毫秒\n", iend - ibegin); for(i=0;i<MAX;i++) { num[i]=num1[i]; } printf("--当前数据量为%d--By vicent(http://blog.csdn.net/sunboyiris)--\n", MAX); printf("基数 排序: "); ibegin = clock(); RadixSort(num,MAX); iend = clock(); printf("%d毫秒\n", iend - ibegin); // quicksort(num,0,MAX-1); // MergeSort(num,MAX); //stl_heap(num,MAX); //c_sort(num,MAX); //c_qsort(num,MAX); //RadixSort(num,MAX); //for(i=MAX-1;i>=0;i--) //{ cout<<num[i]<<" ";} return 0; }