网上看到的排序源码, 准备面试的朋友可以看看
//选择排序
template <typename T>
void SelectionSort(T arr[],int n)
{
int smallIndex;
int pass,j;
T temp;
for (pass=0;pass<n-1;pass++)
{
smallIndex=pass; 
for(j=pass+1;j<n;j++)
{
if(arr[j]<arr[smallIndex])
smallIndex=j;
}
if(smallIndex!=pass)
{
temp=arr[pass];
arr[pass]=arr[smallIndex];
arr[smallIndex]=temp;
}
}
}
//冒泡排序
template <typename T>
int bubbleSortEx(T arr[],int n)
{
int i,pass;
T temp; 
for (pass=0;pass<n;pass++)
{
for (i=0;i<n-pass;i++)
{
if (arr[i]>arr[i+1])
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
}
return pass;
} 
posted on 2005-05-11 10:48 hunter_gio 阅读(520) 评论(0) 收藏 举报


浙公网安备 33010602011771号