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

    private static void quickSort(int left, int right, int[] array) {
    		if (left > right) {
    			return;
    		}
    
    		int leftIndex = left, rightIndex = right, flag = array[left];
    		while (leftIndex != rightIndex) {
    			// 从右开始找比指定的基准数小的数
    			while (leftIndex < rightIndex && array[rightIndex] >= flag) {
    				rightIndex--;
    			}
    			// 从左开始找比指定的基准数大的数
    			while (leftIndex < rightIndex && array[leftIndex] <= flag) {
    				leftIndex++;
    			}
    			// 交换
    			if (leftIndex < rightIndex) {
    				int tmp = array[leftIndex];
    				array[leftIndex] = array[rightIndex];
    				array[rightIndex] = tmp;
    			}
    		}
    		// 重置被用来比较的flag数
    		array[left] = array[leftIndex];
    		array[leftIndex] = flag;
    		// 递归分治
    		quickSort(left, leftIndex - 1, array);
    		quickSort(rightIndex + 1, right, array);
    	}
    
  • 相关阅读:
    JavaScript事件处理
    JavaScript模拟"类"的三种方法
    非构造函数的继承和拷贝
    构造函数的继承
    vim开发环境
    socket之非阻塞
    网络编程
    多线程
    消息队列
    信号
  • 原文地址:https://www.cnblogs.com/eahau/p/10980672.html
Copyright © 2011-2022 走看看