头文件,声明:
1 #ifndef sorts_h 2 #define sorts_h 3 #include <stdio.h> 4 5 void bubbleSort(int arr[], int arrSize); 6 void outArr(char *name, char *time, int arr[], int arrSize); 7 void insertSort(int arr[], int arrSize); 8 void binaryInsertSort(int arr[], int arrSize); 9 void selectSort(int arr[], int arrSize); 10 int partition(int arr[], int low, int high); 11 void quickSort(int arr[], int left, int right); 12 13 #endif
实现文件,
1 #include "sorts.h" 2 3 void bubbleSort(int arr[], int arrSize){ 4 int tmp = 0; 5 int swap = 1; 6 while(swap){ 7 swap = 0; 8 for(int i = 0; i < (arrSize -1); i++){ 9 if(arr[i] > arr[i + 1]){ 10 tmp = arr[i]; 11 arr[i] = arr[i + 1]; 12 arr[i + 1] = tmp; 13 swap = 1; 14 } 15 } 16 } 17 18 return; 19 } 20 21 void outArr(char* name, char* time, int arr[], int arrSize){ 22 printf("%s %s is: ", name, time); 23 for(int i = 0; i < arrSize; i++){ 24 printf("%d ", arr[i]); 25 } 26 printf(" "); 27 28 return; 29 } 30 31 void insertSort(int arr[], int arrSize){ 32 for(int i = 1; i < arrSize; i++){ 33 if(arr[i] < arr[i - 1]){ 34 int j = 0; 35 int tmp = arr[i]; 36 for(j = i - 1; j >= 0 && tmp < arr[j]; j--){ 37 arr[j + 1] = arr[j]; 38 } 39 arr[j + 1] = tmp; 40 } 41 } 42 43 return; 44 } 45 46 void binaryInsertSort(int arr[], int arrSize){ 47 int tmp = 0; 48 int i = 0; 49 int j = 0; 50 int low = 0; 51 int high = 0; 52 for(i = 0; i < arrSize; i++){ 53 tmp = arr[i]; 54 low = 0; 55 high = i - 1; 56 while(low <= high){ 57 int mid = (low + high)/2; 58 if(tmp < arr[mid]){ 59 high = high - 1; 60 }else{ 61 low = low + 1; 62 } 63 } 64 for(j = i - 1; j >= low; j--){ 65 arr[j + 1] = arr[j]; 66 } 67 arr[low] = tmp; 68 } 69 return; 70 } 71 72 void selectSort(int arr[], int arrSize){ 73 for(int i = 0; i < arrSize; i++){ 74 int index = i; 75 for(int j = i + 1; j < arrSize; j++){ 76 if(arr[j] < arr[index]){ 77 index = j; 78 } 79 } 80 if(index != i){ 81 int tmp = arr[index]; 82 arr[index] = arr[i]; 83 arr[i] = tmp; 84 } 85 } 86 87 return; 88 } 89 90 int partition(int arr[], int low, int high){ 91 int i = low; 92 int j = high; 93 int tmp = arr[low]; 94 while(i < j){ 95 while(i < j && arr[j] >= tmp){ 96 j--; 97 } 98 if(i < j){ 99 arr[i] = arr[j]; 100 i++; 101 while(i < j && arr[i] <= tmp){ 102 i++; 103 } 104 if(i < j){ 105 arr[j] = arr[i]; 106 j--; 107 } 108 } 109 } 110 arr[i] = tmp; 111 112 return i; 113 } 114 115 void quickSort(int arr[], int left, int right){ 116 if(left < right){ 117 int tmp = partition(arr, left, right); 118 quickSort(arr, left, tmp - 1); 119 quickSort(arr, tmp + 1, right); 120 } 121 122 return; 123 } 124
测试文件:
1 #include "sorts.h" 2 3 int main(int argc, char **argv) 4 { 5 int arr0[] = {42, 93, 64, 15, 26, 57, 48, 79, 80}; 6 int arr1[] = {42, 93, 64, 15, 26, 57, 48, 79, 80}; 7 int arr2[] = {42, 93, 64, 15, 26, 57, 48, 79, 80}; 8 int arr3[] = {42, 93, 64, 15, 26, 57, 48, 79, 80}; 9 int arr4[] = {42, 93, 64, 15, 26, 57, 48, 79, 80}; 10 int *ptrArr[] = {arr0, arr1, arr2, arr3, arr4}; 11 int arrSize = sizeof(arr0)/sizeof(arr0[0]); 12 outArr("bubbleSort", "before", *(ptrArr + 0), arrSize); 13 14 bubbleSort(*(ptrArr + 0), arrSize); 15 outArr("bubbleSort", "after", *(ptrArr + 0), arrSize); 16 outArr("insertSort", "before", *(ptrArr + 1), arrSize); 17 insertSort(*(ptrArr + 1), arrSize); 18 outArr("insertSort", "after", *(ptrArr + 1), arrSize); 19 outArr("binaryInsertSort", "before", *(ptrArr + 2), arrSize); 20 binaryInsertSort(*(ptrArr + 2), arrSize); 21 outArr("binaryInsertSort", "after", *(ptrArr + 2), arrSize); 22 outArr("selectSort", "before", *(ptrArr + 3), arrSize); 23 selectSort(*(ptrArr + 3), arrSize); 24 outArr("selectSort", "after", *(ptrArr + 3), arrSize); 25 outArr("quickSort", "before", *(ptrArr + 4), arrSize); 26 quickSort(*(ptrArr + 4), 0, arrSize - 1); 27 outArr("quickSort", "after", *(ptrArr + 4), arrSize); 28 29 return 0; 30 }
Makefile文件:
1 testSort:testSort.c sorts.c 2 gcc -g testSort.c sorts.c -o testSort 3 4 clean: 5 rm testSort
代码很简单,可以改进的余地比较大。