Java for fast sorting

Source: Internet
Author: User

I am ashamed to say that I spoke about some of my blog posts yesterday.AlgorithmIn fact, these algorithms have been learned in universities, but almost all of them are forgotten. Although algorithms are basically not used in upper-layer Java Development, I still feel that algorithms are a kind of idea and a kind of soul. So I not only opened the data structure of teacher Yan Weimin, implement the previously forgotten algorithms one by one.

Basic Idea of quick sorting:

Sort the records to be sorted into two separate parts. If the keywords of one part of the records are smaller than those of the other, sort the two parts separately, until the entire sequence is ordered.

Let's take a look at this figure first:

The whole sequence is regarded as an array, and the zero position is regarded as the central axis. If it is compared with the last one, it will not be processed if it is larger than it; after the exchange, we can compare it with a small client, which is smaller than it, but not more than it. In this way, after a round-robin is completed, the left side is smaller than the central axis, and the right side is bigger than the central axis. Then, the two independent arrays are sorted by Division and control.

Public int getmiddle (integer [] list, int low, int high) {int TMP = list [low]; // the first of the array acts as the center axis while (low  

Recursive grouping and sorting algorithms:

 
Public void _ quicksort (integer [] list, int low, int high) {If (low  

Public void quick (integer [] Str) {If (Str. length> 0) {// check whether the array is empty _ quicksort (STR, 0, str. length-1 );}}

Compile the Test method:

 
Public class testmain {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stub integer [] list = {34,3, 53,2, 23,7, 14,10}; quicsort Qs = new quicsort (); Qs. quick (list); For (INT I = 0; I <list. length; I ++) {system. out. print (list [I] + "");} system. out. println ();}}

Let's take a look at the printed results:

2 3 7 10 14 23 34 53

In this way, the sorting is better. Quick sorting is an improvement of Bubble sorting, and the average time complexity is O (nlogn ).

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.