zoukankan      html  css  js  c++  java
  • c语言:快速排序

    练手代码(分治实现):

    input:

    int input[] = {12,6,3,9,10,6,2};

    output:

    =======================
    len = 7
    input[0]=2
    input[1]=3
    input[2]=6
    input[3]=6
    input[4]=9
    input[5]=10
    input[6]=12

    这里强烈推荐一款web端代码编译网站,不用换个机器想写点code还必须安装编译器。

    http://codepad.org/ 

    界面如下:

    int partition(int input[],int low,int high){
        int pivot = input[low];
        while(low < high){
          while(low < high && input[high] >= pivot) high--;
          input[low] = input[high];
          while(low < high && input[low] <= pivot) low++;
          input[high] = input[low];
        }//low=high时跳出
        input[low] = pivot;
        return low;
    }
    
    int quicksort(int input[],int inplen,int low,int high){//快速排序
            int k=0;
            if(low < high){
                k = partition(input,low,high);   
                quicksort(input,inplen,low,k-1);
                quicksort(input,inplen,k+1,high);
            }
            return 0;
    }
    
    int main(){
            printf("=======================
    ");
            int input[] = {12,6,3,9,10,6,2};
            int input1[] = {2,1,3};
            int len = sizeof(input)/sizeof(int);
            printf("len = %d
    ",len);
            quicksort(input,len,0,len-1);
            int i=0;
            for(;i<len;i++){
                printf("input[%d]=%d
    ",i,input[i]);
            }
            return 0;
    }
  • 相关阅读:
    nodejs
    httpClient closeableHttpClient
    re(正则表达式)模块
    ConfigParser模块
    random和string模块
    常用文件操作模块json,pickle、shelve和XML
    time和datetime模块
    os、sys和shutil模块
    内建函数
    生成器、迭代器、装饰器
  • 原文地址:https://www.cnblogs.com/McQueen1987/p/3629695.html
Copyright © 2011-2022 走看看