Deep introduction to sorting algorithms-select sorting

Source: Internet
Author: User

# Include <iostream>


/* Select sorting

 

Basic Idea: Find the smallest one from the back and put it to the last one sorted in the front.

Features: time complexity O (N ^ 2)

*/

Void selectsort (INT array [], int N ){

Int I, J;

Int temp = 0, flag = 0;

For (I = 0; I <n-1; I ++ ){

Temp = array [I];

Flag = I;

For (j = I + 1; j <n; j ++ ){

If (array [J] <temp) {// retrieve from I + 1 ~ The minimum one of N-1 is put into temp.

Temp = array [J];

Flag = J;

}

}

// Avoid assigning values. In extreme cases, values 1, 2, 3, 4, and 5 are ordered.

If (flag! = I ){

Array [flag] = array [I]; // place the value of a [I] to flag

Array [I] = temp; // the value of a [I] is equal to I + 1 ~ N-1)

}

}

}


Int main (INT argc, const char * argv [])

{

Int I = 0;

Int A [] = };

Int Len = sizeof (a)/sizeof (A [0]);

// Select sorting

Selectsort (A, Len );


For (I = 0; I <Len; I ++ ){

Printf ("% d", a [I]);

}

Printf ("\ n ");

Return0;

}

Deep introduction to sorting algorithms-select sorting

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.