您的位置:首页 > 编程语言 > C语言/C++

c++快速排序

2016-01-23 15:49 239 查看
快速排序(n个数)
从数中随便选取一个数a,(一般选取第一个数),将a与其余的数字s进行比较,若a小于s,则交换。n-1轮比较完成后。
另选一个数a,与其余n-2数分别比较,(交换),如此重复,
进行N-1轮比较。

// kspx2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
using namespace std;

void kspx(int a[10]){
int temp;
for(int i=0;i<9;i++){
for(int j=i+1;j<9;j++){
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}

int _tmain(int argc, _TCHAR* argv[])
{
int a[10]={4,5,6,2,1,3,0,7,9,8};
cout<<"before:"<<endl;
for(int i=0;i<10;i++){
cout<<a[i]<<"  ";
}

kspx(a);
cout<<endl<<"after:"<<endl;
for(int i=0;i<10;i++){
cout<<a[i]<<"  ";
}
cout<<endl;
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: