函式模板實現氣泡排序
阿新 • • 發佈:2019-02-03
#include<iostream> using namespace std; template<class T> void Bubble(T* array,int size) { T temp; for(int i=0;i<size;i++) { for(int j=0;j<size-i;j++) { if(*(array+j)>*(array+j+1))//交換 { temp=*(array+j); *(array+j)=*(array+j+1); *(array+j+1)=temp; } } } } int main() { int a[10]={2,45,67,32,24,3456,-98,54,-667,0}; double b[8]={2.345,5.111,-99.1234,32.1,3.1415926,-45.0,1.0,456.123}; Bubble(a,10);//將整數陣列排序 for(int i=0;i<10;i++) { cout<<" "<<a[i]; } cout<<endl; Bubble(b,8);//將double型陣列排序 for(i=0;i<8;i++) { cout<<" "<<b[i]; } cout<<endl; return 0; }
<img src="https://img-blog.csdn.net/20150617140226581?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2VpY2hhbmp1YW4z/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />