复习快速排序,用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语言快速排序的更多相关文章

  1. c语言 快速排序---归并排序----堆排序

    //快速排序: #include <stdio.h> #define MAX 500000 int s[MAX]; void Q_Sort(int start,int end) { int ...

  2. c语言 快速排序

    #include<stdio.h> #include<stdlib.h> #define BUF_SIZE 10 void display(int array[], int m ...

  3. c语言快速排序算法(转)

    原文链接http://blog.csdn.net/morewindows/article/details/6684558 快速排序由于排序效率在同为O(N*logN)的几种排序方法中效率较高,因此经常 ...

  4. C语言“快速排序”函数写法

    代码是:C语言中快速排的写法,要加入头文件   <stdlib.h> qsort(数组名, 长度, 数据类型大小,比较算子 ): #include <stdio.h> #inc ...

  5. C语言快速排序函数------qsort();

    #include<stdio.h> #include<stdlib.h> #include<string.h> #include<conio.h> ty ...

  6. C语言排序

    排序算法 快速排序 C语言快速排序qsort(数组,长度,元素大小,cmp函数(*,*))//注意函数cmp的参数为指针 #include <stdio.h> #include <s ...

  7. C++程序设计(一)

    1. 函数指针 程序运行期间,每个函数都会占用一段连续的内存空间.而函数名就是该函数所占内存区域的起始地址(也称"入口地址").我们可以将函数的入口地址赋给一个指针变量,使该指针变 ...

  8. 快速排序(Quick Sort)的C语言实现

    快速排序(Quick Sort)的基本思想是通过一趟排序将待排记录分割成独立的两部分,其中一部分记录的关键字均比另一部分记录的关键字小,则可分别对着两部分记录继续进行排序,以达到整个序列有序,具体步骤 ...

  9. 深入浅出数据结构C语言版(20)——快速排序

    正如上一篇博文所说,今天我们来讨论一下所谓的"高级排序"--快速排序.首先声明,快速排序是一个典型而又"简单"的分治的递归算法. 递归的威力我们在介绍插入排序时 ...

随机推荐

  1. discuz核心类库class_core的函数注释

    class discuz_core { // 数据库存储引擎 var $db = null; // 内存缓冲object var $mem = null; // 会话 object var $sess ...

  2. 冲销交货单WS_REVERSE_GOODS_ISSUE

    LOOP AT ITAB. AT END OF VBELN. PERFORM FRM_LOCK_DELIVERY(ZSDS0002) USING ITAB-VBELN. CALL FUNCTION ' ...

  3. Objective-C:Foundation框架-常用类-NSDictionary

    与NSString.NSArray一样,NSDictionary是不可变的,其对应可变类型为NSMutableDictionary.其用法如下: #import <Foundation/Foun ...

  4. 关于for循环条件性能问题

    昨天看一博客写到一条 尽量使用 for(int i=0,ct=list.Count();i<ct;i++){} 这样的格式,因为我平时一般都是用for(int i=0;i<list.Cou ...

  5. uva 10723

      10723 - Cyborg Genes Time limit: 3.000 seconds Problem F Cyborg Genes Time Limit 1 Second Septembe ...

  6. Javascript 严格模式详解(转)

    一.概述 除了正常运行模式,ECMAscript 5添加了第二种运行模式:"严格模式"(strict mode).顾名思义,这种模式使得Javascript在更严格的条件下运行. ...

  7. vue.js——初体验

    看到最近很火的vue.js,于是开启了自己人生中首篇翻译之路,才意识到这个纯英文版的的确没有中文的通俗易懂~~~~~~不过, 还是硬着头皮把这篇英文版的博客给翻译完了,希望可以帮助自己的同时也方便别人 ...

  8. CentOS查看软件源提供的软件版本命令

    yum list available第二列包更详细的说明:yum info packagename

  9. UVa 11427 - Expect the Expected

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  10. (BFS)hdoj2377-Bus Pass

    题目地址 因为最后要看的是到所有路线上的区域最大距离最小的中心点,所以可以采取遍历路线上所有的区域,对每个区域进行BFS的办法.为了更方便的在每一次BFS都遍历所有的区域,可以加一个reach数组,记 ...