zoukankan      html  css  js  c++  java
  • 快速排序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 }
  • 相关阅读:
    js 兼容nextSibling
    ie background repeat 出现空白
    自制日历组件
    js cookie操作方法
    html table 上下左右边框
    js window.onload函数
    js 兼容event.target
    ie minheight
    css table 固定宽度
    [翻译]建立你的第一个Visual WebGui应用程序
  • 原文地址:https://www.cnblogs.com/VincentLEcho/p/4206021.html
Copyright © 2011-2022 走看看