简单算法之快速排序

     写了好多业务,总感觉心里很不踏实。今天就写一个简单的排序算法->快速排序,转转脑子,不然真的长锈了。

package com.chasen;

public class QuickSort {
    public static void main(String[] args) {
        int[] arr = {100, 6, 1, 2, 7, 9, 3, 4, 5, 10, 8};
        quickSort(arr, 0, arr.length - 1);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
    public static void quickSort(int[] arr, int left, int right) {
        if (right < left) {
            return;
        }
        int left0 = left;
        int right0 = right;
        // init base Number
        int baseNumber = arr[left0];
        while (left != right) {
            // find less than baseNumber from right array
            while (arr[right] >= baseNumber && right > left) {
                right--;
            }
            // find large than baseNumber from left array
            while (arr[left] <= baseNumber && right > left) {
                left++;
            }
            // change position
            change(arr, left, right);
        }
        // baseNumber back
        change(arr, left, left0);

        // sort left
        quickSort(arr, left0, left - 1);
        // sort right
        quickSort(arr, left + 1, right0);
    }
    public static void change(int[] arr, int indexA, int indexB) {
        int temp = arr[indexA];
        arr[indexA] = arr[indexB];
        arr[indexB] = temp;
    }
}

 

你可能感兴趣的:(java)