quicksort

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "stdio.h"

void quicksort(int a[], int left, int right)
{
int i, j, t, temp;
if(left > right)
return;

temp = a[left];
i = left;
j = right;
while(i != j)
{
while(a[j] >= temp && i < j)
j --;
while(a[i] <= temp && i < j)
i ++;
if(i < j)
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
a[left] = a[i];
a[i] = temp;

quicksort(a, left, i-1);
quicksort(a, i+1, right);
}

int main(){
int i, j, t;
int n = 5;
scanf("%d", &n);
int a[n];
for(i = 1;i <= n; i++)
scanf("%d", &a[i]);

quicksort(a, 1, n);

for(i=1; i <= n; i++)
printf("%d ", a[i]);
putchar('\n');
return 0;
}

运行结果

-------------本文结束感谢您的阅读-------------
请站长喝杯咖啡吧´◡`
  • 本文作者: Canvas/套陆
  • 本文链接: https://blog.iamzhl.top/quicksort.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!