zoukankan      html  css  js  c++  java
  • C语言冒泡排序

     1 #include <stdio.h>
     2 
     3 void bubble_sort(int arr[], int len);
     4 
     5 void main() {
     6     int arr[] = {5, 5, 6, 9, 10, 1, 0, 3, 2};
     7 
     8     int total_size = sizeof(arr);
     9 
    10     // 数组元素个数计算:总占用字节数 / 单个int占用字节数
    11     int len = total_size / sizeof(int);
    12 
    13     bubble_sort(arr, len);
    14 
    15     printf("after...
    ");
    16 
    17     for (int i = 0; i < len; i ++) {
    18         printf("%d
    ", arr[i]);
    19     }
    20 }
    21 
    22 void bubble_sort(int arr[], int len) {
    23     // 调用此function arr传的是地址,这里sizeof(arr) == 4
    24     // printf("%d
    ", sizeof(arr)); 
    25     for (int i = 0; i <= len - 1; i ++) {
    26         for (int j = 0; j < len - i - 1; j ++) {
    27             if (arr[j] > arr[j + 1]) {
    28                 int tmp = arr[j + 1];
    29                 arr[j + 1] = arr[j];
    30                 arr[j] = tmp;
    31             }
    32         }
    33     }
    34 }
  • 相关阅读:
    多线程
    集合与文件操作
    Net基础复习
    form表单
    html的常用标签和属性
    C#泛型与linq
    2020 年度总结 & OI 生涯感想——当年酒狂自负
    TODO-List
    Attention Points
    THUWC2020 游记
  • 原文地址:https://www.cnblogs.com/Joynic/p/13721189.html
Copyright © 2011-2022 走看看