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