• 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;
    }
  • 相关阅读:
    day6心得
    安卓下拉刷新和上拉加载的具体实例
    安卓下拉刷新空间SwipeRefreshLayout的基本使用
    fresco的使用教程
    封装安卓的okhttp
    handler更新ui线程的基本用法
    安卓recyclerview的基本使用
    安卓轮播组件
    自定义toolbar教程
    原生toolbar基本使用教程
  • 原文地址:https://www.cnblogs.com/McQueen1987/p/3629695.html
Copyright © 2020-2023  润新知