快速排序(一种分治的思想)——C语言

    科技2022-07-10  103

    #include <stdio.h> #include <stdlib.h>

    /* run this program using the console pauser or add your own getch, system("pause") or input loop */

    void Quicksort(int a[], int low, int high) {     if (low >= high)         return;     int k = a[1], i = low, j = high;     while (i != j)     {         int tmp;         while ((j > i) && (a[j] > k))             j--;

            tmp = a[j];         a[j] = a[i];         a[i] = tmp;

            while ((i < j) && (a[i] < k))             i++;

            tmp = a[j];         a[j] = a[i];         a[i] = tmp;

        }     Quicksort(a, low, i - 1);     Quicksort(a, i + 1, high); }

    int main() {     int a[1001];     int n, i;     scanf_s("%d", &n);     for (i = 1; i <= n; i++)         scanf_s("%d", &a[i]);     Quicksort(a, 1, n);     for (i = 1; i <= n; i++)         printf("%d ", a[i]);     return 0; }  

    Processed: 0.010, SQL: 8