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 order of the last sequence of the ordered sequence. Until all the data elements are sorted out.

Select Sort is an unstable sort method.

I. Algorithmic descriptive narrative

Select sort: For example, in an unordered array of length n. In the first trip to traverse N data, find the smallest value in exchange with the first element, the second traversal of the remaining N-1 data, find the smallest of the values and the second element exchange ... N-1 traverses the remaining 2 data to find out the smallest value exchanged with the first N-1 element. The selection is now 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 add portability ( take sizeof ()), count the number of elements in the array

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

//

for (int i = 0; i < count-1; i++) { // A comparison of 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;

}

}

// Assuming there is no comparison to the last remaining number , then run 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.