You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
//简单选择排序
|
|
|
|
|
#include <iostream>
|
|
|
|
|
using namespace std;
|
|
|
|
|
void selectSort(int r[], int n){
|
|
|
|
|
int i,index,j;
|
|
|
|
|
for(i=1; i<n; i++)
|
|
|
|
|
{
|
|
|
|
|
index = i;
|
|
|
|
|
for(j=i+1; j<n; j++)
|
|
|
|
|
{
|
|
|
|
|
if(r[index] > r[j])
|
|
|
|
|
{
|
|
|
|
|
index = j;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(index != i)
|
|
|
|
|
{
|
|
|
|
|
int temp = r[index];
|
|
|
|
|
r[index] = r[i];
|
|
|
|
|
r[i] = temp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
int array[]={4,6,2,8,12,55,3,1,0};
|
|
|
|
|
int n = sizeof(array)/sizeof(array[0]);
|
|
|
|
|
selectSort(array, n);
|
|
|
|
|
for(int i=1; i<n; i++)
|
|
|
|
|
{
|
|
|
|
|
cout<<array[i]<<" ";
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
//运行原理:选出最小(或者最大)的一个数与第1个位置的数交换;然后在剩下的数当中再找最小(或者最大)的与第2个位置的数交换,依次类推,直到第n-1个元素(倒数第二个数)和第n个元素(最后一个数)比较为止。
|
|
|
|
|
//运行结果:0 1 2 3 6 8 12 55
|
|
|
|
|
--------------------------------
|
|
|
|
|
Process exited after 0.3569 seconds with return value 0
|