• 快速排序quicksort实现方法


     1 #include <stdlib.h>
     2 #include <stdio.h>
     3 
     4 #define ARRAY_SIZE 10
     5 
     6 void swap(int *x, int *y)
     7 {
     8     int a = *x;
     9     *x = *y;
    10     *y = a;
    11 }
    12 
    13 int randint(int low, int high)
    14 {
    15     return (low + (rand() % (high - low + 1)));
    16 }
    17 
    18 void quicksort(int *arr, int low, int high)
    19 { 
    20     int i, mid;
    21     if (low >= high) 
    22         return;
    23     swap(arr+low, arr + randint(low, high));
    24     mid = low;
    25     for (i = low+1; i <= high; i++)
    26     {
    27         if (arr[i] < arr[low])
    28         {
    29             swap(arr + ++mid, arr + i);
    30         }
    31     }
    32     swap(arr + low, arr + mid);
    33     quicksort(arr, low, mid-1);
    34     quicksort(arr, mid+1, high);
    35 }
    36 
    37 void printArray(int *arr, int size, const char *prefix)
    38 {
    39     printf("%s:", prefix);
    40     for(int i = 0; i < size - 1; i++)
    41     {
    42         printf("%d ", arr[i]);
    43     }
    44     printf("
    ");
    45 }
    46 
    47 int main(int argc, char **argv)
    48 {
    49     int array_unsort[ARRAY_SIZE] = {11, 23, 44, 55, 32, 12, 8, 9, 63, 82};
    50     printArray(array_unsort, ARRAY_SIZE, "before sort");
    51     quicksort(array_unsort, 0, ARRAY_SIZE-1);
    52     printArray(array_unsort, ARRAY_SIZE, "after sort");
    53     return 0;
    54 
    55 }
  • 相关阅读:
    Linux性能监测:磁盘IO篇
    Linux性能监测:网络篇
    Linux性能监测:内存篇
    Linux系统和性能监控之CPU篇
    linux 进程和线程讲解
    web-单机时代
    自动化-ELK日志管理
    自动化--zabbix-proxy
    自动化-zabbix(2)
    IOS中的用户安全
  • 原文地址:https://www.cnblogs.com/VincentLEcho/p/4206021.html
Copyright © 2020-2023  润新知