05-数组与冒泡排序:第一个数据结构与经典算法

【C语言学习笔记 05】数组与冒泡排序:第一个数据结构,第一个经典算法

变量只能一个一个地开,要管理 100 个成绩怎么办?数组是第一个"数据结构",冒泡排序是第一个"经典算法",这一篇把两者一起拿下。

一、数组的定义与使用

c 复制代码
#include <stdio.h>

int main()
{
    int num[5] = {0, 1, 2, 3, 4};   // 定义并初始化,长度 5
    num[2] = 22;                     // 修改下标为 2 的元素
    printf("%d", num[2]);            // 读取

    int a[3] = {0, 1, 0};
    printf("%d\n", a[1]);            // 输出 1
    a[1] = 2;                        // 给数组元素赋值
    printf("%d\n", a[1]);            // 输出 2
    return 0;
}

数组是一段连续的内存,存放同类型元素,用下标(索引)访问:
#mermaid-svg-Ft98W3ABArIRqhpm{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-Ft98W3ABArIRqhpm .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-Ft98W3ABArIRqhpm .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-Ft98W3ABArIRqhpm .error-icon{fill:#552222;}#mermaid-svg-Ft98W3ABArIRqhpm .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Ft98W3ABArIRqhpm .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-Ft98W3ABArIRqhpm .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Ft98W3ABArIRqhpm .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Ft98W3ABArIRqhpm .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-Ft98W3ABArIRqhpm .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Ft98W3ABArIRqhpm .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Ft98W3ABArIRqhpm .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Ft98W3ABArIRqhpm .marker.cross{stroke:#333333;}#mermaid-svg-Ft98W3ABArIRqhpm svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Ft98W3ABArIRqhpm p{margin:0;}#mermaid-svg-Ft98W3ABArIRqhpm .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-Ft98W3ABArIRqhpm .cluster-label text{fill:#333;}#mermaid-svg-Ft98W3ABArIRqhpm .cluster-label span{color:#333;}#mermaid-svg-Ft98W3ABArIRqhpm .cluster-label span p{background-color:transparent;}#mermaid-svg-Ft98W3ABArIRqhpm .label text,#mermaid-svg-Ft98W3ABArIRqhpm span{fill:#333;color:#333;}#mermaid-svg-Ft98W3ABArIRqhpm .node rect,#mermaid-svg-Ft98W3ABArIRqhpm .node circle,#mermaid-svg-Ft98W3ABArIRqhpm .node ellipse,#mermaid-svg-Ft98W3ABArIRqhpm .node polygon,#mermaid-svg-Ft98W3ABArIRqhpm .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-Ft98W3ABArIRqhpm .rough-node .label text,#mermaid-svg-Ft98W3ABArIRqhpm .node .label text,#mermaid-svg-Ft98W3ABArIRqhpm .image-shape .label,#mermaid-svg-Ft98W3ABArIRqhpm .icon-shape .label{text-anchor:middle;}#mermaid-svg-Ft98W3ABArIRqhpm .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-Ft98W3ABArIRqhpm .rough-node .label,#mermaid-svg-Ft98W3ABArIRqhpm .node .label,#mermaid-svg-Ft98W3ABArIRqhpm .image-shape .label,#mermaid-svg-Ft98W3ABArIRqhpm .icon-shape .label{text-align:center;}#mermaid-svg-Ft98W3ABArIRqhpm .node.clickable{cursor:pointer;}#mermaid-svg-Ft98W3ABArIRqhpm .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-Ft98W3ABArIRqhpm .arrowheadPath{fill:#333333;}#mermaid-svg-Ft98W3ABArIRqhpm .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-Ft98W3ABArIRqhpm .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-Ft98W3ABArIRqhpm .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Ft98W3ABArIRqhpm .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-Ft98W3ABArIRqhpm .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Ft98W3ABArIRqhpm .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-Ft98W3ABArIRqhpm .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-Ft98W3ABArIRqhpm .cluster text{fill:#333;}#mermaid-svg-Ft98W3ABArIRqhpm .cluster span{color:#333;}#mermaid-svg-Ft98W3ABArIRqhpm div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-Ft98W3ABArIRqhpm .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-Ft98W3ABArIRqhpm rect.text{fill:none;stroke-width:0;}#mermaid-svg-Ft98W3ABArIRqhpm .icon-shape,#mermaid-svg-Ft98W3ABArIRqhpm .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Ft98W3ABArIRqhpm .icon-shape p,#mermaid-svg-Ft98W3ABArIRqhpm .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-Ft98W3ABArIRqhpm .icon-shape .label rect,#mermaid-svg-Ft98W3ABArIRqhpm .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Ft98W3ABArIRqhpm .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-Ft98W3ABArIRqhpm .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-Ft98W3ABArIRqhpm :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 内存
num0
num1
num2
num3
num4
地址 0x1000
0x1004
0x1008
0x100C
0x1010

