排序【快速排序代码示例】

排序

Time Limit: 1000ms   Memory limit: 32678K  有疑问?点这里^_^

题目描述

题目链接:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1582

    给你N(N<=100)个数,请你按照从小到大的顺序输出。

输入

    输入数据第一行是一个正整数N,第二行有N个整数。

输出

    输出一行,从小到大输出这N个数,中间用空格隔开。

示例输入

5
1 4 3 2 5

示例输出

1 2 3 4 5

提示

代码:

 1 #include<iostream>
 2 #include<string.h>
 3 using namespace std;
 4 int f[100];
 5 void Qsort(int f[],int low,int heigh);
 6 int OneQsort(int f[],int s,int t);
 7 int main()
 8 {
 9     int n;
10     cin>>n;
11     int i;
12     for(i=1;i<=n;i++)
13         cin>>f[i];
14     Qsort(f,1,n);
15     for(i=1;i<=n;i++)
16         {
17             if(i==1)
18             cout<<f[i];
19             else cout<<" "<<f[i];
20         }
21     cout<<endl;
22     return 0;
23 }
24 void Qsort(int f[],int s,int t)
25 {
26     int mid;
27     if(s<t)
28     {
29         mid=OneQsort(f,s,t);
30         Qsort(f,s,mid-1);
31         Qsort(f,mid+1,t);
32     }
33 }
34 int OneQsort(int f[],int low,int height)
35 {
36     int key=f[low];
37     while(low<height)
38     {
39         if(f[height]>=key&&low<height)
40             height--;
41         f[low]=f[height];
42         if(f[low]<=key&&low<height)
43             low++;
44         f[height]=f[low];
45     }
46     f[low]=key;
47     return low;
48 }
View Code
原文地址:https://www.cnblogs.com/kuangdaoyizhimei/p/3485416.html