Quick Sorting Algorithm and Sorting Algorithm
Quick sorting is a very efficient sorting algorithm. It uses the "divide and conquer" idea to split a large one into smaller ones and then a small one into smaller ones.
The principle is as follows:
After sorting a group of records, the original sequence is divided into two parts. All the data in the first part is smaller than all the records in the latter part, then, the records of the first and second parts are sorted in sequence, and the process is recursive until all records in the sequence are sorted.
Java implementation of quick sorting:
1 package com. mianshi. easy; 2 3 public class TestQuickSort {4 5 public static void quickSort (int [] a) {6 sort (a, 0,. length-1); 7} 8 9 public static void sort (int [] a, int low, int high) {10 11 int I, j; 12 int index; 13 14 if (low> = high) {15 return; 16} 17 18 I = low; 19 j = high; 20 index = a [I]; 21 while (I <j) {22 while (I <j & a [j]> = index) {23 j --; 24} 25 if (I <j) 26 a [I ++] = a [j]; 27 while (I <j & a [I] <index) {28 I ++; 29} 30 if (I <j) {31 a [j --] = a [I]; 32} 33} 34 a [I] = index; 35 sort (, low, I-1); 36 sort (a, I + 1, high); 37} 38 39 40 public static void main (String [] args) {41 42 int [] a = {6, 5, 8}; 43 // fast sort 44 quickSort (); 45 for (int I = 0; I <. length; I ++) {46 System. out. print (a [I] + ""); 47} 48} 49 50 51 52}View Code
Result:
0 1 2 3 4 5 5 6 7 8 9
Algorithm stability:An unstable sorting algorithm.
Time Complexity:Average time complexity isO (nlogn)
Excellent Post help comprehension:
Http://blog.csdn.net/morewindows/article/details/6684558