1759_C语言中冒泡排序的实现以及新编译环境测试

news/2024/10/22 11:40:11/

全部学习汇总: GreyZhang/c_basic: little bits of c. (github.com)

最近在重新学习C语言的数据结构,找了一份国外的电子书一点点看。刚刚学完双向链表,接下来的任务是搞定几个常用的排序。

冒泡排序还算是我比较熟悉的,工作之后遇到的排序功能我基本上都是首选这个算法进行排序功能的实现。

冒泡排序是把一个序列分为已经排序的和没有排序的两部分,左侧的数据作为排好序的部分,右侧的为未排序部分。从最左边的一个数据开始作为比较对象,依次遍历比较右边的数据。如果右边的数据比左边的小,那么两者交换位置。完成右边的便利比较之后,左侧的数据成为已经排好序的部分。接下来,进行第二个数据的排序。如此循环,直到处理完整个数据序列。

一个C语言的冒泡排序核心代码实现如下:

void BubbleSort(int *array,size_t array_size)

{

    int i = 0;

    int j = 0;

   

    for(i = 0; i < array_size; i++)

    {

        for(j = i + 1; j < array_size; j++)

        {

            if(array[i] > array[j])

            {

                array[i] = array[i] ^ array[j];

                array[j] = array[i] ^ array[j];

                array[i] = array[i] ^ array[j];

            }

        }

    }

}

void ArrayPrint(int *array,size_t array_size)

{

    int i = 0;

    for(i = 0; i < array_size; i++)

    {

        printf("%d,",array[i]);

    }

}

测试数据以及主函数中的测试调用主要代码如下:

#include "stdio.h"

#include "bubble_sort.h"

int test_array[12] = {1,3,5,7,2,5,9,5,21,55,1,0};

int main(void)

{

    printf("before sorted:\n");

    ArrayPrint(test_array,MY_CARD(test_array));

         BubbleSort(test_array,MY_CARD(test_array));

         printf("\nafter sorted:\n");

         ArrayPrint(test_array,MY_CARD(test_array));

    return 0;   

}

编译运行结果如下:

测试看来,编译环境正常,排序正确。

 


http://www.ppmy.cn/news/712783.html

相关文章

5002 - git命令

git init git remote add origin https://gitee.com/xxxxx/zai-demo.git git add . git commit -m "添加注释信息" git push -u origin master -f --提交git rm -r --cache

A newer version of WinPcap (internal version 10.2.0.5002) is already installed on this machine 问题解决

GNS3 / WinPcap 安装问题解决 此计算机上已安装更新版本的WinPcap(内部版本10.2.0.5002&#xff09; 因为windows10自带在Win10Pcap版本与GNS3安装版本冲突解决方法 点击http://www.win10pcap.org/download/下载下方安装包 运行点击remove完成后续即可 如运行界面为安装界面即…

HDU 5002 Tree (2014年鞍山赛区网络赛F题)

1.题目描述&#xff1a;点击打开链接 2.解题思路&#xff1a;LCT的模板题 3.代码&#xff1a; #include <cstdio> #include <cstdlib> #include <algorithm> #include <iostream> #include <vector>using namespace std;const int N 111111; …

P5002 专心OI - 找祖先

P5002 专心OI - 找祖先 给定一棵有根树&#xff08;\(n \leq 10000\)&#xff09;&#xff0c;\(M \leq 50000\) 次询问&#xff0c; 求以 \(x\) 为 \(LCA\) 的点对个数 错误日志&#xff1a; 看下面 Solution 设点 \(u\) 的子树大小为 \(size[u]\) 现询问以 \(u\) 为 \(LCA\) …

Server com.webank.webase.front.Application Port 5002..Failed

ubuntu打开端口5000和5002 Ubuntu20.04LTS开启22端口 https://blog.csdn.net/m0_38068876/article/details/121115598

E - The Queue UVALive - 5002 树形dp

题目链接 题意&#xff1a;n个人排队&#xff0c;每个人b除CEO外都有一个监督人a&#xff0c;b必须排在a的后面&#xff0c;问有多少排队方案。 思路: 这是一道树形dp和组合数学的题目,当时训练的时候公式推对了 ,但是写的时候想错了... 这道题我一看是n个点 n-1个边 又有上下…

UVALive 5002 The Queue (树形Dp)

题意&#xff1a; 在某些特殊场合&#xff0c;Nadia公司为公司所有员工提供非常特别的午餐。 食物供应前&#xff0c;所有员工必须站在食品柜台前排队。 公司应用了排队队列的规则。 这个规则是没有人可以在他的主管面前的任何地方。 例如&#xff0c;如果Abul是Babul的负责人…

卜若的代码笔记系列-unity系列-第二章:json2-5002

1.我们返回对象数组时&#xff0c;请注意&#xff0c;来自于服务器的恶意&#xff1a; 2.这个时候我们就需要自行组装字符串了 //1.将字符串按,拆解 //2.将字符数组‘&#xff0c;’组装 //3.退格“}” 完成 IEnumerator ieReuqest(){var request (HttpWebRequest)WebRequest…