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

    1.快速排序

    参考资料:http://developer.51cto.com/art/201403/430986.htm(下面的代码出于此处,作者写的很详细。)    及各种贴吧、文库......

    1. #include <stdio.h> 
    2. int a[101],n;//定义全局变量,这两个变量需要在子函数中使用 
    3. void quicksort(int left,int right) 
    4.     int i,j,t,temp; 
    5.     if(left>right) 
    6.        return; 
    7.                                 
    8.     temp=a[left]; //temp中存的就是基准数 
    9.     i=left; 
    10.     j=right; 
    11.     while(i!=j) 
    12.     { 
    13.                    //顺序很重要,要先从右边开始找 
    14.                    while(a[j]>=temp && i<j) 
    15.                             j--; 
    16.                    //再找右边的 
    17.                    while(a[i]<=temp && i<j) 
    18.                             i++; 
    19.                    //交换两个数在数组中的位置 
    20.                    if(i<j) 
    21.                    { 
    22.                             t=a[i]; 
    23.                             a[i]=a[j]; 
    24.                             a[j]=t; 
    25.                    } 
    26.     } 
    27.     //最终将基准数归位 
    28.     a[left]=a[i]; 
    29.     a[i]=temp; 
    30.                              
    31.     quicksort(left,i-1);//继续处理左边的,这里是一个递归的过程 
    32.     quicksort(i+1,right);//继续处理右边的 ,这里是一个递归的过程 
    33. int main() 
    34.     int i,j,t; 
    35.     //读入数据 
    36.     scanf("%d",&n); 
    37.     for(i=1;i<=n;i++) 
    38.                    scanf("%d",&a[i]); 
    39.     quicksort(1,n); //快速排序调用 
    40.                              
    41.     //输出排序后的结果 
    42.     for(i=1;i<=n;i++) 
    43.         printf("%d ",a[i]); 
    44.     getchar();getchar(); 
    45.     return 0; 
  • 相关阅读:
    网络分析(二)定向与非定向
    Flex 找不到html文件,不能自动生成html问题解决
    常用的功能点记录
    git常规操作命令整理
    语境驱动测试7原则
    探索式测试的问与答
    测试建模:Google ACC
    探索式测试:基于测程的测试管理(SessionBased Test Management)
    用Excel展示SQL Server中的数据 (III): IronPython与自动化
    在Ajax中使用Flash实现跨域数据读取
  • 原文地址:https://www.cnblogs.com/boyiliushui/p/4395444.html
Copyright © 2011-2022 走看看