插入排序
#include <stdio.h> int Insertsort(int b[], int n) { int i, j; int tmp; for(i = 1; i < n; i++) { tmp = b[i]; for(j = i - 1; j >= 0; j--) { if(tmp < b[j]) { b[j + 1] = b[j]; } else { break; } } b[j + 1] = tmp; } } int main() { int i = 0; int a[] = {1, 5, 7, 9, 4, 7, 2, 8, 6}; int length = sizeof(a) / sizeof(a[0]); Insertsort(a, length); for(i = 0; i < length; i++) { printf("%d ", a[i]); } printf("\n"); return 0; }
希尔排序
#include <stdio.h> int Shellsort(int b[], int n) { int i, j, h; int tmp; for(h = n / 2; h > 0; h /= 2) { for(i = h; i < n; i++) { tmp = b[i]; for(j = i - h; j >= 0; j -= h) { if(tmp < b[j]) { b[j + h] = b[j]; } else { break; } } b[j + h] = tmp; } } } int main() { int i = 0; int a[] = {1, 5, 7, 9, 4, 7, 2, 8, 6}; int length = sizeof(a) / sizeof(a[0]); Shellsort(a, length); for(i = 0; i < length; i++) { printf("%d ", a[i]); } printf("\n"); return 0; }
简单选择排序
#include <stdio.h> Selectsort(int *a, int n) { int i, j; int tmp = 0; int min = 0; for(i = 0; i < n - 1; i++) { tmp = a[i]; min = i; for(j = i + 1; j < n; j++) { if(a[j] < tmp) { tmp = a[j]; min = j; } } if(min != i) { a[min] = a[i]; a[i] = tmp; } } } int main() { int i; int a[] = {12, 2, 6, 8, 30, 7, 9, 0}; int len = sizeof(a) / sizeof(a[0]); Selectsort(a, len); for(i = 0; i < len; i++) { printf("%d ", a[i]); } printf("\n"); return 0; }
快速排序
#include <stdio.h> int Sort(int array[], int low, int high) { int i, j; int index; if(low >= high) { return ; } i = low; j = high; index = array[i]; while(i < j) { while(i < j && array[j] >= index) { j--; } if(i < j) { array[i++] = array[i]; } while(i < j && array[i] < index) { i++; } if(i < j) { array[j--] = array[i]; } } array[i] = index; Sort(array, low, i - 1); Sort(array, i + 1, high); return 1; } int Quicksort(int array[], int length) { Sort(array, 0, length - 1); return 1; } int main() { int i = 0; int a[] = {29, 96, 18, 56, 3, 56, 39, 77}; int length = sizeof(a) / sizeof(a[0]); Quicksort(a, length); for(i = 0; i < length; i++) { printf("%d ", a[i]); } printf("\n"); return 0; }
堆排序
#include <stdio.h> void AdjustMinHeap(int *a, int pos, int len) { int temp; int child; for(temp = a[pos]; 2 * pos + 1 <= len; pos = child) { child = 2 * pos + 1; if(child < len && a[child] > a[child + 1]) { child++; } if(a[child] < temp) { a[pos] = a[child]; } else { break; } } a[pos] = temp; } void Swap(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } void Printarray(int *a, int length) { int i; for(i = 0; i < length; i++) { printf("%d ", a[i]); } printf("\n"); } void Heapsort(int *array, int length) { int i; for(i = length / 2 - 1; i >= 0; i--) { AdjustMinHeap(array, i, length - 1); } for(i = length - 1; i >= 0; i--) { Swap(&array[0], &array[i]); AdjustMinHeap(array, 0, i - 1); } } int main() { int array[] = {0, 13, 1, 14, 27, 18}; int length = sizeof(array) / sizeof(array[0]); Heapsort(array, length); Printarray(array, length); return 0; }
基数排序(代码1)
#include <stdio.h> #include <stdlib.h> int RadixCountSort(int *index, int *a, int len) //收集 { int *count = (int *)malloc(sizeof(int) * 10); int i; for (i = 0; i < len; ++i) { count[i] = 0; } for (i = 0; i < len; ++i) { ++count[index[i]]; } for (i = 1; i < 10; ++i) { count[i] += count[i - 1]; } int *sort = (int *)malloc(sizeof(int) * len); for (i = len - 1; i >= 0; --i) { --count[index[i]]; sort[count[index[i]]] = a[i]; } for (i = 0; i < len; ++i) { a[i] = sort[i]; } free(sort); free(count); return 1; } int RadixSort(int *a, int len) //分配 { int *radix = (int *)malloc(sizeof(int) * len); int radix_base = 1; int is_ok = 0; while (!is_ok) { int i; is_ok = 1; radix_base *= 10; for (i = 0; i < len; ++i) { radix[i] = a[i] % radix_base; radix[i] /= radix_base / 10; if (a[i] / (radix_base / 10) > 0) { is_ok = 0; } } if (is_ok) { break; } RadixCountSort(radix, a, len); } free(radix); return 1; } int main() { int i; int a[] = {278, 109, 63, 930, 589, 184, 505, 269, 8, 83}; int len = sizeof (a) / sizeof (a[0]); RadixSort(a, len); for (i = 0; i < len; ++i) { printf("%d ", a[i]); } printf("\n"); return 0; }
基数排序(代码2)
/******************* *File name : Radixsort.c *Author : Lifengyu *Date : 2018.8.11 *Funtions : 基数排序 *******************/ #include <stdio.h> #include <stdlib.h> /******************* * 获取数组a中最大值 * * 参数说明: * a -- 数组 * n -- 数组长度 *******************/ int Get_max(int *a, int n) { int i, max; max = a[0]; for( i = 1; i < n; i++) { if(a[i] > max) { max = a[i]; } } return max; } /************************************* * 对数组按照"某个位数"进行排序(桶排序) * * 参数说明: * a -- 数组 * n -- 数组长度 * exp -- 指数。对数组a按照该指数进行排序。 * * 例如,对于数组a={278, 109, 63, 930, 589, 184, 505, 269, 8, 83}; * (01) 当exp=1表示按照"个位"对数组a进行排序 * (02) 当exp=10表示按照"十位"对数组a进行排序 * (03) 当exp=100表示按照"百位"对数组a进行排序 * ... **************************************/ int count_sort(int *a, int n, int exp) { int output[n]; // 存储"被排序数据"的临时数组 int i; int buckets[10] = {0}; //桶数组 // 将数据最后一位对应的数存储在buckets[]对应的桶中,则该桶++ for(i = 0; i < n; i++) { buckets[(a[i] / exp) % 10 ] ++; } //更改buckets[i]。目的是让更改后的buckets[i]的值,是该数据在output[]中的位置。 for(i = 1; i < 10; i++) { buckets[i] += buckets[i - 1]; } // 将数据存储到临时数组output[]中 for( i = n - 1; i >= 0; i--) { output[buckets[ (a[i] / exp) % 10 ] -1 ] = a[i]; buckets[ (a[i] / exp) % 10]--; } // 将排序好的数据赋值给a[] for(i = 0; i < n; i++) { a[i] = output[i]; } return 1; } /****************** * 基数排序 * * 参数说明: * a -- 数组 * n -- 数组长度 ******************/ int Radix_sort(int *a, int n) { int exp; //指数,对个位排序为1,对十位排序为10... int max = Get_max(a, n); // 数组a中的最大值 // 从个位开始,对数组a按"指数"进行排序 for(exp = 1; max / exp > 0; exp *= 10) { count_sort( a, n, exp); } return 1; } int main() { int i; int a[] = {278, 109, 63, 930, 589, 184, 505, 269, 8, 83};//按照PPT上的数据 int len = sizeof(a) / sizeof(a[0]); //求数组的长度 printf("Before sort : "); for(i = 0; i < len; i ++) { printf("%d ", a[i]); } printf("\n\n"); Radix_sort(a, len); printf("After sort : "); for(i = 0; i < len; i++) { printf("%d ", a[i]); } printf("\n"); return 0; }
归并排序
#include <stdio.h> #include <stdlib.h> void Merge(int array[], int start, int middle, int end) { int i, j, k, n1, n2; n1 = middle - start + 1; n2 = end - middle; int *L = (int *)malloc(n1 * sizeof(int)); int *R = (int *)malloc(n2 * sizeof(int)); for (i = 0, k = start; i < n1; i++, k++) { L[i] = array[k]; } for (i = 0, k = middle + 1; i < n2; i++, k++) { R[i] = array[k]; } for (k = start, i = 0, j = 0; i < n1 && j < n2; k++) { if (L[i] < R[j]) { array[k] = L[i]; i++; } else { array[k] = R[j]; j++; } } if (i < n1) { for (j = i; j < n1; j++, k++) { array[k] = L[j]; } } if (j < n2) { for (i = j; i < n2; i++, k++) { array[k] = R[i]; } } } void MergeSort(int array[], int start, int end) { int middle; int i; if (start < end) { middle = (start + end) / 2; MergeSort(array, start, middle); MergeSort(array, middle + 1, end); Merge(array, start, middle, end); } } int main() { int i = 0; int a[] = {49, 38, 65, 97, 76, 13, 27}; int length = sizeof(a) / sizeof(a[0]); MergeSort(a, 0, length -1); for (i = 0 ; i < length; i++) { printf("%d ", a[i]); } printf("\n"); return 0; }