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)——快速排序
正如上一篇博文所说,今天我们来讨论一下所谓的"高级排序"--快速排序.首先声明,快速排序是一个典型而又"简单"的分治的递归算法. 递归的威力我们在介绍插入排序时 ...
随机推荐
- $.get的重写
window.meng = window.meng || {}; (function () { function Get() { this.def = $.Deferred(); } Get.prot ...
- License Manager 10.3启动失败解决方法
参考:http://jingyan.baidu.com/article/c275f6bac8763ae33d756788.html 安装ARCGIS10.3过程中无法启动license. 解决办法: ...
- Mybatis 实用
1.<delete id="removeZtreeS" parameterType="String"> DELETE FROM sys_fun WH ...
- UB单修改
FUNCTION Z_SD_UB_CHANGE. *"-------------------------------------------------------------------- ...
- Java网络通信
一.基本概念 1.网络程序: 能够接受另一台计算机发送过来的数据或者能够向另一台计算机发送数据的程序叫做网络程序. 2.IP 能够在网络中唯一标示一台主机的编号就是IP 3.端口号 16位数字表示 4 ...
- Scout YYF I(POJ 3744)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5565 Accepted: 1553 Descr ...
- VS复习 -- if···else和if···else嵌套语句
注意:理清逻辑,画出逻辑分支图,理清思路 1.if语句 2.if...else语句 3.if..else if...else static void Main(string[] args) { Con ...
- Android: Intent实现活动之间的交互
Intent的作用:是Android中各个组件直接交互的一种重要方式,且利用Intent可以启动Activity.Service以及Broadcast Receiver. Intent的创建:显示和隐 ...
- 静态工具类中使用注解注入service
转载:http://blog.csdn.net/p793049488/article/details/37819121 一般需要在一个工具类中使用@Autowired 注解注入一个service.但是 ...
- JDE隐藏Constant等(Hide Object)
Grid中隐藏列,列值可以使用属性配置,但是列表头Constant需要用函数修改,如下所示: