zoukankan      html  css  js  c++  java
  • 1209.1——快速排序算法

    #include <stdio.h>

    void quickSort(int array[], int low, int high){

        int i = low; //从左到右

        int j = high; //从右边到左

        

        //保存参考值 第一个数作为参考值

        int temp = array[low];

        

        if (low < high) {

            while (i < j) {

                //先从最右边开始找到第一个比temp小的数

                while (i < j && array[j] >= temp) {

                    j--;

                }

                

                //找到第一个比temp小得数了。

                //交换

                array[i] = array[j];

                

                //从左边开始,找到第一个比temp大得数

                while (i < j && array[i] <= temp) {

                    i++;

                }

                

                //找到第一个比 temp大得数了。

                //交换

                array[j] = array[i];

            }

            

            //找到临界点的位置

            array[i] = temp;

            

            //以i为基准,左边的比temp小, 右边的比temp大

            //对左边进行排序

            quickSort(array, 0, i-1);

            

            //对右边进行排序

            quickSort(array, i+1, high);

        }

    }

    int main(int argc, const char * argv[]) {

        int array[] = {3,1,9,2,8,3,7,4};

        

        quickSort(array, 0, 7);

        

        for (int i = 0; i < 8; i++) {

            printf("%d ", array[i]);

        }

        printf(" ");

        return 0;

    }

  • 相关阅读:
    pands数据框(DataFrame)02
    mysql 临时表
    【转】Mysql 多表连接查询的执行细节 (一)
    【转】cuckoo hash
    [转]全域哈希
    【转】 bloom filter
    【转】bitmap
    golang 反汇编代码阅读-- defer
    assignment3
    Lecture 12: Visualizing and Understanding
  • 原文地址:https://www.cnblogs.com/damonWq/p/5033031.html
Copyright © 2011-2022 走看看