快速排序记录
来源:互联网 发布:matlab矩阵的逆 编辑:程序博客网 时间:2024/06/14 00:29
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <stack>
using namespace std;
static ncount = 0;
//非递归普通快速
void exchange(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
void prints(int a[], int len)
{
for (int i = 0; i < len; ++i)
{
printf("%d ", a[i]);
}
printf("\n");
}
int Partition(int a[], int low, int high)
{
int privot = a[low];
printf("a[low] = %d \n", a[low]);
while (low < high)
{
while ( low < high && a[high] >= privot)
{
high--;
ncount++;
}
if (low < high)
{
exchange(a[low++], a[high]);
prints(a, 8);
}
while (low < high && a[low] <= privot)
{
low++;
ncount++;
}
if (low < high)
{
exchange(a[low], a[high--]);
prints(a, 8);
}
ncount++;
}
// a[low] = privot;
printf("a[low] = %d \n", a[low]);
return low;
}
void sort(int a[], int low, int high)
{
int pivolt;
if (low < high)
{
pivolt = Partition(a, low, high);
sort(a, low, pivolt-1);
sort(a, pivolt + 1, high);
}
}
int main(void)
{
int a[] = {13, 27, 55, 63, 7, 8, 15, 16, 12, 1, 1 ,1 ,1 ,1 ,1};
sort(a, 0, sizeof(a)/sizeof(int) - 1);
for (int i = 0; i < sizeof(a)/sizeof(int); ++i )
{
printf("%d ", a[i]);
}
printf("\n");
printf("%d\n", ncount);
return 1;
}
//平衡快速,适用于催向顺序的排列
void QuickSort(int *pData,int left,int right)
{
int i(left),j(right),middle(0),iTemp(0);
middle=pData[(left+right)/2];//求中间值
middle=pData[(rand()%(right-left+1))+left]; //生成大于等于left小于等于right的随机数
do{
while((pData[i]<middle)&&(i<right))//从左扫描大于中值的数
{
i++;
ncount++;
}
while((pData[j]>middle) && (j>left))//从右扫描小于中值的数
{
j--; //找到了一对值,交换
ncount++;
}
if(i<=j){
iTemp=pData[j];
pData[j]=pData[i];
pData[i]=iTemp;
i++;
j--;
}
ncount++;
}while(i<=j);//如果两边扫描的下标交错,就停止(完成一次) //当左边部分有值(left<j),递归左半边
if(left<j){
QuickSort(pData,left,j);
} //当右边部分有值(right>i),递归右半边
if(right>i){
QuickSort(pData,i,right);
}
}
//非递归版快速排序
struct Num
{
int low,high;
Num(int low = 0, int high = 0)
{
this->low = low;
this->high = high;
}
};
void sort(int val[],int ,int );
int main()
{
int arg[] = {13, 27, 55, 63, 7, 8, 15, 16, 12, 1, 1 ,1 ,1 ,1 ,1};
sort(arg,0,sizeof(arg)/sizeof(int)-1);
for(int i = 0; i < sizeof(arg)/sizeof(int); i++)
{
cout<<arg[i]<<" ";
}
system("pause");
return 0;
}
void sort(int arr[], int begin, int end)
{
std::stack<Num> myStack;
myStack.push(Num(begin, end));
while(!myStack.empty())
{
int i = myStack.top().low;
int j = myStack.top().high;
int b = i;
int e = j;
myStack.pop();
if(i >= j)
continue;
int key = arr[i];
while(i < j)
{
while(i < j && arr[j] >= key)
j--;
if(i < j)
arr[i++] = arr[j];
while(i < j && arr[i] <= key)
i++;
if(i < j)
arr[j--] = arr[i];
}
arr[i] = key;
printf("%d\n", i);
myStack.push(Num(b, i - 1));
myStack.push(Num(i + 1, e));
}
}
- 快速排序记录
- 记录一笔-快速排序
- 算法记录:二进制快速排序
- JS快速排序--记录点滴
- 带时间记录的快速排序&随机快速排序
- java写的快速排序 记录下。。
- 记录代码——快速排序
- Java 实现快速排序算法-学习记录
- 关于快速排序算法的问题记录
- 记录下三种排序,冒泡,选择,和快速。
- 记录自已学习之排序算法(快速排序)
- 快速排序(QuickSort)学习记录&个人理解
- c语言快排(快速排序)poj2388《学习记录》
- 快速排序
- 快速排序
- 快速排序
- 快速排序!
- 快速排序
- dedecms如何提高网页打开速度
- Oracle 有关 Consistent gets 的测试
- 邂逅在华灯初上
- 瑞晟笔试题
- java 项目svn地址
- 快速排序记录
- 给盒子的贺文
- 使用sendfile提升性能
- ADB 命令
- Android 实用工具Hierarchy Viewer实战
- 面试驱动开发人员的感悟
- 程序员成长的六个阶段,咱们又是哪个阶段呢?
- 暑期9_15队内练习赛 B题 大数取模+矩阵快速幂
- 使用 Minidumps 和 Visual Studio .NET 进行崩溃后调试