//C语言版排序 #include#include //冒泡排序void bubleSort(int data[], int n);//快速排序void quickSort(int data[], int low, int high);int findPos(int data[], int low, int high);//插入排序void bInsertSort(int data[], int n);//希尔排序void shellSort(int data[], int n);//选择排序void selectSort(int data[], int n);//堆排序void heapSort(int data[], int n);void swap(int data[], int i, int j);void heapAdjust(int data[], int i, int n);//归并排序void mergeSort(int data[], int first, int last);void merge(int data[], int low, int mid, int high);//基数排序void radixSort(int data[], int n);int getNumPos(int num, int pos);int main() { int data[10] = {43, 65, 4, 23, 6, 98, 2, 65, 7, 79}; int i; printf("原先数组:"); for(i=0;i<10;i++) { printf("%d ", data[i]); } printf("\n"); /*printf("冒泡排序:"); bubleSort(data, 10); for(i=0;i<10;i++) { printf("%d ", data[i]); } printf("\n"); printf("快速排序:"); quickSort(data, 0, 9); for(i=0;i<10;i++) { printf("%d ", data[i]); } printf("\n"); printf("插入排序:"); bInsertSort(data,10); for(i=0;i<10;i++) { printf("%d ", data[i]); } printf("\n"); printf("希尔排序:"); shellSort(data, 10); for(i=0;i<10;i++) { printf("%d ", data[i]); } printf("\n"); printf("选择排序:"); selectSort(data, 10); for(i=0;i<10;i++) { printf("%d ", data[i]); } printf("\n"); int data[11] = {-1, 43, 65, 4, 23, 6, 98, 2, 65, 7, 79}; int i; printf("原先数组:"); int data[11] = {-1, 43, 65, 4, 23, 6, 98, 2, 65, 7, 79}; for(i=1;i<11;i++) { printf("%d ", data[i]); } printf("\n"); printf(" 堆排序:"); heapSort(data, 10); for(i=1;i<11;i++) { printf("%d ", data[i]); } printf("\n"); printf("归并排序:"); mergeSort(data, 0, 9); for(i=0;i<10;i++) { printf("%d ", data[i]); } printf("\n");*/ printf("基数排序:"); radixSort(data, 10); for(i=0;i<10;i++) { printf("%d ", data[i]); } printf("\n"); return 0;}/*--------------------冒泡排序---------------------*/void bubleSort(int data[], int n) { int i,j,temp; //两个for循环,每次取出一个元素跟数组的其他元素比较 //将最大的元素排到最后。 for(j=0;j data[i+1]) { temp = data[i]; data[i] = data[i+1]; data[i+1] = temp; } } } }/*--------------------快速排序---------------------*/int findPos(int data[], int low, int high) { //将大于t的元素赶到t的左边,大于t的元素赶到t的右边 int t = data[low]; while(low < high) { while(low < high && data[high] >= t) { high--; } data[low] = data[high]; while(low < high && data[low] <=t) { low++; } data[high] = data[low]; } data[low] = t; //返回此时t在数组中的位置 return low;}//在数组中找一个元素,对大于该元素和小于该元素的两个数组进行再排序//再对两个数组分为4个数组,再排序,直到最后每组只剩下一个元素为止void quickSort(int data[], int low, int high) { if(low > high) { return; } int pos = findPos(data, low, high); quickSort(data, low, pos-1); quickSort(data, pos+1, high); }/*--------------------插入排序---------------------*/void bInsertSort(int data[], int n) { int low,high,mid; int temp,i,j; for(i=1;i temp) { high = mid-1; } else { low = mid+1; } } int j = i; //让data与已经排序好的数组的各个元素比较,小的放前面 while((j > low) && data[j-1] > temp) { data[j] = data[j-1]; --j; } data[low] = temp; }}/*--------------------希尔排序---------------------*/void shellSort(int * data, int n) { int step,i,j,key; //将数组按照step分组,不断二分到每组只剩下一个元素 for(step=n/2;step>0;step/=2) { //将每组中的元素排序,小的在前 for(i=step;i =0 && key 0;i--) { heapAdjust(data, i, n); } //循环每个结点,将大的结点交换到堆顶 for(i=n;i>1;i--) { swap(data, 1, i); //每次交换完都要调整二叉树,将剩下的最大的结点交换到堆顶 heapAdjust(data, 1, i-1); }}//交换函数void swap(int data[], int i, int j) { int temp; temp = data[i]; data[i] = data[j]; data[j] = temp;}void heapAdjust(int data[], int i, int n) { int j, temp; //假设第一个结点的元素是最大的 temp = data[i]; //i结点:2*i是i结点的左结点,2*i+1是i结点的右结点 //把结点元素大的交换到前面 for(j=2*i;j<=n;j*=2) { if(j < n && data[j] < data[j+1]) { j++; } if(temp >= data[j]) { break; } data[i] = data[j]; i = j; } data[i] = temp;}/*--------------------归并排序---------------------*/void mergeSort(int data[], int first, int last) { int mid = 0; //将数组不停的二分分组再组合,直到每组只剩一个元素 if(first < last) { mid = (first+last)/2; mergeSort(data, first, mid); mergeSort(data, mid+1, last); merge(data, first, mid, last); } return;}void merge(int data[], int low, int mid, int high) { int i, k; //定义一个临时数组存放传进来的无序数组排好序之后的数组 int *temp = (int *)malloc((high-low+1)*sizeof(int)); //将无序数组分成两个序列 int left_low = low; int left_high = mid; int right_low = mid+1; int right_high = high; //将两个序列比较排序,小的排前 for(k=0;left_low<=left_high && right_low<=right_high;k++) { if(data[left_low]<=data[right_low]) { temp[k] = data[left_low++]; } else{ temp[k] = data[right_low++]; } } //左序列如果有剩下元素未排序,加到临时数组的末尾 if(left_low <= left_high) { for(i=left_low;i<=left_high;i++) { temp[k++] = data[i]; } } //右序列如果有剩下元素未排序,加到临时数组的末尾 if(right_low <= right_high) { for(i=right_low;i<=right_high;i++) { temp[k++] = data[i]; } } //将排好序的小分组转移到原数组中 for(i=0;i
以上排序算法的优劣(时间复杂度和空间复杂度对比):
希望你们好好学C,期末取得好成绩 zhangsiman lizhiyuan wangying