Selection Sort Selection Sorting
1. Simple and intuitive
2, in the array, find the smallest value, and the first element exchange (if the first element is the smallest element then swap with yourself). Again, find the smallest element in the remaining element and swap it with the second element of the array. In turn.
Here's a look at pseudo-code
Selectionsort (mylist) {
For (I=0;i<len (mylist); i++) {
Min=i;
For (J=i+1;j<len (mylist); j + +) {
if (Mylist[min]>mylist[j]) {
If the value of mylist[min] is greater than mylist[j], then the interchange min and J
Min=j
}
}
Inner loop complete, swap
temp = Mylist[i];
Mylist[i] = Mylist[min];
Mylist[min] = Temp
}
}
#!/usr/bin/env python#-*-coding:utf-8-*-def selection (mylist): For I in range (0, Len (mylist)): min = i For j in range (i + 1, len (mylist)): if MYLIST[J] < Mylist[min]: Min = j Mylist[i], my List[min] = Mylist[min], mylist[i] return mylista = [10, 2, 3, 1, 23, 14, 25, 68, 72, 81, 33, 56, 98, 102, 77, 56, 7, 4 , 5]print (Selection (a))
This article is from the "coding" blog, make sure to keep this source http://xtceetg.blog.51cto.com/5086648/1933331
Selection Sort Selection Sorting