Sort algorithm-bubble sort (change), select sort

Source: Internet
Author: User

Last said bubble sort left 2 questions, one is the selection sort, one is the bubble sort performance, this time will say select sort first, then say bubble sort optimization

A selection sort

Select Sort is a simple and intuitive sorting algorithm. It works by selecting the smallest (or largest) element of the element to be sorted each time, storing it at the beginning of the sequence until all the data elements are sorted out

Process:

(with small to large sort)

First round

Compares 0-bit elements to all subsequent elements, placing small elements in 0-bit

Second round

Compares 1-bit elements to all subsequent elements, placing small elements in 1-bit

...

Until the last one

The code was last posted, and here's a copy.

1    Public Static voidSelectSort1 (int[] array)2         {3              for(inti =0; I < array. Length-1; i++)4             {5                 //Note that there are different loops in the process6                  for(intj = i +1; J < Array. Length; J + +)7                 {8                     intc =Array[i];9                     //here is the i-bit versus J-bit comparisonTen                     if(Array[i] >Array[j]) One                     { AArray[i] =Array[j]; -ARRAY[J] =C; -                     } the                 } -             } -}

Up to now say 2 kinds of very simple sort, can see the sort mainly involves operation including, 1 times the calendar selects each element 2 compares with other element 3 Exchange element The seat sort certainly will 1 times the calendar selects each element, therefore the sorting algorithm efficiency question mainly is looks 2, 3 operation the number of times, so we in the optimization algorithm, the main consideration,2,3 operation , there is the algorithm will use some variables to save the intermediate data, and will involve additional memory use,

Two next talk about the optimization problem of bubble sort

1 data would have been ordered, or would have been almost orderly, only a few times can be completed, but the above-mentioned bubble sort is always a complete comparison

such as array a:9,1,2,3,4,5

Array b:1,2,3,5,4

This is done in the first few times, with a meaningless comparison behind it.

Optimization method: Add a tag, when a trip comparison does not appear the element exchange table, indicating that the sorting is nearly complete, you can directly exit (mainly to reduce the outer loop)

Code:

1   Public Static voidBubbleSort2 (int[] array)2         {3              for(inti =0; I < array. Length-1; i++)4             {5                 //No swap occurs by default6                 BOOLChange =false;7                  for(intj =0; J < Array. Length-1I J + +)8                 {9                     if(Array[j] > array[j +1])Ten                     { One                         intc =Array[j]; AARRAY[J] = array[j +1]; -Array[j +1] =C; -  the                         //Exchange occurred -Change =true; -                     } -                 } +                 //swap not occurred, exit loop -                 if(!Change ) +                 { A                      Break; at                 } -             } -}

2 above is to reduce the outer circulation, actually can also reduce the inner circulation

For example, array 3,2,1,4,5,6,7,8,9,10

In this case, the first loop can be judged in the inner loop 4 to the back of the elements are orderly, so you can mark this seat, the next loop will not have to traverse the back of the element

Code

1   Public Static voidBUBBLESORT3 (int[] array)2         {3             //The element of the last exchange seat in this loop, followed by an orderly element.4             intLastchange = array. Length-1;5             //The element of the last exchange seat, the loop needs to traverse to the element so far6             intChange ;7              for(inti =0; I < array. Length-1; i++)8             {9Change =Lastchange;Ten                  for(intj =0; J < Change; J + +) One                 { A                     if(Array[j] > array[j +1]) -                     { -                         intc =Array[j]; theARRAY[J] = array[j +1]; -Array[j +1] =C; -  -                         //this round-robin Exchange seat +Lastchange =J; -                     } +                 } A  at                 //this time there is no exchange of seats, indicating that it is nearly orderly -                 if(Lastchange = =Change ) -                 { -                      Break; -                 } -             } in}

Three now can be seen above the choice of sort is actually a bit of a problem

For example: array: 9,8,7,6,5,4,3,2,1

There are small to large sort words, according to the above selection sort, the0-bit element will continue to exchange seats with the following elements , until with the last exchange, according to the order of small to large, 9 direct and 1 Exchange is good, to know that the element exchange is very performance-consuming

Solution: Add a tag that saves smaller (larger) elements in the following table each time it is compared, and finally swaps seats based on the tag

Code:

1  Public Static voidSelectSort2 (int[] array)2         {3              for(inti =0; I < array. Length-1; i++)4             {5                 //mark minimum value below table6                 intMinvalueindex =i;7                  for(intj = i +1; J < Array. Length; J + +)8                 {9                     //here is the minimum value compared to J-bitTen                     if(Array[minvalueindex] >Array[j]) One                     { AMinvalueindex =J; -                     } -                 } the                 //if the current position is not the minimum value, the interchange -                 if(Minvalueindex! =i) -                 { -                     intc =Array[i]; +Array[i] =Array[minvalueindex]; -Array[minvalueindex] =C; +                 } A             } at}

Summary below:

The sorting algorithm mainly takes note of the 3 operations (1 traversal selects all elements, 2 compared with other elements 3 swap elements), and there is memory overhead

Sort algorithm-bubble sort (change), select 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.