快速排序

代码

#include <stdio.h>

void printList(int iList[], int iLen)
{
int i = ;
for(i = ; i < iLen; i++)
{
printf("%d ", iList[i]);
}
printf("\n");
} void printList(int iList[], int iBegin, int iEnd)
{
int i = ;
for(i = ; i < iBegin; i++)
{
printf("%c ", '_');
}
for(i = iBegin; i < iEnd; i++)
{
printf("%d ", iList[i]);
}
printf("\n");
} int _quickSort(int iList[], int iLeft, int iRight)
{
if(iLeft >= iRight)
{
return ;
}
int i = iLeft, j = iRight;
int t = iList[i];
printf("%d<->%d mid(%d) : ", i, j, t);
while(i < j)
{
while(t < iList[j] && j > i)
{
j--;
}
if(j > i)
{
iList[i] = iList[j];
i++;
} while(t >= iList[i] && i < j)
{
i++;
}
if(i < j)
{
iList[j] = iList[i];
j--;
}
}
iList[i] = t;
printList(iList, );
_quickSort(iList, iLeft, i - );
_quickSort(iList, i + , iRight); return ;
} int Quick(int iList[], int iLen)
{
_quickSort(iList, , iLen - );
return ;
} int main()
{
int iNum = ;
int iList[] = {, , , , , , , , , };
printf("src : ");
printList(iList, iNum);
putchar('\n');
Quick(iList, iNum);
putchar('\n');
printf("dst : ");
printList(iList, iNum); return ;
}

编译

$ g++ -g -o quickSort quickSort.cpp

运行

$ ./quickSort
src : <-> mid() :
<-> mid() :
<-> mid() :
<-> mid() :
<-> mid() :
<-> mid() : dst :

再见……

纪念逝去的岁月——C/C++快速排序的更多相关文章

  1. 纪念逝去的岁月——C++实现一个队列(使用类模板)

    1.代码 2.运行结果 1.代码 #include <stdio.h> #include <string.h> template <typename T> clas ...

  2. 纪念逝去的岁月——C++实现一个栈(使用类模板)

    这个版本是上个版本的加强版,上个版本的代码:http://www.cnblogs.com/fengbohello/p/4542912.html 目录 1.代码 2.运行结果 1.代码 1.1 调试信息 ...

  3. 纪念逝去的岁月——C++实现一个栈

    1.代码 2.运行结果 1.代码 stack.cpp #include <stdio.h> #include <string.h> class ClsStack { priva ...

  4. 纪念逝去的岁月——C/C++排序二叉树

    1.代码 2.运行结果 3.分析 1.代码 #include <stdio.h> #include <stdlib.h> typedef struct _Node { int ...

  5. 纪念逝去的岁月——C/C++二分查找

    代码 #include <stdio.h> int binarySearch(int iList[], int iNum, int iX, int * pPos) { if(NULL == ...

  6. 纪念逝去的岁月——C/C++交换排序

    交换排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...

  7. 纪念逝去的岁月——C/C++选择排序

    选择排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...

  8. 纪念逝去的岁月——C/C++冒泡排序

    冒泡排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...

  9. 纪念逝去的岁月——C/C++字符串回文

    判断字符串是否是回文: 1. 输入:hello world dlrow olleh 输出:1 2. 输入:nihao hello 输出:0 代码 #include <stdio.h> #i ...

随机推荐

  1. OS X thrift setup

    OS X Setup The following command install all the required tools and libraries to build and install t ...

  2. 手机访问 localhost

    为了测试开发的手机网站,常常需要使手机直接访问本地网络.在这个过程中碰到几个问题,记下来供以后参考 1. 在本地主机运行apache后,使用localhost和127.0.0.1可以访问页面,但使用I ...

  3. JavaScript中call,apply和prototype

    [TOC] call()方法 语法:call([thisObj[,arg1[, arg2[, [,.argN]]]]]) 定义:调用一个对象的一个方法,以另一个对象替换当前对象. 如果没有提供 thi ...

  4. android倒计时(整理)

    android倒计时 用到CountDownTimer Android中文API(143) —— CountDownTimer 前言 本章内容android.os.CountDownTime章节,版本 ...

  5. 有关servlet初学者的资源和建议

    四天来学习servlet是很痛苦的经历,其实可以不必要这么痛苦,关键是一定要学会冷静的分析问题与解决问题,要不言学习也没有那么多的乐趣.初学java刚满15天. 首先对于资源来说建议先读一点点的PPT ...

  6. 制作caffe中的test.txt和val.txt

    find -name *.jpeg |cut -d '/' -f2-3> train.txt(图片在当前文件夹) find train/dog -name *.JPEG |cut -d '/' ...

  7. H5移动前端性能优化

    在移动端,因手机的配置和3/4G网络的原因,从两个方面解决性能优化问题,1.加载不超过3秒,用loading或者资源不要超过1M.2.渲染速度. 基于以上两个方面,所有影响首屏加载和渲染的代码应在处理 ...

  8. Android拓展系列(9)--Android视频录制screenrecord命令

    在Android4.4 Kitkat上集成了一个比较好用的视频录制功能.参考:http://forums.androidcentral.com/android-4-4-kitkat/329674-ho ...

  9. PHP 一个表单多个提交按钮,处理不同的业务逻辑

    <?phpini_set("error_reporting","E_ALL & ~E_NOTICE");?> <head>< ...

  10. js实现冒泡排序

    冒泡排序 冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法. 它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到 ...