关键规则:

  1. 下标从 0 开始,长度为 n 的数组合法下标范围是 0 ~ n-1;
  2. 数组名本身代表这段内存的首地址------这就是下一篇指针的入口;
  3. 越界访问(如 num[5])编译器不报错,但属于未定义行为,可能改坏别的变量甚至崩溃。

用循环批量处理数组

c 复制代码
int aaa[4];
int i;
for (i = 0; i < 4; i++) {
    scanf("%d", &aaa[i]);           // 注意 &aaa[i],取元素的地址
}

数组 + for 循环是天生一对:for (i = 0; i < n; i++) 遍历所有元素。

二、字符数组与字符串

c 复制代码
#include <stdio.h>
#include <string.h>

int main()
{
    char aaa[30];
    strcpy(aaa, "chendu");
    printf("%s", aaa);
    return 0;
}

C 语言没有原生字符串类型,字符串就是以 '\0' 结尾的字符数组:
#mermaid-svg-cj1Tn3dElLPjj3q6{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-cj1Tn3dElLPjj3q6 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-cj1Tn3dElLPjj3q6 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-cj1Tn3dElLPjj3q6 .error-icon{fill:#552222;}#mermaid-svg-cj1Tn3dElLPjj3q6 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-cj1Tn3dElLPjj3q6 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-cj1Tn3dElLPjj3q6 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-cj1Tn3dElLPjj3q6 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-cj1Tn3dElLPjj3q6 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-cj1Tn3dElLPjj3q6 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-cj1Tn3dElLPjj3q6 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-cj1Tn3dElLPjj3q6 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-cj1Tn3dElLPjj3q6 .marker.cross{stroke:#333333;}#mermaid-svg-cj1Tn3dElLPjj3q6 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-cj1Tn3dElLPjj3q6 p{margin:0;}#mermaid-svg-cj1Tn3dElLPjj3q6 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-cj1Tn3dElLPjj3q6 .cluster-label text{fill:#333;}#mermaid-svg-cj1Tn3dElLPjj3q6 .cluster-label span{color:#333;}#mermaid-svg-cj1Tn3dElLPjj3q6 .cluster-label span p{background-color:transparent;}#mermaid-svg-cj1Tn3dElLPjj3q6 .label text,#mermaid-svg-cj1Tn3dElLPjj3q6 span{fill:#333;color:#333;}#mermaid-svg-cj1Tn3dElLPjj3q6 .node rect,#mermaid-svg-cj1Tn3dElLPjj3q6 .node circle,#mermaid-svg-cj1Tn3dElLPjj3q6 .node ellipse,#mermaid-svg-cj1Tn3dElLPjj3q6 .node polygon,#mermaid-svg-cj1Tn3dElLPjj3q6 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-cj1Tn3dElLPjj3q6 .rough-node .label text,#mermaid-svg-cj1Tn3dElLPjj3q6 .node .label text,#mermaid-svg-cj1Tn3dElLPjj3q6 .image-shape .label,#mermaid-svg-cj1Tn3dElLPjj3q6 .icon-shape .label{text-anchor:middle;}#mermaid-svg-cj1Tn3dElLPjj3q6 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-cj1Tn3dElLPjj3q6 .rough-node .label,#mermaid-svg-cj1Tn3dElLPjj3q6 .node .label,#mermaid-svg-cj1Tn3dElLPjj3q6 .image-shape .label,#mermaid-svg-cj1Tn3dElLPjj3q6 .icon-shape .label{text-align:center;}#mermaid-svg-cj1Tn3dElLPjj3q6 .node.clickable{cursor:pointer;}#mermaid-svg-cj1Tn3dElLPjj3q6 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-cj1Tn3dElLPjj3q6 .arrowheadPath{fill:#333333;}#mermaid-svg-cj1Tn3dElLPjj3q6 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-cj1Tn3dElLPjj3q6 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-cj1Tn3dElLPjj3q6 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-cj1Tn3dElLPjj3q6 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-cj1Tn3dElLPjj3q6 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-cj1Tn3dElLPjj3q6 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-cj1Tn3dElLPjj3q6 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-cj1Tn3dElLPjj3q6 .cluster text{fill:#333;}#mermaid-svg-cj1Tn3dElLPjj3q6 .cluster span{color:#333;}#mermaid-svg-cj1Tn3dElLPjj3q6 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-cj1Tn3dElLPjj3q6 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-cj1Tn3dElLPjj3q6 rect.text{fill:none;stroke-width:0;}#mermaid-svg-cj1Tn3dElLPjj3q6 .icon-shape,#mermaid-svg-cj1Tn3dElLPjj3q6 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-cj1Tn3dElLPjj3q6 .icon-shape p,#mermaid-svg-cj1Tn3dElLPjj3q6 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-cj1Tn3dElLPjj3q6 .icon-shape .label rect,#mermaid-svg-cj1Tn3dElLPjj3q6 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-cj1Tn3dElLPjj3q6 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-cj1Tn3dElLPjj3q6 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-cj1Tn3dElLPjj3q6 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} aaa30 的内存
'c'
'h'
'e'
'n'
'd'
'u'
'\\0'
未使用...

  • strcpy(目标, 源):把源字符串复制进目标数组(string.h)
  • printf("%s", ...) 从首地址开始打印,直到遇见 \0
  • 目标数组必须足够大,装得下内容 + \0,否则缓冲区溢出

