fork download
  1. #include <stdio.h>
  2.  
  3. #define N 20
  4.  
  5. int main()
  6. {
  7. int a[N] = {
  8. 45, 12, 78, 3, 56,
  9. 29, 91, 17, 64, 8,
  10. 33, 70, 1, 52, 86,
  11. 24, 39, 95, 11, 60
  12. };
  13.  
  14. int i, j, temp;
  15.  
  16. printf("排序前的数据:\n");
  17. for(i = 0; i < N; i++)
  18. printf("%d ", a[i]);
  19.  
  20. // 冒泡排序
  21. for(i = 0; i < N - 1; i++)
  22. {
  23. for(j = 0; j < N - 1 - i; j++)
  24. {
  25. if(a[j] > a[j + 1])
  26. {
  27. temp = a[j];
  28. a[j] = a[j + 1];
  29. a[j + 1] = temp;
  30. }
  31. }
  32. }
  33.  
  34. printf("\n\n冒泡排序后的数据:\n");
  35. for(i = 0; i < N; i++)
  36. printf("%d ", a[i]);
  37.  
  38. return 0;
  39. }
Success #stdin #stdout 0s 5336KB
stdin
Standard input is empty
stdout
排序前的数据:
45 12 78 3 56 29 91 17 64 8 33 70 1 52 86 24 39 95 11 60 

冒泡排序后的数据:
1 3 8 11 12 17 24 29 33 39 45 52 56 60 64 70 78 86 91 95