zoukankan      html  css  js  c++  java
  • 关于快速排序的一些理解

     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 }
  • 相关阅读:
    UE4 Cel Shading(卡通渲染)
    UE4 常用数学
    锈迹材质全流程实例:Blender-》SP-》UE4
    ue4 优化建议与经验
    VisualStudio开发UE4工程设置
    Procedural Mesh Component in C++:Getting Started
    Java容器有哪些?
    java 连接mongodb
    mongodb 系列 ~ mongo 用户验证系列
    mongodb连接认证失败
  • 原文地址:https://www.cnblogs.com/luoweiKnowledge/p/3960281.html
Copyright © 2011-2022 走看看