稳定性:快速 希尔 选择 堆排序不稳定

时间复杂度:平均情况下,快速、希尔、归并和堆排序的时间复杂度均为O(nlog2(n)),其他都是O(n^2)。最坏情况下,快排的时间复杂度为O(n^2)

 #include <iostream>
#include <stdlib.h>
#include <time.h>
#define N 1000
using namespace std; //输出
void output(int a[], int num){
for(int i=;i<num;i++){
cout<<a[i]<<" ";
}
cout<<endl;
} //交换元素位置
void swap(int a[], int index1, int index2){
int tmp = a[index1];
a[index1] = a[index2];
a[index2] = tmp;
} //快速排序
void quickSort(int a[], int l, int r, int num){
if(l<r){
int left = l;
int right = r;
int tmp = a[l];
while(left<right){
while(left<right && a[right]>=tmp){
right--;
}
if(left<right){
a[left++] = a[right];
}
while(left<right && a[left]<=tmp){
left++;
}
if(left<right){
a[right--] = a[left];
}
}
a[left] = tmp;
//output(a,num);
quickSort(a,l,left-,num);
quickSort(a,left+,r,num);
}
} //冒泡排序
void bubbleSort(int a[], int num){
int i,j;
int flag = ;
for(i=num-;i>;i--){
flag = ;
for(j=;j<i;j++){
if(a[j]>a[j+]){
swap(a,j,j+);
flag=;
}
}
if(flag==){
break;
}
}
} //选择排序
void selectSort(int a[], int num){
int right,i;
int maxIndex;
for(right=num-;right>;right--){
maxIndex = ;
for(i=;i<right;i++){
if(a[maxIndex]<a[i]){
maxIndex = i;
}
}
swap(a,maxIndex,right);
} } //建立大顶堆
void buildHeap(int a[], int index, int len){
int curParent = a[index];
int child = index * +;
while(child<len){
if(child+<len && a[child]<a[child+]){
child++;
}
if(curParent < a[child]){
a[index] = a[child];
//这里不用把curParent赋值给child,因为还要迭代子树,如果孩子中有更大的,会上移,curParent还要继续下移
index = child;
child = child * +;
} else {
break;
}
}
a[index] = curParent;
} /*
void buildHeap(int a[], int i, int len){
int parent = a[i];
int left = i*2+1;
while(child < len){
if(child+1<len && a[child]<a[child+1]){
child++;
}
if(parent<a[child]){
a[i] = child;
child = 2*i+1;
} else {
break;
}
}
a[p] = parent;
}*/ //堆排序
void heapSort(int a[], int num){
int i;
for(i=num/-;i>=;i--){
buildHeap(a, i, num);
}
for(i=num-;i>=;i--){
int max = a[];
a[] = a[i];
a[i] = max;
buildHeap(a,,i);
} }
//插入排序
void insertSort(int a[], int num){
int i,j,tmp;
for(i=;i<num;i++){
tmp = a[i];
for(j=i-;j>=;j--){
if(a[j]>tmp){
a[j+] = a[j];
} else {
break;
}
}
a[j+] = tmp;
}
} void merge(int a[], int first, int mid, int last){
int *tmp = (int *)malloc((last-first+)*sizeof(int));
int i = first;
int j = mid+;
int k = ;
while(i<=mid && j<=last){
if(a[i]<=a[j]){
tmp[k++] = a[i++];
} else {
tmp[k++] = a[j++];
}
}
while(i<=mid){
tmp[k++] = a[i++];
}
while(j<=last){
tmp[k++] = a[j++];
}
for(i=first;i<=last;i++){
a[i] = tmp[i-first];
}
free(tmp);
} void mergeSort(int a[], int first, int last){
int mid = (first+last)/;
if(first<last){
mergeSort(a,first,mid);
mergeSort(a,mid+,last);
merge(a,first,mid,last);
}
} int main(){
int num;
while(cin>>num){
int i;
start = clock();
int *arr = new int[num];
for(int i=; i<num; i++){
arr[i] = rand()%;
//cin>>arr[i];
}
cout<<"待排序数组为:"<<endl;
output(arr,num); //quickSort(arr,0,num-1,num); //快速排序
//bubbleSort(arr,num); //冒泡排序
//selectSort(arr,num); //归并排序
//insertSort(arr,num); //插入排序
//heapSort(arr,num); //堆排序
//mergeSort(arr,0,num-1); //归并排序
stop = clock();
cout<<stop-start<<endl; cout<<"排序后的数组为:"<<endl;
output(arr,num); }
return ;
}

