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