Data Structure-simple selection sort and code (C ++)

Source: Internet
Author: User

Data Structure-simple selection sort)

 

 

Selection sort ):In the n-I + 1 record, the records with the smallest keyword are selected as the I-th record in the sequence.

 

Simple selection sort ):By comparing the n-I keywords, the records with the minimum keyword are selected from the n-I + 1 record and exchanged with the I record.

 

Select sort needs to compare n (n-1)/2 times, namely (n-1) + (n-2) +... + 1 = [(n-1) + 1] (n-1)/2 times,The time complexity is O (n ^ 2).

 

Simple selection and sortingMain StepsYes: 1. Select the location of a small element. 2. Switch.

 

Code:

 

/* * SimpleSelectionSort.cpp * *  Created on: 2014.6.5 *      Author: Spike */#include 
 
  #include 
  
   void print(const std::vector
   
    & L) {for (auto i : L) {std::cout << i <<  ;}std::cout << std::endl;}int SelectMinKey(std::vector
    
     & L, const size_t p) {int min = p;for (size_t i=p+1; i
     
      & L) {for (size_t i=0; i
      
        L = {49, 38, 65, 97, 76, 13, 27, 49, 55, 4};SelectSort(L);print(L);}
      
     
    
   
  
 

Output:

 

 

4 38 65 97 76 13 27 49 55 49 4 13 65 97 76 38 27 49 55 49 4 13 27 97 76 38 65 49 55 49 4 13 27 38 76 97 65 49 55 49 4 13 27 38 49 97 65 76 55 49 4 13 27 38 49 49 65 76 55 97 4 13 27 38 49 49 55 76 65 97 4 13 27 38 49 49 55 65 76 97 4 13 27 38 49 49 55 65 76 97 4 13 27 38 49 49 55 65 76 97 4 13 27 38 49 49 55 65 76 97 


 

 

 

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.