稳定排序:冒泡排序、插入排序、归并排序、基数排序、桶排序
不稳定排序:选择排序、快速排序、希尔排序、堆排序
二、插入排序:
代码:
#include<iostream> #include<cstdio> #include<stdlib.h> #include<vector> using namespace std; int main() { int A[6] = { 4,5,6,3,2,1 }; int n = 6; int i, j; for (i = 1; i < n; i++) { if (A[i] < A[i - 1])//i为要检查的元素,要和前面的有序序列(从小到大) { int temp = A[i];//哨兵位 for (j = i - 1; j >=0; j--)//和有序队列的每一位元素进行比较,插入合适的位置 { if (A[j] > temp) { A[j + 1] = A[j];//j移到j+1的位置,都往后挪一位 break; } } A[j+1] = temp;//最后一个元素是j+1,为什么,因为可能到-1 } } for (int j = 0; j < n; j++) { cout <<A[j] << endl; } return 0; }
三、希尔排序
在插入排序的基础上
这里王道讲的代码,有n个数的话实际上留了n+1位,第一位空着做哨兵位。我对数组修改
#include<iostream> #include<cstdio> #include<stdlib.h> #include<vector> using namespace std; int main() { int A[6] = { 4,5,6,3,2,1 }; int n = 6;//留第一个位置做哨兵位 int i, j; for (int d = n / 2; d >= 1; d = d / 2)//每一趟d都进行缩小 { for (i = d; i <n;i++)//注意是从d开始 { if (A[i] < A[i - d])//说明可以插入的有序队列,这个if到下面的逻辑基本是插入排序,找到一个合适的位置插入,其余往后移动 { int temp = A[i]; for (j = i - d; j >=0 && temp< A[j]; j = j - d) { A[j+d] = A[j]; } A[j + d] = temp; } } } for (int j = 0; j < n; j++) { cout << A[j] << endl; } return 0; }
三、冒泡排序(相邻位置元素进行比较和swap,直到把最大的元素“冒”到最后)
#include<iostream> #include<cstdio> #include<stdlib.h> #include<vector> using namespace std; void swap(int *a, int *b) { int temp = *a; *a= *b; *b = temp; } void Swap1(int& x, int& y) { int temp = x; x = y; y = temp; } int main() { int A[6] = { 4,5,6,3,2,1 }; int n = 6; for (int i = 0; i < n-1; i++) { bool flag = false; for (int j =0; j < n -1-i; j++) { if (A[j] > A[j + 1]) { Swap1(A[j], A[j+1]); flag = true; } } if (flag == false)//代表一次都没有发生交换,说明数组有序,可以停止比较了 { break; } } for (int j = 0; j < n; j++) { cout << A[j] << endl; } return 0; }
快速选择排序------------------基于交换排序,前序遍历构建树,枢轴,
#include<iostream> #include<cstdio> #include<stdlib.h> #include<vector> using namespace std; //类似前序遍历构建树 //1、找到枢轴的位置 int Partition(int A[], int low, int high) { int base = A[low]; while (low < high) { while (low <high && A[high] >= base) { high--; } A[low] = A[high]; while (low < high && A[low] <= base) { low++; } A[high] = A[low]; } A[low] = base; return low; } //前序遍历构建树 void QuickSort(int A[], int low, int high) { if (low < high) { int root_index = Partition(A, low, high); QuickSort(A, low, root_index - 1); QuickSort(A, root_index + 1, high); } } int main() { int A[6] = { 4,5,6,3,2,1 }; int n = 6; for (int i=0;i<n;i++) { cout << A[i] << " "; } cout << endl; QuickSort(A, 0, n - 1); for (int i = 0; i < n; i++) { cout << A[i] << " "; } cout << endl; return 0; }
简单选择排序
#include<iostream> #include<cstdio> #include<stdlib.h> #include<vector> using namespace std; void Swap3(int& a, int& b) { int temp = a; a = b; b = temp; } int main() { int A[6] = { 4,5,6,3,2,1 }; int n = 6; for (int i = 0; i < n - 1; i++) { int minn = i; for (int j = i + 1; j < n; j++) { if (A[j]<A[minn]) { minn = j; } } if (minn != i) { Swap3(A[i], A[minn]); } } for (int j = 0; j < n; j++) { cout << A[j] << endl; } return 0; }
归并排序:
#include<iostream> #include<cstdio> #include<stdlib.h> #include<vector> using namespace std; //借助辅助数组 vector<int>B; void Merge(int A[],int low,int mid,int high) { int k = low,i=low,j=mid+1; while(i <= mid && j <= high) { if (A[i] < A[j]) { B[k++] = A[i++]; } else { B[k++] = A[j++]; } } while (i <= mid) { B[k++] = A[i++]; } while (k <= high) { B[k++] = A[j++]; } for (k = low; k <= high; k++) { A[k] = B[k]; } } void MergeSort(int A[], int low, int high) { if (low < high) { int mid = low + (high - low) / 2; MergeSort(A, low, mid); MergeSort(A, mid+1,high); Merge(A, low, mid, high); } } int main() { int A[6] = { 4,5,6,3,2,1 }; int n = 6; B.resize(n); MergeSort(A, 0, n - 1); for (int i = 0; i < n; i++) { cout << A[i] << " "; } cout << endl; return 0; }
基数排序:
桶排序: