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