01.mysort.cpp 568 B

1234567891011121314151617181920212223242526
  1. #include <algorithm>
  2. #include <stdio.h>
  3. #define SIZEOF(T) (sizeof(T)) / (sizeof(T[0]))
  4. void print_arr(int *arr, int n);
  5. void bubbleSort(int *arr, int n) {
  6. for (int i = 0; i < n - 1; i++)
  7. for (int j = 0; j < n - i - 1; j++)
  8. if (arr[j] > arr[j + 1])
  9. std::swap(arr[j], arr[j + 1]);
  10. }
  11. int main() {
  12. int arr[] = {9, 2, 5, 7, 3, 1, 8, 4, 6};
  13. int n = SIZEOF(arr);
  14. bubbleSort(arr, n);
  15. print_arr(arr, n);
  16. return 0;
  17. }
  18. void print_arr(int *arr, int n) {
  19. printf("排序结果\n");
  20. for (int i = 0; i != n; i++) {
  21. printf("%d ", arr[i]);
  22. }
  23. }