#include <iostream>
using namespace std;
template <typename T>
//冒泡排序
int bubbleSort(T *const t, int len)
{
for(int i = len - 1; i > 0; i--)
{
for(int j = 0 ; j < i; j++)
{
if(t[j] > t[j + 1])
{
T temp = t[j];
t[j] = t[j + 1];
t[j + 1] = temp;
}
}
}
return 0;
}
//插入排序
template <typename T>
int insertSort(T *const t, int len)
{
for(int i = 1; i < len; i++)
{
for(int j = i; j >= 1; j--)
{
if(t[j] < t[j - 1])
{
T temp = t[j - 1];
t[j - 1] = t[j];
t[j] = temp;
}else
{
break;
}
}
}
}
template <typename T>
int printArray(T *const t, int len)
{
for(int i = 0; i < len; i++)
{
cout << t[i] << ';';
}
cout<<endl;
}
int main(int argc, char *argv[])
{
int c[10] = {9,4,7,6,11,4,3,2,1,0};
// bubbleSort(c, sizeof(c)/sizeof(int));
insertSort(c, sizeof(c)/sizeof(int));
printArray(c, sizeof(c)/sizeof(int));
return 0;
}
冒泡排序和插入排序
最新推荐文章于 2025-07-09 23:17:20 发布