Algorithm: Fast sorting, algorithm: Sorting
Principles of the Quick Sort Algorithm: see the description.
The Code is as follows:
package com.huan;import java.util.Arrays;import java.util.Random;public class QuickSort {public static void main(String[] args) {int[] data = new int[10];for (int i = 0; i < 10; i++){data[i] = new Random().nextInt(1000);}System.out.println(Arrays.toString(data));quicksort(data, 0, data.length - 1);System.out.println(Arrays.toString(data));}public static void quicksort(int[] data, int i, int j){if(i < j){int m = i, n = j;int k = data[i];while(i < j){while (i < j && data[j] > k){j--;}data[i] = data[j];while (i < j && data[i] < k){i++;}data[j] = data[i];}data[i] = k;quicksort(data, m, i-1);quicksort(data, i+1, n);}}}
Compile an algorithm for fast sorting
/*************************************** *********************************/
/* Fast sorting */
/*************************************** *********************************/
Void CommonALG: quickSort (float * arr, int left, int right)
{
Int ltemp = left;
Int rtemp = right;
Float f = arr [(left + right)/2];
Float t = 0;
While (ltemp <rtemp)
{
While (arr [ltemp] <f)
++ Ltemp;
While (arr [rtemp]> f)
-- Rtemp;
If (ltemp <= rtemp)
{
T = arr [ltemp];
Arr [ltemp] = arr [rtemp];
Arr [rtemp] = t;
-- Rtemp;
++ Ltemp;
}
}
If (ltemp = rtemp)
Ltemp ++;
If (left <rtemp)
QuickSort (arr, left, ltemp-1 );
If (ltemp <right)
QuickSort (arr, rtemp + 1, right );
}
C ++ quick sort algorithm code
Let's take a look at Baidu encyclopedia. It's very detailed:
Baike.baidu.com/view/42516.htm