• 关于快速排序的一些理解


     1 public class QuickSort {
     2     
     3     public  void sort(int[] k,int low,int high)
     4     {
     5         int point;
     6         if(low<high)   //这里要主要不要写成了while
     7         {
     8             point=Partition(k,low,high); //获取中间点并把数组一分为二
     9             sort(k,low,point-1);         //对小于Point的左边数组进行递归
    10             sort(k,point+1,high);        //对大于Point的右边数组进行递归
    11         }
    12     }
    13     /*
    14      * 
    15      * 该函数实现获取中间轴点的功能*/
    16     public  int Partition(int[] k,int low,int high)
    17     {
    18         int point;
    19         point=k[low];//以数组的第一个值作为中轴
    20         while(low<high)
    21         {
    22             while(low<high &&k[high]>=point)  //当右边的值比中轴值大时,不进行交换,右边的数组向左移动一位
    23             {
    24                 high--;
    25             }
    26             k[low]=k[high];//当右边的值比当前值小,进行交换
    27             while(low<high &&k[low]<=point)
    28             {
    29                 low++;
    30             }
    31             k[high]=k[low];//当左边的值比当前值大,进行交换
    32             
    33         }
    34         k[low]=point; //把中间值放到数组的中间位置
    35         return low;  //返回中间值的位置
    36     }
    37 
    38     public static void main(String[] args) {
    39         // TODO Auto-generated method stub
    40         QuickSort sort = new QuickSort();
    41         int[] A ={5,7,2,9,8,6,1,3,4,0};
    42         System.out.println("排序前的顺序:");
    43         for (int i : A) {
    44             System.out.print(i+" ");
    45         }
    46         sort.sort(A,0,A.length-1);
    47         System.out.println();
    48         System.out.println("使用快速排序后的顺序为");
    49         for (int i : A) {
    50             System.out.print(i+" ");
    51         }
    52         
    53         
    54     }
    55 
    56 }
  • 相关阅读:
    下载MySQL数据库
    2012开源项目计划-WPF企业级应用整合平台
    WPF入门教程系列(二) 深入剖析WPF Binding的使用方法
    WPF入门教程系列(一) 创建你的第一个WPF项目
    一、什么是WPF?
    asp.net页面间传值的几种方法
    .NET 代码编译过程
    全面认识.NET框架(一)
    C#里partial关键字的作用(转摘)
    .NET概念:.NET程序编译和运行
  • 原文地址:https://www.cnblogs.com/luoweiKnowledge/p/3960281.html
Copyright © 2020-2023  润新知