C++ Exercises(七)

本文介绍了一种名为双端选择排序的算法,并展示了其在C++中的实现方式。此外,还介绍了冒泡排序算法及其实现,并提供了字符串回文判断、十进制转二进制及随机数组生成等实用函数。
None.gif#include <iostream>
None.gif#include 
<string>
None.gif#include 
<cstdlib>
None.gif#include 
<ctime>
None.gif
using namespace std;
None.gif
None.giftemplate 
<typename T>
None.gif
void deSelSort(T arr[],int n)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{//双端选择排序
InBlock.gif
    int min,max;
InBlock.gif    
for (int i=0,j=n-1;i<j;++i,--j)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        min 
= i;
InBlock.gif        max 
= j;
InBlock.gif        
if (arr[min]>arr[max])
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{//确保两个端点处有序
InBlock.gif
            MySwap(arr[min],arr[max]);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
for (int m=i+1;m<j;++m)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (arr[m]>arr[max])
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                max 
= m;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if(arr[m]<arr[min])
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                min 
= m;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
//交换最小值和最大值到合适的位置
InBlock.gif
        MySwap(arr[min],arr[i]);
InBlock.gif        MySwap(arr[max],arr[j]);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.giftemplate 
<typename T>
None.gif
void bubbleSort(T arr[] ,int n)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{//冒泡排序
InBlock.gif
    bool isSwaped;
InBlock.gif    
for (int i=n-1;i>=1;--i)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        isSwaped 
= false;
InBlock.gif        
for (int j=0;j<=i;++j)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (arr[j]>arr[j+1])
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                isSwaped 
= true;
InBlock.gif                MySwap(arr[j],arr[j
+1]);
InBlock.gif                
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
if (isSwaped==false)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{//没有交换,停止排序
InBlock.gif
            break;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.giftemplate 
<typename T>
None.gif
void MySwap(T& a,T& b)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{//交换两个值
InBlock.gif
    T temp;
InBlock.gif    temp 
= a;
InBlock.gif    a 
= b;
InBlock.gif    b 
= temp;
ExpandedBlockEnd.gif}

None.giftemplate
<typename T>
None.gif
void printArray(T arr[],int n)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{//输出列表
InBlock.gif
    for (int i=0;i<n;++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        cout
<<arr[i]<<'\t';
ExpandedSubBlockEnd.gif    }

InBlock.gif    cout
<<endl;
ExpandedBlockEnd.gif}

None.gif
bool isPal(const string& str,int start,int end)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{//判断是否是回文
InBlock.gif
    if (start>=end-1)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return true;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else if (str[start]!=str[end-1])
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return false;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
InBlock.gif        
return isPal(str,start+1,end-1);
ExpandedBlockEnd.gif}

None.gif
void dec2bin(int n,int length)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{//输出整数n为固定长度length的进制数字
InBlock.gif
    if (length==1)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        cout
<<"0";
InBlock.gif        
return;
ExpandedSubBlockEnd.gif    }

InBlock.gif    dec2bin(n
/2,length-1);
InBlock.gif    cout
<<n%2;
InBlock.gif
ExpandedBlockEnd.gif}

None.giftemplate
<typename T>
None.gif
void generateArray(T arr[],int n)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{//产生随即数数组
InBlock.gif

InBlock.gif    srand((unsigned)time(NULL));
InBlock.gif    
for (int i=0;i<n;++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        arr[i] 
= rand()%1000;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

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

None.gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值