|
|
//选择排序
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;
}

|
|