Learn Java absolutely to understand, Java programming the most commonly used in several sorting algorithms!

Source: Internet
Author: User

Today, let's share some Java code for some common sorting algorithms in Java

Recommend my Java learning sheep June Qian 616, 959, the last 444. Put the numbers together! , the group has free learning videos and projects for everyone to practiced hand. When the great God is free, will bring everyone, learn the process of Java must ask more than remember, do not fear the problem, to take the initiative to solve the problem.

Bubble sort

Select sort

Insert Sort

Merge sort

For example: Descending row {10,5,9,6} is divided into {10,5},{9,6} and then {5,10},{6,9} then completed

Quick Sort

On the last quick sort maybe people don't understand, so I'll give you an example.

Start entering the following array

Create a variable i=0 (point to the first data), j=5 (point to the last data), k=6 (assigns the value of the first data).

We're going to move all the numbers smaller than K to the left of K, so we can start looking for a smaller number than 6, starting with J, from right to left, constantly diminishing the value of the variable J, we find the first subscript 3 of the data is smaller than 6, so the data 3 is moved to subscript 0 position, the subscript 0 of the data 6 to subscript 3, the first comparison is completed:

I=0 j=3 k=6

Then, to start the second comparison, this time to become bigger than the K, and to go after the search. Sliding scale variable I, found that the data of subscript 2 is the first larger than the K, so with the subscript 2 of the data 7 and J points to the subscript 3 of the data of 6 to exchange, the data state into the following table:

i=2 j=3 k=6

It is said that the above two comparisons are a cycle.

Then, the variable J is decremented again, repeating the above cycle comparison.

In this example, we do a loop and find I and J "Meet": they all point to subscript 2. So, the first time the comparison is over. The results are as follows, usually K (=6) the number on the left is smaller than it, the number of K right is larger than it:

If I and J do not meet, then sliding scale I find large, not yet, and then diminishing J to find small, so repeated, continuous circulation. Attention to judgment and search is carried out at the same time.

Then, the data on both sides of the K, and then the process is divided into the above, until no further grouping.

Note: The first time quick sort does not directly get the final result, only the number smaller than K and K is divided into the two sides of K. To get the final result, you need to perform this step again on an array of subscript 22 edges, and then decompose the array until the array is no longer decomposed (only one data) to get the correct result.

Finally, you need to pay attention to two points (Java Learning AC sheep June first 616, 959, the last 444. Put the numbers together! Every day will be carefully selected a new program to play out the detailed explanation, share! Including answering questions! )

    1. Bubble sorting is a bit similar to selecting sort, first by comparing the smallest/largest element to the last; the difference is that each time the bubble sort is performed, the position is exchanged after each comparison, and the selection sort is the position of the smallest value after each comparison, and the last position is exchanged at the end.

    2. The insertion sort is characterized by starting with the second element of the sequence, sorting with the first element, completing the round, and the second round from the third element, followed by the previous comparison, and then sorted, in fact, to do bubble sort.

    3. All of these assume that the length of the array is n

These sorts of sorting algorithms, just simple implementation of the function of sorting, but also need to improve, hope to teach, we have what other algorithms can also share

Learn Java absolutely to understand, Java programming the most commonly used in several sorting algorithms!

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.