Java implementation sorting algorithm: Fast row, bubble sort, select sort, insert sort, merge sort

Source: Internet
Author: User

First, overview: This paper mainly introduces the principle of several kinds of sorting algorithms and Java implementation, including: Bubble sort, select sort, insert sort, quick sort, merge sort, etc.

Second, bubble sort:

(1) Principle:

1, starting from the first data, compared to the second data, if the second data is less than the first data, then the position of two data exchange.

2, the pointer is moved from the first data to the second data, the second data compared to the third data, if the third data is less than the second data, then the position of two data exchange.

  3, and so on, complete the first round of sorting. After the first round of sorting, the largest element is moved to the far right.

4, according to the above process for the second round of sorting, the second largest ranked in the penultimate position.

5, repeat the above process, did not complete a round, the number of comparisons is reduced once.

(2) Example:

Pending data: 7, 6, 9, 8, 5,1

First round sequencing: the pointer points to 7,7 and 6 comparisons, 6<7, exchanges 6 and 7 positions, the result is: 6,7,9,8,5,1

The pointer points to the second element 7,7 and 9, 9>7, without swapping the position, the result is still: 6,7,9,8,5,1

The pointer points to the third element 9, compares 9 and 8,8<9, exchanges 8 and 9 positions, the result is: 6,7,8,9,5,1

The pointer points to the fourth element 9, compares 9 and 5,5<9, swaps 5 and 9, and the result is: 6,7,8,5,9,1

The pointer points to the Fifth Element 9, compares 9 and 1,1<9, exchanges 1 and 9 positions, and the result is 6,7,8,5,1,9

After the first round of sorting, the largest number 9 is moved to the far right.

The second round of sorting, the process is the same, only because the largest 9 has been placed on the far right, so do not compare 9, less a comparison, the end of the second round of the result is: 6,7,5,1,8,9

Third round result: 6,5,1,7,8,9

Fourth round comparison results: 5,1,6,7,8,9

Fifth round comparison results: 1,5,6,7,8,9

The final sorting result is: 1,5,6,7,8,9, sorted by the above-known n data, need to be N-1 wheel sort, the first round of the order required to compare the number of n-i times.

(3) Coding ideas:

  A two-layer loop is required, and the first loop I represents the number of rounds ordered, and the second loop, J, represents the number of comparisons.

(4) Code implementation:

 PackageCom.test.insertsort;/*** Bubble Sort *@authorBJH **/ Public classBobsort {Private int[] array; Private intlength; /*** Constructor Function *@paramArray*/     PublicBobsort (int[] Array) {         This. Array =Array;  This. length =Array.Length; }        /*** Print data in an array*/     Public voiddisplay () { for(intI:array) {System.out.print (i+" ");    } System.out.println (); }        /*** Bubble Sort*/     Public voidBobsort () { for(inti=0;i<length-1;i++) {//Sort Wheel Number             for(intj=0;j<length-1-i;j++) {//Number of comparisons                if(array[j]>array[j+1]){                    inttemp = array[j+1]; Array[j+1] =Array[j]; ARRAY[J]=temp; }            }             This. display (); }    }        /*** Test method *@paramargs*/     Public Static voidMain (string[] args) {int[] Array = {77,29,28,36,33,25,10}; Bobsort Bobsort=NewBobsort (array); System.out.println ("The data before sorting is:");        Bobsort.display ();        Bobsort.bobsort (); System.out.println ("The sorted data is:");    Bobsort.display (); }}

(5) Bubble Sorting algorithm Summary:

n elements need to sort N-1 round;

The first round needs to be compared n-i times;

n Order of elements, need to compare N (n-1)/2 times;

The algorithm of bubble sorting is of high complexity, O (n*n)

Java implementation sorting algorithm: Fast row, bubble sort, select sort, insert sort, merge sort

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.