Quick sorting-JAVA Implementation

Source: Internet
Author: User

Quick Sort)
1. Thoughts
Quick sorting is a large-scale recursive algorithm that sorts data in the local area separately. Essentially, it is the local version of Merge Sorting. The quick sorting can be composed of the following four steps.

(1) If no more than one data entry is returned.
(2) generally, the leftmost value of the sequence is used as the pivot data.
(3) divide the sequence into two parts. One part is greater than the pivot data, and the other part is smaller than the pivot data.
(4) use recursive sorting sequence on both sides.

Quick sorting is faster than most sorting algorithms. Although we can write algorithms faster than quick sorting in some special cases, it is generally not faster than it. Quick sorting is recursive. It is not a good choice for machines with very limited memory.

 

Note: The core idea is to put a small part on the left and a large part on the Right To achieve segmentation.


2,Algorithm complexity

Best case: Because the sequence is divided into two parts each time (generally, the complexity of binary is related to logn), it is O (n * logn)
Worst case: in basic order, it degrades to bubble order, which is about N x n times, so it is O (n x N)

 

3,Stability

The original order may be disrupted because each time the element needs to be exchanged with the central axis. For example, if the sequence is 5 3 3 4 3 8 9 10 11, the sequence of 3 is disrupted. Therefore, fast sorting is unstable!

 

4,Implementation

Java implementation method.

Public class quicksort {

 

/**

* @ Param ARGs

*/

Public static void main (string [] ARGs ){

Int [] DATA = {49, 38, 65, 97, 76, 13, 27, 9 };

Quicksort (data, 0, Data. Length-1); // The first implementation method

Printdata (data );

}

 

Public static void quicksort (INT [] data, int low, int high ){

Int I = low;

Int J = high;

 

// Parameter check

If (low> = high ){

Return;

}

 

Int comparablekey = data [low]; // generally, the first number is selected as the base number.

If (I <j ){

While (I <j ){

While (I <High & comparablekey> data [I]) {// search for a number greater than the base number from the left.

I ++;

}

 

While (low <J & comparablekey <data [J]) {// search for a number smaller than the base number from the right.

J --;

}

 

If (I <j ){

Swap (data, I, j );

}

 

}

Quicksort (data, low, I-1 );

Quicksort (data, J + 1, high );

}

 

}

 

Public static void swap (INT [] data, int I, Int J ){

Int temp = data [I];

Data [I] = data [J];

Data [J] = temp;

}

 

Public static void printdata (INT [] data ){

For (int I: Data ){

System. Out. Print (I + "");

}

}

 

 

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.