选择排序

include

using namespace std;
//Function prototype
int* selectSort(int ,int);
void showArray(const int [],int);
int main()
{
int array[] = {7,2,4,5,9,10};
int size = sizeof(array)/sizeof(array[0]);
showArray(array,size);
selectSort(array,size);
showArray(array, size);
cout <<array<< endl;
return 0;
}
int
selectSort(int *p,int size)
{
int startScan, minIndex, minValue;
for (startScan = 0;startScan<(size-1);startScan++)
{
minIndex = startScan;
minValue = (p+startScan);
for (int index = startScan+1;index<size;index++)
{
if (
(p+index)<minValue)
{
minValue = *(p + index);
minIndex = index;
}

	}
	*(p + minIndex) = *(p + startScan);
	*(p + startScan) = minValue;
}
return p;

}
void showArray(const int array[], int size)
{
for (int count=0;count<size;count++)
{
cout << array[count] <<" ";
}
cout << endl;
}

posted on 2019-10-22 20:16  华1  阅读(100)  评论(0编辑  收藏  举报