博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
经典排序算法回顾:选择排序,快速排序
阅读量:4317 次
发布时间:2019-06-06

本文共 1895 字,大约阅读时间需要 6 分钟。

//选择排序基本思想就是:一个一个最值查找,然后排序

//the simple insertSortWayvoid selectSort(int *a){  int n = strlen(a);  for(int k; k
a[l]){ l = j; } } int tmp = a[k]; a[k] = a[l]; a[l] = tmp; } }//the nice insertSortWayvoid SelectSort(int r[],int n) { int i ,j , min ,max, tmp; for (i=1 ;i <= n/2;i++) { min = i; max = i ; for (j= i+1; j<= n-i; j++) { if (r[j] > r[max]) { max = j ; continue ; } if (r[j]< r[min]) { min = j ; } } tmp = r[i-1]; r[i-1] = r[min]; r[min] = tmp; tmp = r[n-i]; r[n-i] = r[max]; r[max] = tmp; } }

//快速排序

 

void print(int a[], int n){      for(int j= 0; j
= privotKey) --high; //从high 所指位置向前搜索,至多到low+1 位置。将比基准元素小的交换到低端 swap(&a[low], &a[high]); while(low < high && a[low] <= privotKey ) ++low; swap(&a[low], &a[high]); } print(a,10); return low; } void qsort_improve(int r[ ],int low,int high, int k){ if( high -low > k ) { //长度大于k时递归, k为指定的数 int pivot = partition(r, low, high); // 调用的Partition算法保持不变 qsort_improve(r, low, pivot - 1,k); qsort_improve(r, pivot + 1, high,k); } } void quickSort(int r[], int n, int k){ qsort_improve(r,0,n,k);//先调用改进算法Qsort使之基本有序 //再用插入排序对基本有序序列排序 for(int i=1; i<=n;i ++){ int tmp = r[i]; int j=i-1; while(tmp < r[j]){ r[j+1]=r[j]; j=j-1; } r[j+1] = tmp; } } int main(){ int a[10] = {
3,1,5,7,2,4,9,6,10,8}; cout<<"初始值:"; print(a,10); quickSort(a,9,4); cout<<"结果:"; print(a,10); }

 

posted on
2014-11-20 22:20 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/Lxiaolong/p/4066301.html

你可能感兴趣的文章
5 -- Hibernate的基本用法 --2 2 Hibernate的数据库操作
查看>>
RAID
查看>>
Jquery.Sorttable 桌面拖拽自定义
查看>>
PSP
查看>>
身份证的最准确的正则表达式,绝对让你吃惊啊!
查看>>
14.python读写Excel
查看>>
MySQL备份类别
查看>>
JNI数据类型(转)
查看>>
mysql 主从数据同步
查看>>
ContentType的一些值
查看>>
哈希表
查看>>
Codeforces 1174C Ehab and a Special Coloring Problem
查看>>
java并发编程基础 --- 4.1线程简介
查看>>
LeetCode "Word Search"
查看>>
LintCode "Maximum Subarray Difference"
查看>>
压力测试 webbench
查看>>
创建一个简单的WCF程序
查看>>
为什么需要配置环境变量
查看>>
$i++,++$i
查看>>
Knockout学习笔记之一
查看>>