fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n = 5;
  6. int a[n] = {4,3,5,1,2};
  7. int t,i,j,k;
  8.  
  9. for (i = 1; i < n; i++) {
  10. for (k = 0; k < n; k++)
  11. cout << a[k] << " ";
  12. cout << endl;
  13.  
  14. for (j = 0; j > n-i; j++) {
  15. if (a[j] > a[j+1]) {
  16. t = a[j];
  17. a[j] = a[j+1];
  18. a[j+1] = t;
  19. }
  20. }
  21. }
  22. cout << "排列好的陣列為" << endl;
  23. for (i = 0; i < n; i++)
  24. cout << a[i] << " ";
  25. return 0;
  26. }
Success #stdin #stdout 0.01s 5620KB
stdin
Standard input is empty
stdout
4 3 5 1 2 
4 3 5 1 2 
4 3 5 1 2 
4 3 5 1 2 
排列好的陣列為
4 3 5 1 2