常用字符串函数:strlen(求长度,不含 \0)、strcmp(比较)、strcat(拼接)。

三、冒泡排序

3.1 基础版本

c 复制代码
#include <stdio.h>

int main()
{
    int i = 0, j = 0, k = 0;
    int num[5] = {4, 3, 5, 7, 2};
    for (i = 0; i < 5 - 1; i++) {              // 一共进行 4 轮
        for (j = 0; j < 5 - i - 1; j++) {      // 每轮比较次数递减
            if (num[j] < num[j + 1]) {         // 后面的大就交换 → 降序
                int temp = num[j];             // 临时变量交换三步曲
                num[j] = num[j + 1];
                num[j + 1] = temp;
            }
        }
    }
    for (k = 0; k < 5; k++) {
        printf("%d ", num[k]);
    }
    return 0;
}

输出:7 5 4 3 2。把 < 改成 > 即升序。

3.2 原理图解

每一轮从头开始,相邻两两比较,不满足顺序就交换。一轮结束后,最值被"冒泡"到末端就位:
#mermaid-svg-5PsBPyTduBXBmSKm{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-5PsBPyTduBXBmSKm .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-5PsBPyTduBXBmSKm .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-5PsBPyTduBXBmSKm .error-icon{fill:#552222;}#mermaid-svg-5PsBPyTduBXBmSKm .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-5PsBPyTduBXBmSKm .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-5PsBPyTduBXBmSKm .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-5PsBPyTduBXBmSKm .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-5PsBPyTduBXBmSKm .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-5PsBPyTduBXBmSKm .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-5PsBPyTduBXBmSKm .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-5PsBPyTduBXBmSKm .marker{fill:#333333;stroke:#333333;}#mermaid-svg-5PsBPyTduBXBmSKm .marker.cross{stroke:#333333;}#mermaid-svg-5PsBPyTduBXBmSKm svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-5PsBPyTduBXBmSKm p{margin:0;}#mermaid-svg-5PsBPyTduBXBmSKm .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-5PsBPyTduBXBmSKm .cluster-label text{fill:#333;}#mermaid-svg-5PsBPyTduBXBmSKm .cluster-label span{color:#333;}#mermaid-svg-5PsBPyTduBXBmSKm .cluster-label span p{background-color:transparent;}#mermaid-svg-5PsBPyTduBXBmSKm .label text,#mermaid-svg-5PsBPyTduBXBmSKm span{fill:#333;color:#333;}#mermaid-svg-5PsBPyTduBXBmSKm .node rect,#mermaid-svg-5PsBPyTduBXBmSKm .node circle,#mermaid-svg-5PsBPyTduBXBmSKm .node ellipse,#mermaid-svg-5PsBPyTduBXBmSKm .node polygon,#mermaid-svg-5PsBPyTduBXBmSKm .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-5PsBPyTduBXBmSKm .rough-node .label text,#mermaid-svg-5PsBPyTduBXBmSKm .node .label text,#mermaid-svg-5PsBPyTduBXBmSKm .image-shape .label,#mermaid-svg-5PsBPyTduBXBmSKm .icon-shape .label{text-anchor:middle;}#mermaid-svg-5PsBPyTduBXBmSKm .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-5PsBPyTduBXBmSKm .rough-node .label,#mermaid-svg-5PsBPyTduBXBmSKm .node .label,#mermaid-svg-5PsBPyTduBXBmSKm .image-shape .label,#mermaid-svg-5PsBPyTduBXBmSKm .icon-shape .label{text-align:center;}#mermaid-svg-5PsBPyTduBXBmSKm .node.clickable{cursor:pointer;}#mermaid-svg-5PsBPyTduBXBmSKm .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-5PsBPyTduBXBmSKm .arrowheadPath{fill:#333333;}#mermaid-svg-5PsBPyTduBXBmSKm .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-5PsBPyTduBXBmSKm .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-5PsBPyTduBXBmSKm .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-5PsBPyTduBXBmSKm .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-5PsBPyTduBXBmSKm .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-5PsBPyTduBXBmSKm .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-5PsBPyTduBXBmSKm .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-5PsBPyTduBXBmSKm .cluster text{fill:#333;}#mermaid-svg-5PsBPyTduBXBmSKm .cluster span{color:#333;}#mermaid-svg-5PsBPyTduBXBmSKm div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-5PsBPyTduBXBmSKm .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-5PsBPyTduBXBmSKm rect.text{fill:none;stroke-width:0;}#mermaid-svg-5PsBPyTduBXBmSKm .icon-shape,#mermaid-svg-5PsBPyTduBXBmSKm .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-5PsBPyTduBXBmSKm .icon-shape p,#mermaid-svg-5PsBPyTduBXBmSKm .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-5PsBPyTduBXBmSKm .icon-shape .label rect,#mermaid-svg-5PsBPyTduBXBmSKm .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-5PsBPyTduBXBmSKm .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-5PsBPyTduBXBmSKm .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-5PsBPyTduBXBmSKm :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 第4轮
第3轮
第2轮
第1轮
4 3 5 7 2 → 比较4,3
3 4 5 7 2 → 比较4,5 不换
3 4 5 7 2 → 比较5,7 不换
3 4 5 2 7 → 7 到位 ✓
3 4 5 2 → 5 到位 ✓
3 4 2 → 4 到位 ✓
3 2 → 3 到位 ✓

