C语言:10000个随机整数(<100)的选择排序

来源:百度知道 编辑:UC知道 时间:2024/06/01 20:37:09

楼上的,人家明明是C语言,C语言根本没有模板的

下面是基于整数的排序:

#include <stdio.h>
#include <stdlib.h>

void selectSort(int a[],int n)
{
int i,j,min,temp;
for(i = 0;i<n;i++)
{
min = i;
for(j = i+1;j<n;j++)
if(a[j]<a[min])
min = j;
temp = a[min];
a[min] = a[i];
a[i] = a[min];
}
}
int main()
{
int a[10000],i;
for(i = 0;i<10000;i++)
a[i] = rand()%100;
selectSort(a,10000);
for(i=0;i<10000;i++)
printf("%d ",a[i]);
return 0;
}

template<class T>
void SelectSort(T a[],int n)
{
bool sorted=false;
for(int size=n;size>1 && !sorted;size--)
{
int pos=0;
sorted=true;
for(int i=1;i<size;i++)
{
if(a[pos]<=a[i])
{
pos=i;
}
else
{
sorted=false;
}
}
Swap(a[pos