排序_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;
}
而大于关键值的在关键值的右边. 然后分配好了之后就可以直接递归调用了.
贴出我自己写的代码,这是根据数据结构的基本思想写出来的:
#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;
}
相关文章推荐
- 排序——快速排序(quick_sort)
- Algorithm: quick sort implemented in python 算法导论 快速排序
- 数据排序谁最快(javascript中的Array.prototype.sort PK 快速排序)
- 算法导论——快速排序(Quick-Sort)
- c语言数组五种排序法:bubble,choise,quick,insert,shell 和js 数组排序sort的区别
- 【数据结构】 MergeSort与QuickSort的详细分析 - 归并排序、快速排序
- 【数据结构笔记】快速排序(quick_sort)和快速选择(quick_select)--Python2.7
- 数据排序谁最快(javascript中的Array.prototype.sort PK 快速排序)
- quick_sort(快速排序)c语言实现
- C标准库中的快速排序(quick-sort)函数 [简单应用]
- 迭代的快速排序(Iterative Quick Sort)
- 快速排序(Quick-Sort)
- 快速排序(Quick_Sort)
- 快速排序(quick_sort)
- 一天一排序之“快速排序(quic_sort)”
- java quick sort(快速排序)
- 排序1+3:基数排序(RadixSort),希尔排序(ShellSort)和快速排序(QuickSort)
- 经典排序算法 - 快速排序Quick_sort
- 笔试算法题(54):快速排序实现之单向扫描、双向扫描(single-direction scanning, bidirectional scanning of Quick Sort)
- 快速排序的简单实现(Quick_Sort)