快排

来源:互联网 发布:网络侵权行为不包括 编辑:程序博客网 时间:2024/04/27 14:43
 
  • package net.itdos.csdn;  
  •   
  • public class QuickSort {  
  •     public static void main(String[] args) {  
  •   
  •          int[] nums = {65723345687988723576132};  
  •   
  •          sort(nums, 0, nums.length-1);  
  •   
  •          for(int i = 0; i < nums.length; i++) {  
  •   
  •            System.out.print(nums[i] + " ");  
  •   
  •          }  
  •   
  •        }  
  •   
  •   
  •        static int partition(int[] nums, int low, int hight) {  
  •   
  •            int pivot = nums[low]; //枢纽  
  •       
  •            while(low < hight) {  
  •       
  •              while(low < hight && pivot <= nums[hight]) { //从高位开始向前搜索  
  •       
  •                hight--;  
  •       
  •              }  
  •       
  •              nums[low] = nums[hight];  
  •       
  •              while(low < hight && pivot >= nums[low]) { //从低位开始向后搜索  
  •       
  •                   low++;  
  •       
  •                }  
  •       
  •                nums[hight] = nums[low];  
  •       
  •                }  
  •       
  •                nums[low] = pivot;  
  •       
  •            return low;  
  •   
  •       }  
  •   
  •       /** 
  •  
  •       *  递归 
  •  
  •       */  
  •   
  •       static void sort(int[] nums, int low, int hight) {  
  •   
  •           if(low < hight) { //跳出递归的条件, 否则会 java.lang.StackOverflowError  
  •   
  •           int pivot = partition(nums, low, hight);  
  •   
  •           sort(nums, low, pivot-1);  
  •   
  •           sort(nums, pivot + 1, hight);  
  •   
  •          }  
  •   
  •       }  
  • }  
  • 原创粉丝点击