快排c++实现
2012-03-03 10:32
169 查看
// 算法.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> #include <ctime> using namespace std; void QuickSort(int e[], int first, int end); int _tmain(int argc, _TCHAR* argv[]) { srand(unsigned(time(NULL)));//set 种子 int t_nArray[10]; //产生随即数组 for(int i = 0; i < 10; i++) { t_nArray[i] = rand()%100; } //快速排序 QuickSort(t_nArray, 0, 9); //显示数组 for(int i = 0; i < 10; i++) { cout << t_nArray[i] ; cout << "," ; } system("pause"); return 0; } void QuickSort(int e[], int first, int end) { int i=first,j=end; int temp=e[first];//记录第一个数据 while(i<j) { while(i<j && e[j]>=temp) //与first数据比较,右边下标逐渐左移 j--; e[i]=e[j]; while(i<j && e[i]<=temp) //与first数据比较,左边下标逐渐右移 i++; e[j]=e[i]; } e[i]=temp; //将first数据放置于i=j处 if(first<i-1) QuickSort(e,first,i-1); if(end>i+1) QuickSort(e,i+1,end); }
相关文章推荐
- c++ 实现atoi()函数
- 用c++实现的简单的日期类
- C++多态的实现原理(转载)
- 读者写者问题的C++实现(使用boost)
- C++实现简单的链表
- 单链表翻转---C++实现
- c++实现写日志功能
- 设计模式C++实现(1)——工厂模式
- 我所理解的设计模式(C++实现)——工厂方法模式(Factory Method Pattern)
- C++实现蛇形矩阵
- C++ 用类实现point 两点的距离
- C++实现ping功能
- 菜鸟学习c++—实现学生登陆管理系统
- BinaryHeap 的C++实现
- knn之构造kd树和最近邻求取c++实现
- 排序算法的c++模板实现
- 单链表的C++实现
- 约瑟夫环程序C++循环单链表实现
- C++实现用堆求最小的k个数
- 表达式求值(c++实现)