? 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