This article describes the fast sorting algorithm implemented by Java based on division and conquer method. Share it for your reference, as follows:
package cn.nwsuaf.quick;/** * Randomly generate 20 numbers and quickly sort them* * @author Liu Yonglang* */public class Quick { /** * Exchange function to implement the exchange operation of two numbers in the array* * @param array * Array to be operated* @param i * First subscript of the exchange array* @param j * Second subscript of the exchange array*/ public static void swap(int[] array, int i, int j) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } /** * Division and conquer division algorithm* * @param array * Array to be operated* @param low * Start address of module in division* @param height * End address of module in division* @return Location subscript of reference element*/ public static int quick(int[] array, int low, int height) { // Set the first number as the reference element int pivot = array[low]; // Scan from right to left to find the first element smaller than pivot while (low < height) { while (low < height && array[height] >= pivot) height--; // Indicates that an element smaller than pivot was found if (low < height) // After swap, low executes +1 operation swap(array, low++, height); // Scan from left to right to find the first element larger than pivot while (low < height && array[low] <= pivot) low++; // Indicates that an element larger than pivot was found if (low < height) // After swap, executes -1 operation swap(array, low, height--); } // Return to the final position of the reference element return height; } /** * Quick sort of array* * @param array * Array to be operated* @param low * Low * @param height * High */ public static void sort(int[] array, int low, int height) { // Record the position corresponding to the divided reference element int temp; // Sort only if (low < height) { // Divide array temp = quick(array, low, height); // Sort recursively for the left interval sort(array, low, temp - 1); // Sort recursively for the right interval sort(array, temp + 1, height); } } public static void main(String[] args) { int[] array = new int[20]; System.out.println("Wulin.com test result:"); System.out.print("Before sorting sequence:"); for (int i = 0; i < array.length; i++) { // Randomly generate 20 integers of 0-99 array[i] = (int) (Math.random() * 100); System.out.print(array[i] + " "); } System.out.print("/nSorted sequence:"); sort(array, 0, array.length - 1); for (int i = 0; i < array.length; i++) System.out.print(array[i] + " "); }}Running results:
PS: Here is a demonstration tool for your reference:
Online animation demonstration insert/select/bubble/merge/hill/quick sorting algorithm process tool:
http://tools.VeVB.COM/aideddesign/paixu_ys
For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.