Sorting algorithm Visualization

Source: Internet
Author: User

Reference 1) <Algorithms_4th> Chapter 2 Sorting

Reference 2) sorting algorithms, Wiki

1 Selection Sort Selection

First, the whole sequence is compared once, the minimum value is selected, and the first position is placed;

Then, the remaining sequence is compared again, the minimum value is selected, and the second position is placed; And so on

Java instance:

 Public classselection{ Public Static voidsort (comparable[] a) {//Sort a[] into increasing order.        intN = A.length;//Array Length         for(inti = 0; i < N; i++)        {               //Exchange A[i] with smallest entry in A[I+1...N).            intmin = i;//index of minimal entr.             for(intj = i+1; J < N; J + +)                if(Less (A[j], a[min])) min =J;        Exch (A, I, min); }      }}
View Code

2 Insertion Sort Insert sorting

First of all, the first two elements comparison, reverse exchange position, the order is unchanged;

The subsequent elements are then compared with them (that is, the preceding elements that have been sorted), the corresponding positions are inserted, and so on.

Java instance:

 Public classinsertion{ Public Static voidsort (comparable[] a) {//Sort a[] into increasing order.        intN =a.length;  for(inti = 1; i < N; i++)        {            //Insert A[i] among a[i-1], a[i-2], a[i-3] ... ..             for(intj = i; J > 0 && less (a[j], a[j-1]); j--) Exch (A, J, J-1); }      }}
View Code

3 merge sort combined sorting

4 Quick Sort Fast sorting

5 Bubble sort Bubble sorting

First, the first two elements are compared, the small value is placed before, the large value is discharged;

Then, move one position to the right and continue the 22 comparison until the maximum value sinks to the end;

Sorting algorithm Visualization

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.