简单选择排序(Simple Selection Sort):相比较冒泡排序,每次都是两两比较交换,n个元素n-1次比较可以确定1个元素的最终位置。简单选择排序法就是通过n-i次关键字的比较,从n-i+1个记录中选出关键字最小的记录,并和第i(1≤i≤n)个记录交换。
#include<iostream>
using namespace std;
//简单选择排序
int simpleSelectionSort(int* arr,int length);
void swap(int& elem1,int& elem2);
void test();
void printArr(int* arr,int length);
void swap(int& elem1,int& elem2)
{
int tmp = elem1;
elem1 = elem2;
elem2 = tmp;
}
int simpleSelectionSort(int* arr,int length)
{
if(NULL==arr||length<=0)
return -1;
int minPos = 0;
for(int idx=0;idx!=length;++idx)
{
minPos = idx;
for(int iidx=idx+1;iidx<length;++iidx)
{
if(arr[iidx]<arr[minPos])
{
minPos = iidx;
}
}
if(idx!=minPos)
{
swap(arr[idx],arr[minPos]);
}
}
return 0;
}
|
void printArr(int* arr,int length)
{
if(NULL==arr||length<=0)
return ;
for(int idx=0;idx!=length;++idx)
{
cout<<arr[idx]<<" ";
}
cout<<endl;
}
void test()
{
int arr[] = {6,5,3,1,8,7,2,4};
printArr(arr,8);
simpleSelectionSort(arr,8);
printArr(arr,8);
cout<<endl;
int arr1[] = {1,2,3,4,5,6,7,8};
printArr(arr1,8);
simpleSelectionSort(arr1,8);
printArr(arr1,8);
cout<<endl;
int arr2[] = {2,2,2,2};
printArr(arr2,4);
simpleSelectionSort(arr2,4);
printArr(arr2,4);
cout<<endl;
int arr3[] = {2,2,1,2};
printArr(arr3,4);
simpleSelectionSort(arr3,4);
printArr(arr3,4);
cout<<endl;
int* arr4 = NULL;
printArr(arr4,4);
simpleSelectionSort(arr4,4);
printArr(arr4,4);
cout<<endl;
}
int main()
{
test();
system("pause");
}
|