Sort--quicksort Quick Row

Source: Internet
Author: User

   Quick the Implementation

Quick Platoon, just like its name must be, the wind as fast. Is basically the quickest sort algorithm. The basic idea of a quick line is to select a segmented element. Sort this element out. All elements on the left side of this element are smaller than this element, and all elements to the right of this element are larger than this element. Then the left and right 2 arrays are sorted separately.

Suppose you have the following array (all I left objects are smaller than the Shard object.) All J right objects are larger than the Shard object. This sentence will be useful to know later.

  

First, we treat the element of index = 0 as a shard element.

Starting with the position of index = 1, find the first element greater than 49. Found 65 is greater than 49 so mark I = 2

  

Then starting from the end of the array to find the first less than 49, when found index = 6, value = 27 is less than 49 so mark.

  

Now swap the elements of I and J.

  

After the exchange, the element also conforms to the definition (all I to the left of the object is less than the Shard object because I will continue to look for the element to the right, all the elements I position now must be less than the Shard element, otherwise not conform to the definition I left the element is less than the Shard element) and (all J right object is greater than )

So you can go down.

Continue to find the element greater than 49 starting from the position i = 2, when i = 3 is found value = 97

  

Continue from the right to find J = 5 When 13 is less than 49

  

Swap location

  

Now I go ahead. Stop at index = 4 where 76 is greater than 49

  

J also continues to look to the left from the right. When j = 3 value = 13 less than 49

  

I >= J so do not swap position. At this point, we need to swap the elements of the Shard and the J position.

  

At this point, the position of index = 3 is already in the right position. 49 of the elements on the right are greater than 49, 49 to the left of the elements are less than 49

You can then sort the elements of 0-3 and the elements from 4 to 7.

Steal a picture to help understand

  

Concrete implementation of Https://github.com/Cheemion/algorithms/blob/master/src/com/algorithms/sort/QuickSort.java

There is no clear place, hope can give me a message. Because I feel like blogging is not very good. Rattle

  

  

  

Sort--quicksort Quick Row

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.