Simple selection sort (Easy Selection sort)

Source: Internet
Author: User

Simple Selectionsort: Compare bubble sort, each time it is 22 compare interchange, n elements n-1 times comparison can determine the final position of 1 elements. The simple selection sorting method is to select the smallest record of the keyword from the n-i+1 records by comparing the N-i keywords, and exchange them with the first (1≤i≤n) records.
#include <iostream> using namespace std; //Simple Select sort int Simpleselectionsort (int* arr,int length); void Swap (int& elem1,int& elem2); void Test (); void Printarr (int* arr,int length);
void Swap (int& elem1,int& elem2) { int tmp = ELEM1; elem1 = elem2; elem2 = tmp; } int Simpleselectionsort (int* arr,int length) { if (null==arr| | length<=0) return-1; int minpos = 0; for (int idx=0;idx!=length;++idx)         { minpos = idx; for (int iidx=idx+1;iidx<length;++iidx)                 { if (Arr[iidx]<arr[minpos])                         { minpos = IIDX;                         }                 } if (idx!=minpos)                 { swap (Arr[idx],arr[minpos]);                 }         } return 0; }
void Printarr (int* arr,int length) { if (null==arr| | length<=0) return; for (int idx=0;idx!=length;++idx)         { cout<<arr[idx]<< "";         } cout<<endl; } void Test () { int arr[] = {6,5,3,1,8,7,2,4}; Printarr (arr,8); Simpleselectionsort (arr,8); Printarr (arr,8); cout<<endl;
int arr1[] = {1,2,3,4,5,6,7,8}; Printarr (arr1,8); Simpleselectionsort (arr1,8); Printarr (arr1,8); cout<<endl;
int arr2[] = {2,2,2,2}; Printarr (arr2,4); Simpleselectionsort (arr2,4); Printarr (arr2,4); cout<<endl;
int arr3[] = {2,2,1,2}; Printarr (arr3,4); Simpleselectionsort (arr3,4); Printarr (arr3,4); cout<<endl;
int* arr4 = NULL; Printarr (arr4,4); Simpleselectionsort (arr4,4); Printarr (arr4,4); cout<<endl; } int main () { test (); System ("pause"); }

Simple selection sort (Easy Selection 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.