zoukankan      html  css  js  c++  java
  • 快速排序*^____^*

    package Test01;
    
    public class QuickSprt {
    
    	public static void main(String[] args) {
    		int[] arr = { 1, 3, 2, 9, 8, 7, 1, 0,0,2,3,3,1,0,101,100,1,1,001,100 }; // 要排序的数组
    	
    
    		QuickSort(arr);
    		for (int i : arr) {
    			System.out.print(i+"  ");
    		}
    	}
    
    	public static void QuickSort(int[] arr) {
    		if (arr == null || arr.length < 2) {
    			return;
    		} else {
    			quickSort(arr, 0, arr.length - 1);
    		}
    
    	}
    
    	public static void quickSort(int[] arr, int L, int R) {
    		if (L < R) {
    			int[] p = partition(arr, L, R);
    			quickSort(arr, L, p[0] );
    			quickSort(arr, p[1] , R);
    		
    		}
    	}
    
    	public static int[] partition(int[] arr, int L, int R) {
    		int num = arr[(int) (Math .random()*(R-L+1)+L)];  //随机快排
    		int less = L - 1;
    		int more = R + 1;
    		while (L < more) {
    			if (arr[L] < num) {
    				swap(arr, L++, ++less);
    
    			} else if (arr[L] > num) {
    				swap(arr, L, --more);
    
    			} else {
    				L++;
    			}
    		}
    		return new int[] { less, more };
    
    	}
    	
    	public static void swap(int arr[], int a, int b) {
    		int temp = arr[a];
    		arr[a] = arr[b];
    		arr[b] = temp;
    
    	}
    }
    
  • 相关阅读:
    Ajax实现表格实时编辑
    自定义简单分页
    有趣的 0
    关于AJAX的一些事
    JQ中的FormData对象 ajax上传文件
    订单导出
    javaScript事件委托
    javascript递归函数
    详解javascript中this的工作原理
    详解JavaScript对象继承方式
  • 原文地址:https://www.cnblogs.com/superfly123/p/10530120.html
Copyright © 2011-2022 走看看