几种常见排序算法
作者:互联网
几种常见排序算法
使用语言及编译环境
- 使用语言:C/C++
- 编译环境:VS Code
冒泡排序
参照https://www.bilibili.com/video/BV1T4411A7Fy?p=7
- 原理:数组元素两两比较,交换元素,大元素往后放
#include <iostream> using namespace std; int main() { int arrayLength = 6; int array[arrayLength] = {10,2,5,80,35,60}; for (int i = 0; i < arrayLength; i++) { cout << array[i] << " "; } cout << endl; for (size_t i = 0; i < arrayLength-1; i++) { for (size_t j = 0; j < arrayLength-1-i; j++) { if (array[j] > array[j+1]) { int temp = array[j+1]; array[j+1] = array[j]; array[j] = temp; } } } for (int i = 0; i < arrayLength; i++) { cout << array[i] << " "; } cout << endl; system("pause"); return 0; }
选择排序
参照https://www.bilibili.com/video/BV1T4411A7Fy?p=8
- 原理:从0索引处开始,将该位置的元素依次和后面的元素进行比较,小的元素往前放
#include <iostream> using namespace std; int main() { int arrayLength = 6; int array[arrayLength] = {10,2,5,80,35,60}; for (int i = 0; i < arrayLength; i++) { cout << array[i] << " "; } cout << endl; for (size_t index = 0; index < arrayLength-1; index++) { for (size_t i = index+1; i < arrayLength; i++) { if (array[index] > array[i]) { int temp = array[index]; array[index] = array[i]; array[i] = temp; } } } for (int i = 0; i < arrayLength; i++) { cout << array[i] << " "; } cout << endl; system("pause"); return 0; }
直接插入排序
参照https://www.bilibili.com/video/BV1T4411A7Fy?p=9
- 原理:从1索引处开始,将后面的元素插入之前的有序列表中,使之仍保持有序
#include <iostream> using namespace std; int main() { int arrayLength = 6; int array[arrayLength] = {10,2,5,80,35,60}; for (int i = 0; i < arrayLength; i++) { cout << array[i] << " "; } cout << endl; for (size_t i = 1; i < arrayLength; i++) { for (size_t j = i; j > 0; j--) { if (array[j] < array[j-1]) { int temp = array[j]; array[j] = array[j-1]; array[j-1] = temp; } } } for (int i = 0; i < arrayLength; i++) { cout << array[i] << " "; } cout << endl; system("pause"); return 0; }
希尔排序
参照https://www.bilibili.com/video/BV1T4411A7Fy?p=10
- 原理:合理选取增量,一轮排序后,不断缩小增量进行插入排序,直到增量为1,排序就结束
- 直接插入排序相当于增量为1的希尔排序
- 增量的合理选择:克努特(Knuth)序列导入,h=3*h+1
#include <iostream> using namespace std; int main() { int arrayLength = 6; int array[arrayLength] = {10,2,5,80,35,60}; for (int i = 0; i < arrayLength; i++) { cout << array[i] << " "; } cout << endl; //克努特序列选取增量 int spac = 1; while (spac <= arrayLength/3) { spac = spac*3+1; } //cout << "spac: " << spac << endl; for (size_t h = spac; h > 0; h=(h-1)/3) { for (size_t i = h; i < arrayLength; i++) { for (size_t j = i; j > h-1; j-=h) { if (array[j] < array[j-h]) { int temp = array[j]; array[j] = array[j-h]; array[j-h] = temp; } } } } for (int i = 0; i < arrayLength; i++) { cout << array[i] << " "; } cout << endl; system("pause"); return 0; }
快速排序
参照https://www.bilibili.com/video/BV1at411T75o/?spm_id_from=333.788
- 思想:分治法,比大小,在分区
- 原理:从数组中取一个数,将比这个数大的全放到右边,比这个数小的全放到左边;对左右两个分区进行此步骤,直到最后分区只有一个数
- 步骤:选一个基准数,先从后往前找小于它的数,将这个数放到基准数的位置,然后在从前往后找大于等于它的数,将这个数放到上一个小于基准数的位置,直到左右两个寻找的指针重合,然后重复对左区和右区进行上述操作,直到分区只有一个元素的时候,就认为整个数组有序了
#include <iostream> using namespace std; void quickSort(int *arr, int start, int end); int main() { int arrayLength = 6; int array[arrayLength] = {10,2,5,80,35,60}; for (int i = 0; i < arrayLength; i++) { cout << array[i] << " "; } cout << endl; quickSort(array, 0, arrayLength-1); for (int i = 0; i < arrayLength; i++) { cout << array[i] << " "; } cout << endl; system("pause"); return 0; } void quickSort(int *arr, int start, int end) { if (start>=end) { return; } int L = start; int R = end; int pivot = arr[L]; while (L<R) { //由后往前找比它小的数 while (L<R && arr[R]>=pivot) { R--; } if (L<R) { arr[L] = arr[R]; L++; } //由前往后找比它大或者等于的数 while (L<R && arr[L]<pivot) { L++; } if (L<R) { arr[R] = arr[L]; R--; } //把基准数放到最后L和R重合的地方 if (L>=R) { arr[L] = pivot; } } //递归处理左区和右区 quickSort(arr, start, L-1); quickSort(arr, L+1, end); }
归并排序
参照:https://www.bilibili.com/video/BV1Pt4y197VZ?spm_id_from=333.337.search-card.all.click
- 思想:先划分再合并
- 原理:将数组中每个元素分为一组,然后两两组合,保证每次组合出来的数组都是有序的,直到只剩下一组的时候,排序完成
#include <iostream> using namespace std; void mergeSort(int *arr, int size); int main() { int arrayLength = 6; int array[arrayLength] = {10,2,5,80,35,60}; for (int i = 0; i < arrayLength; i++) { cout << array[i] << " "; } cout << endl; mergeSort(array, arrayLength); for (int i = 0; i < arrayLength; i++) { cout << array[i] << " "; } cout << endl; system("pause"); return 0; } //合并 void merge(int *arr, int *tempArr, int left, int mid, int right) { int l = left; //标记左半区第一个未排序元素 int r = mid+1;//标记右半区第一个未排序元素 int t_pos = left;//临时数组的下标 //合并 while ((l <= mid)&&(r <= right)) { if (arr[l] <= arr[r]) { tempArr[t_pos++] = arr[l++]; } else { tempArr[t_pos++] = arr[r++]; } } //合并左半区剩余的元素 while (l <= mid) { tempArr[t_pos++] = arr[l++]; } //合并右半区剩余的元素 while (r <= right) { tempArr[t_pos++] = arr[r++]; } //把临时数组的元素放入原数组 while (left <= right) { arr[left] = tempArr[left]; left++; } } //归并排序 void mSort(int *arr, int *tempArr, int left, int right) { if (left<right) { int mid = (left+right)/2; mSort(arr, tempArr, left, mid); //递归划分左半区域 mSort(arr, tempArr, mid+1, right); //递归划分右半区域 merge(arr, tempArr, left, mid, right); //合并部分 } return; } //入口函数 void mergeSort(int *arr, int size) { int *tempArr = new int[sizeof(int)*size]; if (tempArr != NULL) { mSort(arr, tempArr, 0, size-1); delete []tempArr; } return; }
后续会继续补充
标签:cout,temp,int,几种,++,算法,arrayLength,array,排序 来源: https://www.cnblogs.com/dai-xx/p/16354658.html