Data Structure --- fast sorting --- java and c ++ implementation
Quick sorting: first, select the first element in a group as the benchmark number. After the first row, the first number before the benchmark number is smaller than the base number, and the second number is greater than the base number. After being divided into two groups, each group is recursive.
For example ):
(1) Raw Data: 6 2 7 3 8 9 I = 0, j = 5
(2) Use 6 as the benchmark and reduce j. If the ratio is 6, the switch is: 3 2 7 6 8 9 I = 0, j = 3.
(3) increase I based on 6. If I is larger than 6, the exchange is: 3 2 6 7 8 9 I = 2, j = 3.
/* Java Implementation ---------------------------------- */package pack; public class Main {public static void main (String [] args) {int [] arr = {57,68, 59,52, 33, 24}; quickSort (arr, 0, arr. length-1); for (int I = 0; I = high) // if not written, it is an endless loop return; int first = low; int last = high; int key = a [first]; // It is based on the key. If it is the first time, it is a [0] while (first
A [first]) {// move back from front to back first ++;} int temp1 = a [first]; a [first] = a [last]; a [last] = temp1;} quickSort (a, low, first-1); // the first group of recursive quickSort (a, first + 1, high ); // The next group of recursion}/* c ++ implementation ------------------------------------------------ */# include "stdafx. h "# include
Using namespace std; void Qsort (int a [], int low, int high); int _ tmain (int argc, _ TCHAR * argv []) {int a [] = {57, 68, 59, 52, 72, 28, 96, 33, 24}; Qsort (a, 0, sizeof () /sizeof (a [0])-1); for (int I = 0; I <sizeof (a)/sizeof (a [0]); I ++) {cout <a [I] <
= High) {return;} int first = low; int last = high; int key = a [first]; while (first <last) {while (first <last & a [last]> = key) {-- last;} int temp = a [first]; a [first] = a [last]; a [last] = temp; while (first <last & a [first] <= key) {++ first;} int temp1 = a [first]; a [first] = a [last]; a [last] = temp1;} Qsort (a, low, first-1); Qsort (a, first + 1, high );}