1. 快排
详见之前博文快速排序算法。
2. 堆排序
详见之前博文非递归方法的堆排序实现。
3. 简单排序(冒泡排序、选择排序和插入排序)
代码如下:
#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 20 static void show(int *arr, int len) { int index; for(index = 0; index < len; index++) { printf("%d ", arr[index]); } printf(" "); } static void swap(int *left, int *right) { int tmp = *left; *left = *right; *right = tmp; } /** * 选择排序思想:依次将未排序的数组中的最小数放入pos所在位置,pos从0至N-1 * -->前N-1个数放好位置,最后一个数就不用管了 */ static void select_sort(int *arr, int len) { int pos,index; int min_index; for(pos = 0; pos < len - 1; pos++) { min_index = pos; for(index = pos + 1; index < len; index++) { if(arr[index] < arr[min_index]) min_index = index; } if(min_index != pos) { swap(arr + pos, arr + min_index); } } } static void insert_sort(int *arr, int len) { int pos,index; int key; for(pos = 1; pos < len; pos++) { key = arr[pos]; for(index = pos - 1; index >= 0; index--) { if(key < arr[index]) { arr[index+1] = arr[index]; }else { break; } } arr[index+1] = key; } } static void bubble_sort(int *arr, int len) { int pos,index; for(pos = 0; pos < len - 1; pos++) { for(index = 0; index < len - 1 - pos; index++) { if(arr[index] > arr[index + 1]) { swap(arr + index, arr + index + 1); } } } } static void bubble_sort2(int *arr, int len) { int left, right, index; left = 0; right = len - 1; while(left < right) { for(index = left; index < right; index++) { if(arr[index] > arr[index+1]) { swap(arr+index, arr+index+1); } } right--; if(left == right) { break; } for(index = right; index > left; index--) { if(arr[index] < arr[index-1]) { swap(arr + index, arr + index -1); } } left++; } } int main(int argc, char *argv[]) { int arr[N], index; srand(time(NULL)); for(index = 0; index < N; index++) { arr[index] = rand()%100 + 1; } show(arr, N); bubble_sort2(arr, N); show(arr, N); system("pause"); return 0; }
4. 归并排序
示意图:
代码如下:
/** * 归并排序 */ #include <stdio.h> #include <stdlib.h> #include <time.h> #define N 20 void merge(int arr[],int start,int mid,int end) { int i,j,k; int n1 = mid - start + 1; int n2 = end - mid; int *left = (int *)malloc(sizeof(int) * n1); int *right = (int *)malloc(sizeof(int) * n2); for(i = 0; i < n1; i++) left[i] = arr[start + i]; for(j = 0; j < n2; j++) right[j] = arr[mid+1+j]; i = 0; j = 0; k = start; while(i < n1 && j < n2) { if(left[i] < right[j]) arr[k++] = left[i++]; else arr[k++] = right[j++]; } while(i < n1) arr[k++] = left[i++]; while(j < n2) arr[k++] = right[j++]; free(left); free(right); }
void sort(int arr[], int start, int end) { int mid; if(start < end) { mid = (start + end) / 2; sort(arr, start, mid); sort(arr, mid+1, end); merge(arr,start,mid,end); } }
int main(int argc, char * argv[]) { int i,a[N]; srand(time(NULL)); for(i = 0; i < N; i++) a[i] = rand() % 100; printf("归并排序: "); printf("before : "); for(i = 0; i < N; i++) printf("%-2d ",a[i]); printf(" "); sort(a,0,N-1); printf("after : "); for(i = 0; i < N; i++) printf("%-2d ",a[i]); printf(" "); system("pause"); return 0; }
方法2:用数组名以及数组长度作参数。如此一来,与之前的排序算法参数就统一起来了。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> static void show(int *arr, int len) { int index; for(index = 0; index < len; index++) { printf("%3d", arr[index]); } printf(" "); } static void merge(int *arr, int len) { int index,i,j; int n1 = len / 2; int n2 = len - n1; int *left = (int*)calloc(n1, sizeof(int)); int *right = (int*)calloc(n2, sizeof(int)); i = j = 0; for(index = 0; index < n1; index++) { left[i++] = arr[index]; } for(; index < len; index++) { right[j++] = arr[index]; } index = i = j = 0; while(i < n1 && j < n2) { if(left[i] < right[j]) { arr[index++] = left[i++]; }else { arr[index++] = right[j++]; } } while(i < n1) { arr[index++] = left[i++]; } while(j < n2) { arr[index++] = right[j++]; } free(left); free(right); left = right = NULL; } static void sort(int *arr, int len) { int n; if(len == 1) { return; }else { n = len/2; sort(arr, n); sort(arr + n, len-n); merge(arr,len); } } int main(int argc, char *argv[]) { int arr[20],i; srand(time(NULL)); for(i = 0; i < 20; i++) { arr[i] = rand()%100; } show(arr,20); sort(arr, 20); show(arr,20); system("pause"); return 0; }