/*
快速排序
*/
#include <iostream>
using namespace std;
int NewLocation(int a[],int m,int n)
{//获取中间的分隔位置
int xx = a[m];
while(m<n)
{
while(m<n && a[n]>=xx)//a[n]大于比较值,则往前移动
n--;//循环体只有这一句
int temp;
temp = a[m];
a[m] = a[n];
a[n] = temp;
while(m<n && xx>=a[m])
m++;
temp = a[m];
a[m] = a[n];
a[n] = temp;
}
return m;
}
void QuickSort(int a[],int F,int L)
{
if(F<L)
{
int x = NewLocation(a,F,L);
QuickSort(a,F,x-1);
QuickSort(a,x+1,L);
}
}
int main()
{
int M[10] = {1,2,4,9,3,7,5,0,19,12};
QuickSort(M,0,10);
for(int i =0;i<10;i++)
{
cout<<M[i]<<endl;
}
}
/*
假如需要排序的成员数量很多,但是成员的值有很多是雷同的
比如说,我们需要统计一个公司各个年龄的人,并按年龄大小进行排序
我们可以考虑下面的排序算法(统计各个年龄段的人数)
*/
void AnotherArray(int arr[],int length)
{
while(arr = null||length<0)
return;
const int Max_Year = 120;//假定最大的年龄为120岁
int ages[Max_Year+1];///用来储存各个年龄的人数
for(int i =0;i<=Max_Year;i++)
{
ages[i] = 0;
}
for(int i =0;i<length;i++)
{
if(arr[i]<0||arr[i]>Max_Year)
throw exception("out of range");
int x= arr[i];
ages[x]++;
}
}
/*
选择排序:选择最小的与第一个数交换
再从剩下的选择最小的与第二个交换
*/
void SelectOrder (int *Data,int count)
{
int Samlll_Data;
for(int i =0;i<count;i++)
{
Samlll_Data = Data[i];
int Index = i;
for(int j = i+1;j<count;j++)
{
if(Data[j]<Samlll_Data)
{
Samlll_Data = Data[j];
Index = j;
}
}
int temp = Data[j];
Data[j] = Data[i];
Data[i] = temp;
}
for(int i =0;i<count;i++)
{
cout<<Data[i]<<endl;
}
}