C++实现排序算法的更多相关文章

  1. JavaScript实现常用的排序算法

    ▓▓▓▓▓▓ 大致介绍 由于最近要考试复习,所以学习js的时间少了 -_-||,考试完还会继续的努力学习,这次用原生的JavaScript实现以前学习的常用的排序算法,有冒泡排序.快速排序.直接插入排 ...

  2. 排序算法----基数排序(RadixSort(L))单链表智能版本

    转载http://blog.csdn.net/Shayabean_/article/details/44885917博客 先说说基数排序的思想: 基数排序是非比较型的排序算法,其原理是将整数按位数切割 ...

  3. 常见排序算法(附java代码)

    常见排序算法与java实现 一.选择排序(SelectSort) 基本原理:对于给定的一组记录,经过第一轮比较后得到最小的记录,然后将该记录与第一个记录的位置进行交换:接着对不包括第一个记录以外的其他 ...

  4. 几大排序算法的Java实现

    很多的面试题都问到了排序算法,中间的算法和思想比较重要,这边我选择了5种常用排序算法并用Java进行了实现.自己写一个模板已防以后面试用到.大家可以看过算法之后,自己去实现一下. 1.冒泡排序:大数向 ...

  5. 排序算法----基数排序(RadixSort(L,max))单链表版本

    转载http://blog.csdn.net/Shayabean_/article/details/44885917博客 先说说基数排序的思想: 基数排序是非比较型的排序算法,其原理是将整数按位数切割 ...

  6. 排序算法汇总(C/C++实现)

    前言:     本人自接触算法近2年以来,在不断学习中越多地发觉各种算法中的美妙.之所以在这方面过多的投入,主要还是基于自身对高级程序设计的热爱,对数学的沉迷.回想一下,先后也曾参加过ACM大大小小的 ...

  7. 用Java来写常见的排序算法

    随着校招的临近 算法是校招中很重要的一个部分 总结了常见几种排序算法,各种算法的时间复杂度和空间复杂度大家也需要多了解下 package com.huwei.sort; /** * 各种排序算法 * ...

  8. 模板化的七种排序算法,适用于T* vector<T>以及list<T>

    最近在写一些数据结构以及算法相关的代码,比如常用排序算法以及具有启发能力的智能算法.为了能够让写下的代码下次还能够被复用,直接将代码编写成类模板成员函数的方式,之所以没有将这种方式改成更方便的函数模板 ...

  9. 排序算法总结第二弹----冒泡排序---javascript描述

    上篇博文总结了选择排序,这篇来看冒泡排序,接上篇. 冒泡排序思想:若是正再将一组数据升序排序, 第一趟:比较相邻的数据,当左侧值大于右侧值将他们进行交换,将较小值向前浮动,大值向后冒泡,直至比较到最后 ...

  10. 排序算法总结------选择排序 ---javascript描述

    每当面试时避不可少谈论的话题是排序算法,上次面试时被问到写排序算法,然后脑袋一懵不会写,狠狠的被面试官鄙视了一番,问我是不是第一次参加面试,怎么可以连排序算法都不会呢?不过当时确实是第一次去面试,以此 ...

随机推荐

  1. MyEclipse2018.9.0设置全局编码

    1.windows->Preferences打开"首选项"对话框,左侧导航,导航到general->Workspace 右侧Text file encoding,选择O ...

  2. js验证开头不为零的正整数

    WST.zhengZhengShuIn = function (className){ var rex = /^[1-9]{1}[0-9]*$/;//正整数 $("."+class ...

  3. mvc下ajax请求遇到session超时简单处理方式

    转自:http://blog.csdn.net/yeyicsdn/article/details/50032787 参考网址:http://www.cnblogs.com/RachelChen/p/5 ...

  4. db2 sql调优

    当我们发现某个SQL语句执行很慢时,可以通过查看它的访问计划来定位原因,如是否执行了合适的索引.是否采用了正确的连接方法等.但是我们发现很多用户对访问计划的生成和解释工具的使用存在很多疑惑,本文通过一 ...

  5. bzr: ERROR: These branches have diverged. Use the missing command to see how.

    这个错误是在提交之后执行bzr pull时出现的,先uncommit,再pull就可以了.

  6. 什么是RNA-Seq (RNA Sequencing)

    什么是RNA-Seq (RNA Sequencing) 2011-07-14 ~ ADMIN 随着ome为词尾的各种组学的出现,转录组学已经成为了人们了解生物信息的一个重要组成部分.人们使用了许多办法 ...

  7. Python和JavaScript间代码转换4个工具-乾颐堂

    Python 还是 JavaScript?虽然不少朋友还在争论二者目前谁更强势.谁又拥有着更为光明的发展前景,但毫无疑问,二者的竞争在 Web 前端领域已经拥有明确的答案.立足于浏览器平台,如果放弃 ...

  8. process概念

    multiprocess: multiprocess.cpu_count():统计cpu核数 multiprocess.active_chirdren():获取所有的子进程 multiprocess. ...

  9. dateframe_loc.iloc.ix

    import pandas as pddf=pd.DataFrame({ "a":[1,2,3], "b":[4,5,6], "c":[7, ...

  10. HTML5 history详解

    最近研究vue-router单页转跳而不向服务器请求的原理, 主要是HTML5 history以及hash的应用,支持history时使用history模式 下面详细学习了一下常用的history相关 ...