写了好多业务,总感觉心里很不踏实。今天就写一个简单的排序算法->快速排序,转转脑子,不然真的长锈了。
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 Numberint baseNumber = arr[left0];while (left != right) {// find less than baseNumber from right arraywhile (arr[right] >= baseNumber && right > left) {right--;}// find large than baseNumber from left arraywhile (arr[left] <= baseNumber && right > left) {left++;}// change positionchange(arr, left, right);}// baseNumber backchange(arr, left, left0);// sort leftquickSort(arr, left0, left - 1);// sort rightquickSort(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;}
}