插入排序:
插入排序在最坏情况和平均情况下时间复杂度是Θ(n2)
#include <stdio.h> #define LEN 5 int a[LEN] = {10, 5, 2, 4, 7}; void print(void) { for (int i = 0; i < LEN; i++) printf(" %d", a[i]); printf(" "); } void insertion_sort(void) //该算法要求背诵 { int j, key; for (int i = 1; i < LEN; i++) { print(); key = a[i]; for (j = i - 1; j >= 0 && a[j] > key; j--) a[j + 1] = a[j]; a[j + 1] = key; } print(); } int main(void) { insertion_sort(); return 0; }
选择排序:
#include <stdio.h> #define LEN 5 int a[LEN] = {10, 5, 2, 4, 7}; void print(void) { for (int i = 0; i < LEN; i++) printf(" %d", a[i]); printf(" "); } void selection_sort(void) //该算法要求背诵 { int min, low; for (int i = 0; i < LEN - 1; i++) { print(); min = a[i]; low = i; for (int j = i + 1; j < LEN; j++) { if (a[j] < min) { min = a[j]; low = j; } } a[low] = a[i]; a[i] = min; } print(); } int main(void) { selection_sort(); return 0; }
归并排序:
1 #include <stdio.h> 2 #define LEN 8 3 int a[LEN] = {5, 2, 4, 7, 1, 3, 2, 6}; 4 5 void merge(int start, int mid, int end) 6 { 7 int n1 = mid - start + 1; // mid - start + 1 8 int n2 = end - mid; // end - (mid+1) + 1 9 int left[n1], right[n2]; 10 11 //分别将a数组的左半部分和右半部分,拷贝到left和right数组 12 for (int i = 0; i < n1; i++) /* left holds a[start..mid] */ 13 left[i] = a[start + i]; 14 for (int j = 0; j < n2; j++) /* right holds a[mid+1..end] */ 15 right[j] = a[mid + 1 + j]; 16 17 //归并操作,将排序完毕的left和right中较小的切片拷贝到a数组 18 int i = 0, j = 0, k = start; 19 while (i < n1 && j < n2) 20 { 21 if (left[i] < right[j]) 22 { 23 a[k] = left[i]; 24 k++, i++; //这两条语句等价于a[k++] = left[i++]; 25 } 26 else 27 { 28 a[k] = right[j]; 29 k++, j++; //这两条语句等价于a[k++] = right[j++]; 30 } 31 } 32 33 //经过上一步的归并,将left或者right中剩余的部分拷贝至a数组 34 while (i < n1) /* left[] is not exhausted */ 35 a[k++] = left[i++]; /* 循环体的展开形式同上 */ 36 while (j < n2) /* right[] is not exhausted */ 37 a[k++] = right[j++]; 38 } 39 40 void print(void) 41 { 42 for (int i = 0; i < LEN; i++) 43 printf(" %d", a[i]); 44 printf(" "); 45 } 46 47 void sort(int start, int end) 48 { 49 int mid; 50 if (start < end) 51 { 52 mid = (start + end) / 2; 53 printf("sort (%d-%d, %d-%d)", start, mid, mid + 1, end); 54 print(); 55 56 sort(start, mid); 57 sort(mid + 1, end); 58 merge(start, mid, end); 59 60 printf("merge (%d-%d, %d-%d)", start, mid, mid + 1, end); 61 print(); 62 } 63 } 64 65 int main(int argc, char const *argv[]) 66 { 67 sort(0, LEN - 1); 68 return 0; 69 }
快速排序:
1 #include <stdio.h> 2 #define LENGTH 8 3 4 int a[LENGTH] = {5, 2, 4, 7, 1, 3, 8, 6}; 5 6 int partition(int, int); 7 void quicksort(int, int); 8 9 void quicksort(int start, int end) 10 { 11 12 if (start < end) 13 { 14 int mid = partition(start, end); 15 quicksort(start, mid - 1); 16 quicksort(mid + 1, end); 17 } 18 } 19 20 int partition(int start, int end) 21 { 22 int pivot = a[end]; 23 24 while (start < end) 25 { 26 while (start < end && a[start] <= pivot) 27 start++; 28 a[end] = a[start]; 29 30 while (start < end && a[end] >= pivot) 31 end--; 32 a[start] = a[end]; 33 } 34 35 a[start] = pivot; 36 return start; 37 } 38 39 int main(int argc, char const *argv[]) 40 { 41 for (int i = 0; i < LENGTH; i++) 42 printf(" %d", a[i]); 43 printf(" "); 44 45 quicksort(0, LENGTH - 1); 46 47 for (int i = 0; i < LENGTH; i++) 48 printf(" %d", a[i]); 49 printf(" "); 50 51 return 0; 52 }