Quick Sorting Algorithm and Sorting Algorithm

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.