Handy Programming---quick sort (QuickSort)-java implementation

Source: Internet
Author: User

Background

The quick sort is a sort of method proposed by the American Donny Holl in the 60 's. This sort of way, at that time was already very fast sort of a. So it's called "quick Sort" on the name. This algorithm is one of the seven algorithms of the 20th century, the average time complexity is 0 (NLOGN), and in the case of O (Nlogn), the actual operation speed is faster than other same time complexity of the sorting method.

For Donny Holl and quick ordering of the background interested in the students, you can take a look at this introduction: http://www.nowamagic.net/librarys/veda/detail/2391

Sorting ideas

The idea of a quick sort is difficult to think of, but it's very easy to understand.

His train of thought was this:

1, first select the queue, an element is the base value (the general selection of the head element, or the tail element).

2. base value is compared to all elements in turn . The elements are divided into two queues A, B, according to the comparison results. One of all elements is larger than the cardinality value, and one element is smaller than the cardinality value.

3. Use a as a new queue, select the cardinality again, and divide it into two smaller queues

4, so that each small queue has been infinitely split into smaller two queues .

5, until a queue has been split into a can not be unpacked (that is, an element)

6, because the order between the queues is fixed. Once these queues are combined, the overall sorting is done.

(Anti-Theft connection: This article starting from http://www.cnblogs.com/jilodream/)

Note that there are two core steps here,

1. Select the value element to divide the overall queue into two sub-queues

2, then the sub-queue as a new, the overall size is smaller than the current, the new queue, to calculate, until it is very easy to calculate.

These two core steps create the natural advantages of a quick row:

1, through the comparison of the size of the sub-queue elements, in the future of the comparison process, the element's comparison range is always stuck in this sub-queue, no longer make redundant comparisons. This makes early comparisons still have a big impact on later comparisons. and similar to the bubble sort method, then a lot of earlier comparisons, the effect on the latter is very small. This and the KMP algorithm is very similar to the previous comparison as far as possible to maximize the use.

2, the original size of the queue, split into a number of small sub-queue, these sub-queues to (anti-theft connection: This article starting from http://www.cnblogs.com/jilodream/) The problem solved with the original queue, but the size has become smaller. Such constant splitting, the formation of a divide and conquer the idea. This idea coincides with the knapsack algorithm .

For the students who understand the difficulty of the text, you can see below this online classic dynamic map, very vivid:

Below is the Java implementation code

1 Importjava.util.Arrays;2 3  Public classQuickSort4 {5      Public Static voidMain (String args[])6     {7QuickSort QuickSort =NewQuickSort ();8         int[] Arrays =New int[]9{1, 12, 2, 13, 3, 14, 4, 15, 5, 16, 17, 17, 177, 18, 8, 8, 19 };Ten quicksort.quicksort (arrays); One System.out.println (arrays.tostring (Arrays)); A     } -      -     Private voidQuickSort (int[] arrays) the     { -Subquicksort (arrays, 0, arrays.length-1); -     } -      +     Private voidSubquicksort (int[] Arrays,intStartintend) -     { +         if(Start >=end) A         { at             return; -         } -         intMiddleindex =Subquicksortcore (arrays, start, end); -Subquicksort (arrays, start, middleIndex-1); -Subquicksort (Arrays, Middleindex + 1, end); -     } in      -     Private intSubquicksortcore (int[] Arrays,intStartintend) to     { +         intMiddlevalue =Arrays[start]; -          while(Start <end) the         { *              while(Arrays[end] >= middlevalue && Start <end) $             {Panax Notoginsengend--; -             } theArrays[start] =Arrays[end]; +              while(Arrays[start] <= middlevalue && Start <end) A             { thestart++; +             } -Arrays[end] =Arrays[start]; $         } $Arrays[start] =Middlevalue; -         returnstart; -     } the}

Handy Programming---quick sort (QuickSort)-java implementation

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.