C语言快速排序
复习快速排序,用C语言实现:
#include <stdio.h> int quicksort(int begin, int end, int a[], int len); void main()
{
int a[] = {, , , , , , , };
int len = sizeof(a)/sizeof(int);
int i=;
int j=len-;
int pivot;
pivot = quicksort(i, j, a, len);
//printf("\npivot:%d\n", pivot);
//for(i=0; i<len; i++)
//{
// printf("%d ", a[i]);
//}
} int quicksort(int begin, int end, int a[], int len)
{
int tmp = ;
for(tmp=begin; tmp<=end; tmp++)
{
printf("%d ", a[tmp]);
}
printf("\n"); int i=begin;
int j = end;
int key = a[i];
//simple quick sort
while(i<j)
{
while(a[j]>key && j>= && i<j)
{
j--;
}
if(j>= && i<j)
{
a[i] = a[j];
} while(a[i]<key && i<len && i<j)
{
i++;
}
if(i<len && i<j)
{
a[j] = a[i];
} }
a[i] = key; int pivot = i; if(pivot > begin)
quicksort(begin, pivot-, a, pivot-begin); if(pivot < end)
quicksort(pivot+, end, a, end-pivot); return i;
}
运行结果为:
33 22 6 5 7 3 8 9
9 22 6 5 7 3 8
8 3 6 5 7
7 3 6 5
5 3 6
3
6
22
说明递归调用quicksort方法的次数为8次。
C语言快速排序的更多相关文章
- c语言 快速排序---归并排序----堆排序
//快速排序: #include <stdio.h> #define MAX 500000 int s[MAX]; void Q_Sort(int start,int end) { int ...
- c语言 快速排序
#include<stdio.h> #include<stdlib.h> #define BUF_SIZE 10 void display(int array[], int m ...
- c语言快速排序算法(转)
原文链接http://blog.csdn.net/morewindows/article/details/6684558 快速排序由于排序效率在同为O(N*logN)的几种排序方法中效率较高,因此经常 ...
- C语言“快速排序”函数写法
代码是:C语言中快速排的写法,要加入头文件 <stdlib.h> qsort(数组名, 长度, 数据类型大小,比较算子 ): #include <stdio.h> #inc ...
- C语言快速排序函数------qsort();
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<conio.h> ty ...
- C语言排序
排序算法 快速排序 C语言快速排序qsort(数组,长度,元素大小,cmp函数(*,*))//注意函数cmp的参数为指针 #include <stdio.h> #include <s ...
- C++程序设计(一)
1. 函数指针 程序运行期间,每个函数都会占用一段连续的内存空间.而函数名就是该函数所占内存区域的起始地址(也称"入口地址").我们可以将函数的入口地址赋给一个指针变量,使该指针变 ...
- 快速排序(Quick Sort)的C语言实现
快速排序(Quick Sort)的基本思想是通过一趟排序将待排记录分割成独立的两部分,其中一部分记录的关键字均比另一部分记录的关键字小,则可分别对着两部分记录继续进行排序,以达到整个序列有序,具体步骤 ...
- 深入浅出数据结构C语言版(20)——快速排序
正如上一篇博文所说,今天我们来讨论一下所谓的"高级排序"--快速排序.首先声明,快速排序是一个典型而又"简单"的分治的递归算法. 递归的威力我们在介绍插入排序时 ...
随机推荐
- 【软件使用】TortoiseSVN版本管理软件使用简单说明
TortoiseSVN版本管理软件使用简单说明 很多时候在写一个小的项目不想使用github等工具,只想简单在本地搭建一个版本管理器.那么TortoiseSVN就非常适合. 第一步:下载Tortois ...
- sql server 导出数据到 Azure Hbase / Hive 详细步骤
The Hadoop on Azure Sqoop Import Sample Tutorial Table of Contents Overview Goals Key technologi ...
- 在csdn里markdown感受
先来一个百度百科 Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式. Markdown具有一系列衍生版本,用于扩展Markdo ...
- Subscribe的第四个参数用法
看别人的代码真的是很好的学习过程啊 之前用Subscribe订阅的时候都是简单的用法形如: ros::Subscriber sub = node.subscribe<uhf_rfid_api:: ...
- js首字母大写--单个单词的处理方式
var operate2='OR'; for (var j = 0, len = operate1.length; j< len; j++) { //获得unicode码 var ch2 = o ...
- 【转】Nginx+php-fpm+MySQL分离部署详解
转:http://www.linuxidc.com/Linux/2015-07/120580.htm Nginx+php-fpm+MySQL分离部署详解 [日期:2015-07-26] 来源:Linu ...
- Windows Store App 应用程序安装目录
前面介绍了如何对本地应用存储空间中的文件以及文件夹进行操作,在应用中除了可以对本地应用存储空间进行操作之外,还可以对应用程序安装目录进行相关操作.本节将通过一个示例,详细讲解如何对应用程序安装目录中的 ...
- Linux Mysql 1130错误解决
今天在win32下通过navicat 远程登录Mysql时出现如下错误: 想都不用想,肯定是Mysql的访问权限问题. 首先,通过终端(我用的是SSH)远程登录到Linux服务器,为了 ...
- jQuery实现图片延迟加载
html: <img src ="占位图路径" data-original="真实图片路径" /> js: $("img").l ...
- [Jquery]导航菜单效果-纵向
$( document ).ready( function(e){ var $catCont = $( ".cat-cont" ); //二级菜单div var $ca ...