摘要: 本文描述一道冒泡排序编程题,要求根据输入的 N 和 K,输出第 K 遍扫描后的中间结果数列,并给出了完整的输入输出格式说明与 C 语言代码实现。
题目描述
将N个整数按从小到大排序的冒泡排序法是这样工作的:从头到尾比较相邻两个元素,如果前面的元素大于其紧随的后面元素,则交换它们。通过一遍扫描,则最后一个元素必定是最大的元素。然后用同样的方法对前N−1个元素进行第二遍扫描。依此类推,最后只需处理两个元素,就完成了对N个数的排序。
本题要求对任意给定的 K(<N),输出扫描完第 K 遍后的中间结果数列。
#mermaid-svg-NbOlU4qtcVbZlMIP{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-NbOlU4qtcVbZlMIP .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-NbOlU4qtcVbZlMIP .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-NbOlU4qtcVbZlMIP .error-icon{fill:#552222;}#mermaid-svg-NbOlU4qtcVbZlMIP .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-NbOlU4qtcVbZlMIP .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-NbOlU4qtcVbZlMIP .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-NbOlU4qtcVbZlMIP .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-NbOlU4qtcVbZlMIP .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-NbOlU4qtcVbZlMIP .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-NbOlU4qtcVbZlMIP .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-NbOlU4qtcVbZlMIP .marker{fill:#333333;stroke:#333333;}#mermaid-svg-NbOlU4qtcVbZlMIP .marker.cross{stroke:#333333;}#mermaid-svg-NbOlU4qtcVbZlMIP svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-NbOlU4qtcVbZlMIP p{margin:0;}#mermaid-svg-NbOlU4qtcVbZlMIP .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-NbOlU4qtcVbZlMIP .cluster-label text{fill:#333;}#mermaid-svg-NbOlU4qtcVbZlMIP .cluster-label span{color:#333;}#mermaid-svg-NbOlU4qtcVbZlMIP .cluster-label span p{background-color:transparent;}#mermaid-svg-NbOlU4qtcVbZlMIP .label text,#mermaid-svg-NbOlU4qtcVbZlMIP span{fill:#333;color:#333;}#mermaid-svg-NbOlU4qtcVbZlMIP .node rect,#mermaid-svg-NbOlU4qtcVbZlMIP .node circle,#mermaid-svg-NbOlU4qtcVbZlMIP .node ellipse,#mermaid-svg-NbOlU4qtcVbZlMIP .node polygon,#mermaid-svg-NbOlU4qtcVbZlMIP .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-NbOlU4qtcVbZlMIP .rough-node .label text,#mermaid-svg-NbOlU4qtcVbZlMIP .node .label text,#mermaid-svg-NbOlU4qtcVbZlMIP .image-shape .label,#mermaid-svg-NbOlU4qtcVbZlMIP .icon-shape .label{text-anchor:middle;}#mermaid-svg-NbOlU4qtcVbZlMIP .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-NbOlU4qtcVbZlMIP .rough-node .label,#mermaid-svg-NbOlU4qtcVbZlMIP .node .label,#mermaid-svg-NbOlU4qtcVbZlMIP .image-shape .label,#mermaid-svg-NbOlU4qtcVbZlMIP .icon-shape .label{text-align:center;}#mermaid-svg-NbOlU4qtcVbZlMIP .node.clickable{cursor:pointer;}#mermaid-svg-NbOlU4qtcVbZlMIP .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-NbOlU4qtcVbZlMIP .arrowheadPath{fill:#333333;}#mermaid-svg-NbOlU4qtcVbZlMIP .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-NbOlU4qtcVbZlMIP .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-NbOlU4qtcVbZlMIP .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-NbOlU4qtcVbZlMIP .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-NbOlU4qtcVbZlMIP .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-NbOlU4qtcVbZlMIP .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-NbOlU4qtcVbZlMIP .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-NbOlU4qtcVbZlMIP .cluster text{fill:#333;}#mermaid-svg-NbOlU4qtcVbZlMIP .cluster span{color:#333;}#mermaid-svg-NbOlU4qtcVbZlMIP 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-NbOlU4qtcVbZlMIP .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-NbOlU4qtcVbZlMIP rect.text{fill:none;stroke-width:0;}#mermaid-svg-NbOlU4qtcVbZlMIP .icon-shape,#mermaid-svg-NbOlU4qtcVbZlMIP .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-NbOlU4qtcVbZlMIP .icon-shape p,#mermaid-svg-NbOlU4qtcVbZlMIP .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-NbOlU4qtcVbZlMIP .icon-shape .label rect,#mermaid-svg-NbOlU4qtcVbZlMIP .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-NbOlU4qtcVbZlMIP .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-NbOlU4qtcVbZlMIP .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-NbOlU4qtcVbZlMIP :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 是
否
否
是
否
是
开始
输入 N 和 K
读取 N 个整数到数组 a
外层循环 i = 0 到 K-1
内层循环 j = 0 到 N-2-i
aj > aj+1?
交换 aj 与 aj+1
继续下一对比较
j 循环结束?
i 循环结束?
按格式输出数组
结束
输入格式:
输入在第 1 行中给出 N 和 K(1 ≤ K < N ≤ 100),在第 2 行中给出 N 个待排序的整数,数字间以空格分隔。
输出格式:
在一行中输出冒泡排序法扫描完第 K 遍后的中间结果数列,数字间以空格分隔,但末尾不得有多余空格。
输入样例:
in
6 2
2 3 5 1 6 4
输出样例:
out
2 1 3 4 5 6
代码部分实现
c
#include <stdio.h> // 引入标准输入输出头文件
int main() {
int n, k; // n为待排序整数的总数,k为需要输出的排序遍数
scanf("%d %d", &n, &k); // 读入n和k
int a[100]; // 定义数组存储待排序的整数
// 读入n个待排序的整数
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
// 冒泡排序,进行k趟扫描
for (int i = 0; i < k; i++) {
// 第i趟排序,比较相邻元素并交换,将最大元素"冒泡"到末尾
for (int j = 0; j < n - 1 - i; j++) {
if (a[j] > a[j + 1]) { // 如果前一个元素大于后一个元素
int temp = a[j]; // 交换两个元素
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
// 输出经过k趟排序后的数组
for (int i = 0; i < n; i++) {
if (i > 0) printf(" "); // 除第一个元素外,其余元素前输出空格分隔
printf("%d", a[i]);
}
return 0; // 程序正常退出
}