zoukankan      html  css  js  c++  java
  • c语言描述的快速排序法

    #include<stdio.h>
    #include<stdlib.h>
    void QuikSort(int a[],int m,int n){
    //第一个数作为关键字,比他大的放到他后面,比他小的放到他前面,分为两个子序列,然后对这两个子序列分别重复这个操作
        int low=m;
        int high=n;
        int temp=a[low];
        //开始这里我直接写成了temp=a[0]导致只要第一个数比后面的都小时就失败,
        //因为递归的时候不一定high +1就等于零的特别是对于在于关键字右边的序列!
        if(m<n){
          while(low<high){
          //这里有了low<high不代表里面的low<high就没有必要,每一个循环完了后high和low都要变化的!!
            while(low<high&&temp<a[high]){
            //从高位寻找小于temp的数组项并且赋值给低位
                high--;
            }
            a[low]=a[high];
            while(low<high&&temp>a[low]){
            //从低位寻找大于temp的数组项并且赋值给高位
                low++;
            }
            a[high]=a[low];
          }
          a[low]=temp;//a[high]=temp,将关键数据赋值给分割点
          QuikSort(a,m,low-1);//递归排序低位序列
          QuikSort(a,high+1,n);//递归排序高位序列
        }else{
            return ;
        }
    }
    void main(){
    int i=0;
       
    int a[]={1,5,8,4,6};

    QuikSort(a,
    0,4); for(;i<5;i++){ printf("%d ",a[i]); } }
  • 相关阅读:
    qsort()的使用
    c语言不寻常的类型转换(类型提升)
    堆栈段的三个主要用途
    区分 声明与定义
    宏定义陷阱与typedef
    约瑟夫环解决方案
    线程中断测试
    Redis
    本地缓存
    tomcat优化
  • 原文地址:https://www.cnblogs.com/zzy-frisrtblog/p/5710038.html
Copyright © 2011-2022 走看看