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.
Htu1/简单选择排序

38 lines
1.1 KiB

//简单选择排序
#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