#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

template <typename T>
void deSelSort(T arr[],int n)


{//双端选择排序
int min,max;
for (int i=0,j=n-1;i<j;++i,--j)

{
min = i;
max = j;
if (arr[min]>arr[max])

{//确保两个端点处有序
MySwap(arr[min],arr[max]);
}
for (int m=i+1;m<j;++m)

{
if (arr[m]>arr[max])

{
max = m;
}
else if(arr[m]<arr[min])

{
min = m;
}
}
//交换最小值和最大值到合适的位置
MySwap(arr[min],arr[i]);
MySwap(arr[max],arr[j]);
}

}
template <typename T>
void bubbleSort(T arr[] ,int n)


{//冒泡排序
bool isSwaped;
for (int i=n-1;i>=1;--i)

{
isSwaped = false;
for (int j=0;j<=i;++j)

{
if (arr[j]>arr[j+1])

{
isSwaped = true;
MySwap(arr[j],arr[j+1]);
}
}
if (isSwaped==false)

{//没有交换,停止排序
break;
}
}
}
template <typename T>
void MySwap(T& a,T& b)


{//交换两个值
T temp;
temp = a;
a = b;
b = temp;
}
template<typename T>
void printArray(T arr[],int n)


{//输出列表
for (int i=0;i<n;++i)

{
cout<<arr[i]<<'\t';
}
cout<<endl;
}
bool isPal(const string& str,int start,int end)


{//判断是否是回文
if (start>=end-1)

{
return true;
}
else if (str[start]!=str[end-1])

{
return false;
}
else
return isPal(str,start+1,end-1);
}
void dec2bin(int n,int length)


{//输出整数n为固定长度length的进制数字
if (length==1)

{
cout<<"0";
return;
}
dec2bin(n/2,length-1);
cout<<n%2;

}
template<typename T>
void generateArray(T arr[],int n)


{//产生随即数数组

srand((unsigned)time(NULL));
for (int i=0;i<n;++i)

{
arr[i] = rand()%1000;
}
}
int main(void)


{

int a[] =
{8,7,6,5,4,3,2,1};
cout<<"排序前:";
printArray(a,8);
deSelSort(a,8);
//bubbleSort(a,8);
cout<<"排序后:";
printArray(a,8);
string str;
cout<<"是否是回文";
cin>>str;
cout<<isPal(str,0,str.length())<<endl;
int num,len;
cout<<"输入数:";
cin>>num>>len;
dec2bin(num,len);
cout<<"产生随机数数组:"<<endl;
int b[10];
generateArray(b,10);
bubbleSort(b,10);
printArray(b,10);
return 0;
}

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述