您的位置:首页 > 产品设计 > UI/UE

排序_Quick_Sort(快速排序)

2012-11-09 17:50 423 查看
快速排序的主要的思想就是在于划分,就是先找一个关键值,然后运用什么样的方法把数据分成小于关键值的在关键的左边,

而大于关键值的在关键值的右边. 然后分配好了之后就可以直接递归调用了.

贴出我自己写的代码,这是根据数据结构的基本思想写出来的:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int N;

int a[10000];

void Quick_Sort (int low, int high)
{
if (low < high) //这个注意一定要写上,因为如果没有这个条件的话,将会出现无限循环的情况
{
int i = low; // 这是两个动态的指针
int j = high;
int pivot = a[low]; // 将这个关键之存到pivot里面
int pos = low; //这个指针是指向空缺的可以插入的位置上
while (i < j)
{
while (a[j] > pivot)
{
j--;
}
a[pos] = a[j];
a[j] = pivot;
pos = j;
while (a[i] < pivot)
{
i++;
}
a[pos] = a[i];
a[i] = pivot;
pos = i;
}
Quick_Sort (low, pos - 1); // 这是递归调用算法
Quick_Sort (pos + 1, high);
}
}

int main()
{
cout << "Input the number that you want be sorted : " << endl;
cin >> N;
cout << "Please input the numbers : " << endl;
for (int i = 0; i < N; i++)
{
cin >> a[i];
}
Quick_Sort (0, N - 1);
for (int i = 0; i < N; i++)
{
cout << a[i] << " ";
}
cout << endl;
system ("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: