加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

数据结构之排序算法

发布时间:2020-12-13 21:15:47 所属栏目:PHP教程 来源:网络整理
导读:#include#include#include#include#includeusing namespace std;void print(int a[],int n,int i) {cout i ":"; for (int j = 0; j8; j++) { cout a[j] " "; } cout endl; } void swap(int a,int b) { int temp; temp = a; a = b; b = temp; } /*插入排序:
#include#include#include#include#includeusing namespace std; void print(int a[],int n,int i) { cout << i << ":"; for (int j = 0; j<8; j++) { cout << a[j] << " "; } cout << endl; } void swap(int a,int b) { int temp; temp = a; a = b; b = temp; } /*插入排序: 将第1待排序序列第1个元素看作1个有序序列,把第2个元素到最后1个元素当做是未排序序列。 从头到尾顺次扫描未排序序列,将扫描到的每一个元素插入有序序列的适当位置。 如果待插入的元素与有序序列中的某个元素相等,则将待插入元素插入到相等元素的后面。 * 时间复杂度也为O(n^2),比冒泡法和选择排序的性能要更好1些 */ void InsertSort(int a[],int n) { for (int i = 1; i1; j--) { if(a[j] i; j--) { if (a[j] < a[j - 1]) swap(a[j],a[j - 1]); } } } /*快速排序 从数列中挑出1个元素,称为 “基准”(pivot), 重新排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的摆在基准的后面(相同的数可以到任1边)。在这个分区退出以后,该基准就处于数列的中间位置。这个称为分区(partition)操作。 递归地(recursive)把小于基准值元素的子数列和大于基准值元素的子数列排序。*/ void QuickSort(int a[],int n)//n为数组的长度 { int low=0,high=n⑴; int temp,temq; int pivot=a[low];//枢轴值 if(n >1 ) { while(low = pivot ) --high; a[low]= a[high];//比pivot小的都放在左侧 while(low

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读