Sorting algorithm (ii)--selection sorting and improvement

Source: Internet
Author: User

Select sort
Basic ideas

There is a disadvantage in bubble sort, for example, when we compare the first number A1 with the second number A2, as long as the A1 is larger than A2 exchange position, but we are not sure A2 is the smallest element, if there is smaller than it, the element will also be exchanged with A2 again, And it is possible that this exchange can occur several times to determine the final location of the A2.

Selecting sort avoids this time-consuming interchange operation, starting with the first element, scanning the entire array to be sorted, finding the smallest element to swap the position with the first element, and then starting with the second element, continuing to find the smallest element to swap the position with the second element, and so on.



Java implementation

Select sort public   void Selectionsort () {       int minpoint;  Small mark       int len = Array.Length that stores the smallest element;       int temp;       int counter = 1;             for (int i=0;i<len-1;i++) {                   minpoint= i;           for (int j=i+1;j<=len-1;j++) {  //each round of sorting is done, a relative minimum element is determined, and the next round of sorting only sorts the subsequent elements              if (Array[j]<array[minpoint]) {  //If an element in the pending array is smaller than the current element, MinPoint points to the subscript of the element                 minpoint= J;              }          }                   if (minpoint!=i) {  //If a smaller element is found, swap position              temp= array[i];              array[i]= Array[minpoint];              array[minpoint]= temp;          }                   System.out.print ("+counter+" Round Order Result: ");          Display ();          counter++;       }   }


Algorithm analysis

Select sort and bubble sort, you need to do n (N-1)/2 comparisons, but only n interchanges, when n is large, the time of the interchange is more influential, so the time complexity of selecting sort is O (N2).

Although the selection sort and bubble sort are at the same level of time complexity, there is no doubt that sorting is more efficient because it has fewer switching operations and is much faster when switching operations are more time-efficient than comparing operations.


Improvement of the selection sort

The traditional selection sort only determines the minimum value each time, according to the experience of the improved bubbling algorithm, we can make the following improvements to the sorting algorithm: the order of each trip determines two maximum-max value and minimum value, so that the sort pass number can be reduced by half.

The improved code is as follows:

Select the sort improvement version public void Selectionsort_improvement () {int minpoint;  Small standard int maxpoint that stores the smallest element;       The small mark int len = Array.Length that stores the largest element;       int temp;             int counter = 1;          for (int i=0;i<len/2;i++) {minpoint= i;          Maxpoint= i; for (int j=i+1;j<=len-1-i;j++) {//each round of sorting is determined with two maximum values, and the next round sorts when the comparison range is reduced by two elements if (Array[j]<array[minpoint]) {//IF                 An element in the pending array is smaller than the current element, and minpoint points to the subscript of the element minpoint= J;              Continue              }else if (Array[j]>array[maxpoint]) {//If an element in the pending array is larger than the current element, MaxPoint points to the subscript maxpoint= J of the element;              }} if (minpoint!=i) {//If a smaller element is found, swap the position with the first element temp= Array[i];              Array[i]= Array[minpoint];                           array[minpoint]= temp; The original first element has swapped the position with the subscript minpoint element//If the previous MaxPoint points to the first element, then the maxpoint needs to be re-directed to Array[minpoint]//Because now        Array[minpoint] The data in the first element is stored      if (maxpoint== i) {maxpoint= minpoint;              }} if (maxpoint!=len-1-i) {//If a larger element is found, swap the position with the last element temp= Array[len-1-i];              Array[len-1-i]= Array[maxpoint];          array[maxpoint]= temp;          } System.out.print ("+counter+" wheel sort Result: ");          Display ();       counter++; }   }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Sorting algorithm (ii)--selection sorting and improvement

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.