几种排序算法的C++实现

来源:互联网 发布:mac版本的视频播放器 编辑:程序博客网 时间:2024/06/06 05:05

这两天学习几种常见的排序算法,对照书本和一些博客自己实现了一遍,贴在这里当个备份,供以后复习。


#include <iostream>using namespace std;void printA(int *a, int len, int i){cout << "第" << i << "趟排序:" << endl;for (int j=0; j < len; j++)cout << *(a+j) << " ";cout << endl;}//插入排序——直接插入排序(Straight Insertion Sort)void StraightInsertSort(int a[], int len){for (int i=1; i<len; i++){if (a[i] < a[i-1]) //若后一位小于前一位,往前查找到合适的位置,插入。{int tmp = a[i];//a[i] = a[i-1];int j = i - 1;while (j>=0 && tmp < a[j]){a[j+1] = a[j];j--;}a[j+1] = tmp;}printA(a, len, i);}}//插入排序—希尔排序(Shell's Sort)void ShellInsertSort(int a[], int len, int dk){for (int i=dk; i<len; i++){if (a[i] < a[i-dk]) //若后一位小于前一位,往前查找到合适的位置,插入。{int tmp = a[i];//a[i] = a[i-1];int j = i - dk;while (j>=0 && tmp < a[j]){a[j+dk] = a[j];j -= dk;}a[j+dk] = tmp;}printA(a, len, i);}}void ShellSort(int a[], int len){int dk = len/2;while (dk >= 1){ShellInsertSort(a, len, dk);dk /= 2;}}//选择排序—简单选择排序(Simple Selection Sort)void SelectSort(int a[], int len){for (int i=0; i<len; i++){for(int j=i+1; j<len; j++){if (a[i] > a[j])swap(a[i], a[j]);}printA(a, len, i);}}//选择排序—堆排序(Heap Sort)void CreateHeap(int a[], int len){int tag = 1;while (tag == 1){tag = 0;for (int i=0; i<len; i++){if (2*i+2 <= len){if (a[i] < a[2*i+1] || a[i] < a[2*i+2]) //建立大顶堆{tag = 1;if (a[2*i+1] > a[2*i+2])swap(a[i], a[2*i+1]);elseswap(a[i], a[2*i+2]);}}elsebreak;//printA(a, len, i);}}}void HeapSort(int a[], int len){int i=1;int len_tmp = len-1;while (len_tmp >= 2){CreateHeap(a, len_tmp);swap(a[0], a[len_tmp]);len_tmp -= 1;printA(a, len, i);i++;}}//交换排序—冒泡排序(Bubble Sort)void BubbleSort(int a[], int len){int low = 0, high = len-1;for (int i=0; i<len-1; i++) //len-1。因为两两比较 只需要比len-1次{int exchange = 0;for (int j=low; j<high; j++) //正向冒泡,把最大值放high位{if (a[j] > a[j+1]){swap(a[j], a[j+1]);exchange = 1;}}high--;for (int k=high; k>low; k--) //反向冒泡,把最小值放low位{if (a[k] < a[k-1]){swap(a[k], a[k-1]);exchange = 1;}}low++;if (exchange==0) {break;} //上一趟排序没有交换元素,说明已经排好序,breakprintA(a, len, i);}}//交换排序—快速排序(Quick Sort)void QuickSort(int a[], int len, int low, int high){if (high <= low){return;}int left = low, right = high;//用作递归参数int pos = low;while (high > low){while(high>low && a[high] > a[low])//由右往左,找比基数a[low]小的数,和a[low]调换high--;swap(a[high], a[low]);while(high>low && a[high] > a[low])//经过上面的调换,基数变为a[high]。由左往右,找比基数a[high]大的数,和a[high]调换low++;swap(a[high], a[low]);pos = low;//记录下基数最后处于的位置,用作递归参数}if (high <= low) {printA(a, len, 0);}QuickSort(a, len, left, pos-1);QuickSort(a, len, pos+1, right);}//归并排序(Merge Sort)void MergeSort(int a[], int len){if (len <= 1) {//分治的分。归并的归 即递归。分到每个序列只有1个元素时return回上级递归return;}int part1 = len/2;int part2 = len - len/2;int *arry1 = new int[part1];int *arry2 = new int[part2];for (int i=0; i<part1; i++){arry1[i] = a[i];cout << arry1[i] << " ";}cout << endl;for (int i=0; i<part2; i++){arry2[i] = a[i+part1];cout << arry2[i] << " ";}cout << endl;MergeSort(arry1, part1);MergeSort(arry2, part2);int i = 0, j = 0, k = 0;while (i < part1 && j < part2) //分治的治。归并的并 即合并,逐层按序合并当前的小序列。{if (arry1[i] < arry2[j])a[k++] = arry1[i++];elsea[k++] = arry2[j++];}while (i < part1)a[k++] = arry1[i++];while (j < part2)a[k++] = arry2[j++];for (int j=0; j<k; j++)cout << a[j] << " ";cout << endl;}//基数排序(Radix Sort)--按LSD(Least significant digital)排序void RadixSort(int a[], int len){int eachBucketCount[10] = {0};int temp[10][10] = {0};int n = 1, lsd;while (n <= 10)//第一次循环时按最低位(个位数)分类,第二次按十位数分类,n<=10即只做到按十位数分类{for (int i=0; i<len; i++){//if (a[i] < n)lsd = (a[i]/n) % 10; temp[lsd][eachBucketCount[lsd]] = a[i];eachBucketCount[lsd]++;}int k=0, j=0;for (int i=0; i<10; i++)//对当前位({if (eachBucketCount[i] != 0){j = 0;while (j < eachBucketCount[i]){a[k] = temp[i][j++];cout << a[k] << " ";k++;}cout << endl;eachBucketCount[i] = 0;//清零,下一高位排序还要用呢。}}n *= 10;//乘以10,进入下一高位进行基数排序}}int main(){// int a[] = {3,1,5,7,2,4,0,9,6,8};// int a[] = {10,9,55,7,8,73,22,93,0,43,6,14,1,65,39,81,5,28,3,2,4};int a[] = {73, 22, 93, 43, 55, 14, 28, 65, 39, 81};int len = sizeof(a)/sizeof(int);// cout << "插入排序——直接插入排序(Straight Insertion Sort)" << endl;// StraightInsertSort(a, len);// cout << "插入排序—希尔排序(Shell's Sort)" << endl;// ShellSort(a, len);// cout << "选择排序—简单选择排序(Simple Selection Sort)" << endl;// SelectSort(a, len);// cout << "选择排序—堆排序(Heap Sort)" << endl;// HeapSort(a, len);// cout << "交换排序—冒泡排序(Bubble Sort)" << endl;// BubbleSort(a, len);// cout << "交换排序—快速排序(Quick Sort)" << endl;// QuickSort(a, len, 0, len-1);// cout << "归并排序(Merge Sort)" << endl;// MergeSort(a, len);cout << "基数排序(Radix Sort)--LSD" << endl;RadixSort(a, len);cout << "结果:" << endl; for (int i=0; i<len; i++) cout << a[i] << " "; cout << endl;system("pause");return 0;}


原创粉丝点击