八大排序算法

来源:互联网 发布:激活码的软件 编辑:程序博客网 时间:2024/06/16 12:42

写的很好的排序算法总结http://blog.csdn.net/column/details/algorithm-easyword.html

附上自己的六大排序简短代码

#include <iostream>#include <cstdio>#include <cstring>#define MAX 100using namespace std;void bubble_sort(int *a, int len){int i, j, temp;for(i = 0; i < len - 1; i++){for(j = 0; j < len - 1 - i; j++){if(a[j] > a[j + 1]){temp = a[j];a[j] = a[j + 1];a[j + 1] = temp;}}}}void selection_sort(int *a, int len){int i, j, min, temp;for(i = 0; i < len - 1; i++){min = i;for(j = i + 1; j < len; j++){if(a[j] < a[min]){min = j;}}temp = a[i];a[i] = a[min];a[min] = temp;}}void insertion_sort(int *a, int len){int i, j, temp;for(i = 1; i < len; i++){temp = a[i];//取出未排序序列中的第一个元素 for(j = i; (j > 0) && (temp < a[j - 1]); j--){a[j] = a[j - 1];//依次与已排序序列中的元数比较并右移 }a[j] = temp;//放进合适的位置 }}int partion(int *a, int low, int high){int left, right, temp;left = low;right = high;temp = a[low];while(left < right){while(left < right && temp < a[right]){right--;}if(left < right){a[left] = a[right];left++;}while(left < right && temp > a[left]){left++;}if(left < right){a[right] = a[left];right--;}}a[left] = temp;return left;}void q_sort(int *a, int low, int high){if(low < high){int pivot = partion(a, low, high);q_sort(a, low, pivot - 1);q_sort(a, pivot + 1, high);}}void quick_sort(int *a, int len){q_sort(a, 0, len - 1);}void shell_sort(int *a, int len){int i, j, gap, temp;for(gap = len / 2; gap > 0; gap /= 2)//选择步长 {for(i = gap; i < len; i++){for(j = i - gap; j >= 0 && a[j] > a[j + gap]; j -= gap){temp = a[j];a[j] = a[j + gap];a[j + gap] = temp;}}}}int main(){int a[MAX];int n, i;printf("please input the total numbers:");scanf("%d", &n);for(i = 0; i < n; i++){printf("please input the number:");scanf("%d", &a[i]);}//bubble_sort(a, n);//selection_sort(a, n);//insertion_sort(a, n);//quick_sort(a, n);//shell_sort(a, n);for(i = 0; i < n; i++){printf("%d\t", a[i]);}printf("\n");return 0;}


0 0
原创粉丝点击