双重循环的边界公式(n 个元素):

  • 外层:i 从 0 到 n-2,共 n-1 轮
  • 内层:j 从 0 到 n-i-2,第 i 轮比较 n-i-1 次(末尾 i 个已就位无需再比)

交换三步曲 正是第 01 篇的临时变量法:temp = a; a = b; b = temp;

3.3 完整交互版本

c 复制代码
#include <stdio.h>

int main()
{
    int aaa[4] = {0, 0, 0, 0};
    int i = 0;

    for (i = 0; i < 4; i++) {                  // 读入 4 个数
        scanf("%d", &aaa[i]);
    }

    int a = 0, b = 0;
    for (a = 0; a < 4 - 1; a++) {
        for (b = 0; b < 4 - a - 1; b++) {
            if (aaa[b] < aaa[b + 1]) {         // 降序
                int temp = aaa[b];
                aaa[b] = aaa[b + 1];
                aaa[b + 1] = temp;
            }
        }
    }

    for (i = 0; i < 4; i++) {
        printf("%d ", aaa[i]);
    }
    return 0;
}

3.4 复杂度与优化

  • 比较总次数:(n-1) + (n-2) + ... + 1 = n(n-1)/2,时间复杂度 O(n²)
  • 优化:若某一轮一次交换都没发生,说明已经有序,可提前结束:
c 复制代码
for (i = 0; i < n - 1; i++) {
    int swapped = 0;
    for (j = 0; j < n - i - 1; j++) {
        if (num[j] < num[j + 1]) {
            /* 交换 */
            swapped = 1;
        }
    }
    if (!swapped) break;      // 本轮无交换 → 已有序
}

最好情况(已有序)优化后为 O(n),最坏情况仍是 O(n²)。

四、小结

  • 数组 = 连续内存 + 下标访问;下标 0 起,越界不报错但极危险
  • 字符串 = 字符数组 + '\0' 结尾;strcpy/strlen/strcmp 在 <string.h>
  • 冒泡排序:n-1 轮,每轮把最值冒泡到末端;复杂度 O(n²)
  • 交换元素三步曲 + 提前退出优化,是通用技巧

下一篇:函数------把代码封装成可复用的积木。

相关推荐
水饺编程1 小时前
第2章:Linux 基础,mkdir 命令的简单入门
linux·c语言·汇编
鸽鸽程序猿1 小时前
【刷题册】hot21-25
算法
Keven_111 小时前
算法札记:记忆化搜索中面对多组数据的初始化
算法
happyprince1 小时前
02-具体观 · JevAny算法与实现剖析
人工智能·算法
Nil2081 小时前
leetcode 75颜色分类
数据结构·算法·leetcode
Liaiyang662 小时前
空圈容错视角 · 多维切面篇:我们生活的世界,不止三维
人工智能·算法·系统架构·前沿技术·哲学·认知科学
闻缺陷则喜何志丹2 小时前
【环】P6709 [CCC 2020] Swapping Seats 环|普及+
算法·洛谷·环
燕落南枝2 小时前
C 语言学习大总结:从入门到进阶的完整知识体系
服务器·c语言·学习
开开心心就好2 小时前
打印机卡住打不了?一键清理卡死打印队列
服务器·开发语言·算法·pdf·ocr·idea·visual studio code