#include<string>
#include<iostream>
#include<cstring>
#include<head.h>
using namespace std;
//冒泡排序算法
template<typename T>
void bubbleSort(int *,int);
int main()
{
int arr[] = {7,1,6,4,5,2,8,10,3,9,11,12};
int length = sizeof(arr)/sizeof(int);
cout << "原来的序列:" << endl;
for(auto var : arr)
{
cout << var << " ";
}
bubbleSort(arr,length);
cout << "\n现在的序列:" << endl;
for(auto var : arr)
{
cout << var << " ";
}
return 0;
}
template<typename T>
void bubbleSort(T *arr,int length)
{
for(int ix=0;ix<length-1;++ix)
{
for(int jy=ix+1;jy<length;++jy)
{
if(arr[ix] > arr[jy])
{
int temp = arr[ix];
arr[ix] = arr[jy];
arr[jy] = temp;
}
}
}
}
输出:
排序前:
7 1 6 4 5 2 8 10 3 9 11 12
排序后:
1 2 3 4 5 6 7 8 9 10 11 12
|