iOS algorithm (ii) selection of sorting

Source: Internet
Author: User

 Select Sort:

each trip selects the smallest (or largest) element from the data element to be sorted, placed in the final order of the ordered sequence, until all the data elements are sorted out. Select Sort is an unstable sort method.

I. Algorithm description

Select sort: For example, in an unordered array of length n, traversing n data on the first pass, finding the smallest of the values to be exchanged with the first element, and the second traversing the remaining N-1 data to find the smallest value exchanged with the second element ... Section N-1 traverses the remaining 2 data to find out the smallest of the values exchanged with the N-1 element, so that the selection is complete.

Take the following 5 unordered data as an example:

56 12 80 91 20 (the selection process of the first trip is only refined)

1th Trip: 12 56 80 91 20


2nd Trip: 12 20 80 91 56

3rd Trip: 12 20 56 91 80

4th Trip: 12 20 56 80 91

 Code implementation:

Main.m

algorithm ---- Select sort

Created by Lanouhn on 14-9-16.

Copyright (c) year [email protected] All rights reserved.


#import<Foundation/Foundation.h>

int Main (int argc,const Char * argv[])

{

int array[] = {2, 6, 9, 8, 5, 7 , 1, 4};

// in order to increase portability ( take sizeof ()) to calculate the number of array elements count

int count = sizeof(array)/sizeof(array[0]);

//

for (int i = 0; i < count-1; i++) { // Compare the number of trips

int minindex = i; // Find minimum Value

for (int j = minindex +1; J < Count; J + +) {

if (Array[minindex] > Array[j]) {

Minindex = j;

}

}

// If there is no comparison to the last remaining number , then perform the following operation

if (Minindex! = i) {

// Exchanging Data

int temp = 0;

temp = Array[i];

Array[i] = Array[minindex];

Array[minindex] = temp;

}

}

for (int i = 0; i < count; i++) {

printf ("[%2d]:%d\n", I, array[i]);

}

return 0;

}




iOS algorithm (ii) selection of 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.