zoukankan      html  css  js  c++  java
  • 高速排序java语言实现

    本博客不再更新,很多其它精彩内容请訪问我的独立博客


    高速排序是非常重要的排序算法,可是我在学的时候发现网上没有特别好的样例所以自己动手写了一个。

    自己动手丰衣足食。


    package sort;
    
    import java.util.Random;
    
    public class QuickSort {
    	@SuppressWarnings("unused")
    	public boolean initTestArray(int[] testArray) {// 初始化testArray
    		if (testArray == null)
    			return false;
    		Random random = new Random();
    		for (int i = 0; i < testArray.length; i++) {
    			testArray[i] = random.nextInt(200);
    		}
    		return true;
    	}
    
    	public boolean printTestArray(int[] testArray) {// 打印testArray中的内容
    		if (testArray == null)
    			return false;
    		for (int i = 0; i < testArray.length; i++) {
    			System.out.print(testArray[i] + ",");
    		}
    		System.out.println();
    		return true;
    	}
    
    	public static boolean quickSort(int[] testArray, int left, int right) {
    		if(testArray==null)
    			return false;
    		if (left < right) {
    			int pivotpos = QuickSort.partition(testArray, left, right);
    			QuickSort.quickSort(testArray, left, pivotpos - 1);
    			QuickSort.quickSort(testArray, pivotpos + 1, right);
    		}
    		return true;
    	}
    
    	public static int partition(int[] testArray, int low, int high) {
    		int i = low, j = high, pivot = testArray[low],temp=0;
    		while (i < j) {
    			while (i < j && testArray[j] >= pivot)
    				j--;
    			while (i < j && testArray[i] <= pivot)
    				i++;
    			temp = testArray[i];
    			testArray[i] = testArray[j];
    			testArray[j] = temp;
    		}
    		testArray[low] = testArray[i];
    		testArray[i] = pivot;
    		return i;
    	}
    
    	public static void main(String args[]) {
    		int[] testArray = new int[20];
    		QuickSort quickSort = new QuickSort();
    		quickSort.initTestArray(testArray);
    		System.out.println("排序前:");
    		quickSort.printTestArray(testArray);
    		if(!QuickSort.quickSort(testArray, 0, testArray.length - 1))
    			System.out.println("排序出错!

    "); System.out.println("排序后:"); quickSort.printTestArray(testArray); } }



  • 相关阅读:
    九度-剑指Offer
    Fiddler如何模拟弱网环境进行测试
    登录功能测试点
    【转】移动应用崩溃日志收集工具对比
    【转】高扩展性网站的50条原则
    互联网常见架构接口压测性能分析及调优手段建议
    安装JDK时提示 IllegalArgumentException:Invalid characters in hostname的解决方法
    JAVA+Maven+TestNG搭建接口测试框架及实例
    Jmeter(二)Jmeter目录介绍
    性能测试-Gatling(一)
  • 原文地址:https://www.cnblogs.com/yfceshi/p/6812437.html
Copyright © 2011-2022 走看看