快速排序

来源:互联网 发布:mcs51单片机 编辑:程序博客网 时间:2024/06/05 00:09
#include<stdio.h>#include<stdlib.h>#include<time.h>#define N 20static void show(int *p, int length){for (int i = 0; i < length; i++){printf("%3d", p[i]);}printf("\n");}void swap(int *p1, int *p2){int temp = *p1;*p1 = *p2;*p2 = temp;}void qsort(int *p, int left, int right){int i = left;int j = right;if (i < j){while (i < j){while (p[i] <= p[left] && i <= right){i++;}while (p[j] >= p[left] && j>left){j--;}if (i < j){swap(&p[i], &p[j]);}show(p, N);}swap(&p[left], &p[j]);qsort(p, left, j-1);qsort(p, j + 1, right);}}int main(){time_t st;unsigned int data = time(&st);srand(data);int a[N];for (int i = 0; i < N; i++){a[i] = rand() % 100;}show(a, N);qsort(a, 0, N - 1);show(a, N);return 0;}

0 0
原创粉丝点击