fork download
  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. {
  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.  
  23. cout << "排序好的陣列為" <<endl;
  24. for (i = 0; i < n; i++)
  25. cout << a [i] << " ";
  26.  
  27. return 0;
  28. }
  29. // your code goes here
  30. return 0;
  31. }
Success #stdin #stdout 0s 4184KB
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