本帖最後由 tonyh 於 2016-1-15 18:14 編輯
利用選擇排序法, 將任意6個整數, 由小而大排列出來.
- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- int n[6]={12,57,-6,-32,0,23};
- int tmp;
- cout<<"排序前: ";
- for(int i=0; i<6; i++)
- cout<<n[i]<<" ";
- cout<<endl;
- for(int i=0; i<5; i++)
- {
- for(int j=i+1; j<6; j++)
- {
- if(n[j]<n[i])
- {
- tmp=n[j];
- n[j]=n[i];
- n[i]=tmp;
- }
- }
- }
- cout<<"排序後: ";
- for(int i=0; i<6; i++)
- cout<<n[i]<<" ";
- cout<<endl;
- system("pause");
- return 0;
- }
複製代碼 |