6-4 冒泡排序 (10 分)
 

编程实现冒泡排序函数。void bubbleSort(int arr[], int n);。其中arr存放待排序的数据,n为数组长度(1≤n≤1000)。

函数接口定义如下:

/* 对长度为n的数组arr执行冒泡排序 */
void bubbleSort(int arr[], int n);

请实现bubbleSort函数,使排序后的数据从小到大排列。

裁判测试程序样例:

#include <stdio.h>

#define N 1000
int arr[N]; /* 对长度为n的数组arr执行冒泡排序 */
void bubbleSort(int arr[], int n); /* 打印长度为n的数组arr */
void printArray(int arr[], int n); void swap(int *xp, int *yp) {
int temp = *xp;
*xp = *yp;
*yp = temp;
} int main() {
int n, i;
scanf("%d", &n);
for (i = 0; i < n; ++i) {
scanf("%d", &arr[i]);
}
bubbleSort(arr, n);
printArray(arr, n);
return 0;
}
/* 打印长度为n的数组arr */
void printArray(int arr[], int n) {
int i;
for (i = 0; i < n; i++) {
printf("%d", arr[i]);
if (i < n - 1) /* 下标0..n-2每个元素后面有个空格 */
printf(" "); /*下标n-1,也就是最后一个元素后面没有空格*/
}
printf("\n"); /* 一行打印完后换行 */
} /* 你的代码将嵌在这里 */

输入样例:

10
1 19 9 11 4 3 5 8 10 6

输出样例:

1 3 4 5 6 8 9 10 11 19

 1 void bubbleSort(int arr[], int n){
2 int m=n-1,flag=1;
3 while(m>0&&flag==1){
4 flag=0;
5 for(int j=0;j<m;j++){
6 if(arr[j]>arr[j+1]){
7 flag=1;
8 int t=arr[j];
9 arr[j]=arr[j+1];
10 arr[j+1]=t;
11 }
12 }
13 --m;
14 }
15 }

方法2

 1 void bubbleSort(int arr[], int n){
2 int i=n-1;
3 while(i){
4 int k=0;
5 for(int j=0;j<i;j++){
6 if(arr[j+1]<arr[j]){
7 int t=arr[j];
8 arr[j]=arr[j+1];
9 arr[j+1]=t;
10 k=j;
11 }
12 }
13 i=k;
14 }
15 }
 

PTA 冒泡排序的更多相关文章

  1. PTA循环,函数,数组作业

    PTA循环实验作业 题目一:统计素数并求和 ### 1.PTA提交列表 2.设计思路(+流程图) 先定义变量(包含素数区间,循环次数,除数,素数个数记录和和的记录) 输入范围 一重循环:循环提取自然数 ...

  2. PTA的Python练习题(二)

    继续在PTA上练习Python (从 第2章-5 求奇数分之一序列前N项和  开始) 1. x=int(input()) a=i=1 s=0 while(i<=x): s=s+1/a a=a+2 ...

  3. PTA题目集4-6总结

    PTA题目集4-6总结 一:前言 在题集4-6中,所考查的主要知识点有正则表达式,类与类之间的调用,类的聚合,继承,封装,接口与多态,三种排序方法如选择排序,冒泡排序,插入排序,ArrayList,s ...

  4. [C#][算法] 用菜鸟的思维学习算法 -- 马桶排序、冒泡排序和快速排序

    用菜鸟的思维学习算法 -- 马桶排序.冒泡排序和快速排序 [博主]反骨仔 [来源]http://www.cnblogs.com/liqingwen/p/4994261.html  目录 马桶排序(令人 ...

  5. 算法与数据结构(十三) 冒泡排序、插入排序、希尔排序、选择排序(Swift3.0版)

    本篇博客中的代码实现依然采用Swift3.0来实现.在前几篇博客连续的介绍了关于查找的相关内容, 大约包括线性数据结构的顺序查找.折半查找.插值查找.Fibonacci查找,还包括数结构的二叉排序树以 ...

  6. Html5 冒泡排序演示

    冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法. 它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再需要 ...

  7. javascript冒泡排序

    数组冒泡排序算法(升序) 升序:小数在前,大数在后 冒泡排序的原则:每次比较相邻两个元素,如果前一个数>后一个数,说明违反升序的要求,就将两数交换位置.否则,保持不变.继续比较下一对. 例如:玩 ...

  8. Java中的经典算法之冒泡排序(Bubble Sort)

    Java中的经典算法之冒泡排序(Bubble Sort) 神话丿小王子的博客主页 原理:比较两个相邻的元素,将值大的元素交换至右端. 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一 ...

  9. C#冒泡排序算法

    用了两种形式的数据,一个是泛型List,一个是数据int[].记录一下,作为自己学习过程中的笔记. using System; using System.Collections.Generic; us ...

随机推荐

  1. Python+argparse+notebook

    argparse"应用"于jupyter-notebook中 args.xx =======================>> args["xx" ...

  2. adjust All In One

    adjust All In One 调整 https://www.adjust.com/ Maximize the impact of your mobile marketing Adjust is ...

  3. React render algorithm & Fiber vs Stack

    React render algorithm & Fiber vs Stack React 渲染算法 & Fiber vs Stack https://stackoverflow.co ...

  4. React Component All In One

    React Component All In One https://reactjs.org/docs/react-api.html#components React Class Component ...

  5. Google & Chrome console & text adventure game

    Google & Chrome console & text adventure game Google's text adventure game https://www.googl ...

  6. yarn global add !== yarn add global

    yarn global add !== yarn add global yarn does not exist the --global flag, but exits yarn global com ...

  7. 算法的时间复杂度 & 性能对比

    算法的时间复杂度 & 性能对比 累加算法性能对比 // js 累加算法性能对比测试 const n = 10**6; (() => { console.time(`for`); let ...

  8. POSIX cron & schedule

    POSIX cron & schedule https://pubs.opengroup.org/onlinepubs/9699919799/utilities/crontab.html#ta ...

  9. AMP ⚡️原理

    AMP ️原理 AMP 是如何运作的 https://amp.dev/zh_cn/about/how-amp-works/ AMP 瞬时加载 结合了以下优化是 AMP 页面速度之快以至于它们可以瞬时加 ...

  10. css var & auto width css triangle

    css var & auto width css triangle https://codepen.io/xgqfrms/pen/PooeEbd css var https://codepen ...