Quick Sort Algorithm explanation and implementation

Source: Internet
Author: User

Quick sorting is a sort algorithm. It is widely regarded as the optimal sorting algorithm to solve general problems. Like insert sorting, quick sorting is also a type of comparative sorting without additional storage space. Quick sorting is a good choice when processing medium to large data sets.

Fast sorting is a Sort Algorithm, so we can divide the sorting into three steps using the method of divide and conquer.

1. Split: Set a split value to divide the data into two parts.

2. Governance: The quick sorting method is used recursively.

3. Merge: sort the parts until they are completed.


The implementation code is as follows:

Import Java. util. random;/*** fast sorting {points, governance, and combination} * @ author Kevin Luan **/public class quicksort {public static void main (string [] ARGs) {int Total = 5; quicksort = new quicksort (); While (total> 0) {INTEGER arr [] = randomarray (1000000); long start = system. currenttimemillis (); quicksort. sort (ARR); system. out. println ("use time:" + (system. currenttimemillis ()-Start); quicksort. check (ARR); Total -- ;}} public void sort (integer [] ARR) {mergesort (ARR, 0, arr. length-1);} private void mergesort (integer arr [], int low, int high) {int A = low; int B = high; int middle; middle = getmiddle (ARR, low, high); While (low  


The result of sorting 1 million random numbers is as follows (unit: milliseconds ):

Use Time: 249

Use Time: 205

Use Time: 245

Use Time: 220

Use Time: 203




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.