快速排序,Java实现

来源:互联网 发布:暗黑破坏神2 mac 编辑:程序博客网 时间:2024/05/04 10:34

快速排序,是一种较高效的排序算法,同时,也有点小难(偶觉得啦)。
许多时候,一些大公司的面试,就给你一张白纸,手写程序。一时间,还真不能写出来,或错漏百出。

关于快速排序,还有需要有趣的历史与使用历程,有兴趣可了解。


package com.nicchagil.generics.study.No099快速排序;public class QuickSort {    public static void sort(int[] array) {        if (array == null || array.length == 0) {            return;        }        sort(array, 0, array.length);    }    public static void sort(int[] array, int left, int right) {        if (left > right) {            System.out.println("Close for the recursivion : [" + left + "], [" + right + "]");            return;        }        print(array, left, right);        int standardIndex = left;        int standard = array[standardIndex];        int i = left;        int j = right;        while (i < j) {            while (array[j] >= standard && i < j) {                j--;            }            while (array[i] <= standard && i < j) {                i++;            }            /*             * 在此断点,有如下情况:             * 1、j坐标从右向左找,找到小于标准值的数;i坐标从左向右找,找到大于标准值的数             * 2、j坐标从右向左找,找到小于标准值的数;i坐标从左向右找,但没找到,遍历直到与j坐标相等             * 3、j坐标从右向左找,未能找到小于标识值的数,遍历直到与i坐标相等             */            System.out.println("there are meet : [" + i + "], [" + j + "], values are " + array[i] + ", " + array[j]);            if (i < j) {                swap(array, i, j);            }            print(array, left, right);        }        System.out.println("Put the standard value into the middle : [" + standardIndex + "], [" + i + "], values are " + array[standardIndex] + ", " + array[i]);        swap(array, standardIndex, i);        print(array, left, right);        if (left < i - 1) {            System.out.println("Recursive calls in range : [" + left + "], [" + (i - 1) + "]");            sort(array, left, i - 1);        }        if (i + 1 < right) {            System.out.println("Recursive calls in range : [" + (i + 1) + "], [" + right + "]");            sort(array, i + 1, right);        }    }    private static void swap(int[] array, int i, int j) {        System.out.println("swap the position of the array : [" + i + "], [" + j + "], values are " + array[i] + ", " + array[j]);        int temp = array[i];        array[i] = array[j];        array[j] = temp;    }    public static void main(String[] args) {        int[] array = {565,841,695,225,321,541,265,159,842,654};        print(array, 0, array.length - 1);        sort(array, 0, array.length - 1);        print(array, 0, array.length - 1);    }    public static void print(int[] array, int left, int right) {        for (int i = left; i <= right; i++) {            System.out.print(array[i] + " ");        }        System.out.println();    }}

日志如下:

565 841 695 225 321 541 265 159 842 654 565 841 695 225 321 541 265 159 842 654 there are meet : [1], [7], values are 841, 159swap the position of the array : [1], [7], values are 841, 159565 159 695 225 321 541 265 841 842 654 there are meet : [2], [6], values are 695, 265swap the position of the array : [2], [6], values are 695, 265565 159 265 225 321 541 695 841 842 654 there are meet : [5], [5], values are 541, 541565 159 265 225 321 541 695 841 842 654 Put the standard value into the middle : [0], [5], values are 565, 541swap the position of the array : [0], [5], values are 565, 541541 159 265 225 321 565 695 841 842 654 Recursive calls in range : [0], [4]541 159 265 225 321 there are meet : [4], [4], values are 321, 321541 159 265 225 321 Put the standard value into the middle : [0], [4], values are 541, 321swap the position of the array : [0], [4], values are 541, 321321 159 265 225 541 Recursive calls in range : [0], [3]321 159 265 225 there are meet : [3], [3], values are 225, 225321 159 265 225 Put the standard value into the middle : [0], [3], values are 321, 225swap the position of the array : [0], [3], values are 321, 225225 159 265 321 Recursive calls in range : [0], [2]225 159 265 there are meet : [1], [1], values are 159, 159225 159 265 Put the standard value into the middle : [0], [1], values are 225, 159swap the position of the array : [0], [1], values are 225, 159159 225 265 Recursive calls in range : [6], [9]695 841 842 654 there are meet : [7], [9], values are 841, 654swap the position of the array : [7], [9], values are 841, 654695 654 842 841 there are meet : [7], [7], values are 654, 654695 654 842 841 Put the standard value into the middle : [6], [7], values are 695, 654swap the position of the array : [6], [7], values are 695, 654654 695 842 841 Recursive calls in range : [8], [9]842 841 there are meet : [9], [9], values are 841, 841842 841 Put the standard value into the middle : [8], [9], values are 842, 841swap the position of the array : [8], [9], values are 842, 841841 842 159 225 265 321 541 565 654 695 841 842 

参考:
坐在马桶上看算法:快速排序

0 0
原创粉丝点击