日常编程笔记 | 2022.10.1 | 归并排序_一无序列
极大数
又是数组下标访问越界了
当i或j超过右端点时,又在if判断中被拿来当下标参与比较,然后进入arr数列了
还有一处
左边数列的右端点就是中间的数,不应该用mid + 1
mid的计算
这里mid不能这样计算,否则会出现这样的情况
大量重复数字
本来想在i或j超过原来范围的时候强制把它转为右端点,但这样的话,每次赋值就都赋两个数列中,第一个被遍历完的数列的右端点了
C源码
//this function uses Divid-and-Conquer algorithm to sort unordered array #include<stdio.h>
#include<stdlib.h>
#include<time.h>#define N 10void Sort(int*a, int left, int right);//to divide the array into the smallest ordered piecesvoid Merge(int*a, int left, int mid, int right);//to merge the ordered two pieces into a larger oneint main()
{srand((unsigned int)time(0));//initialize the seedint *a = (int*)malloc(sizeof(int)*N);//to create a test arrayint i;for(i = 0; i < N; i++){a[i] = rand()%200 - 100;//to assign a random number from -100 to 100 to a[i]}Sort(a, 0, N - 1);//sort the arrayfor(i = 0 ; i < N; i++)//print the array{printf("%d ", a[i]); }return 0;
}void Sort(int*a, int left, int right)
{if(left >= right)//if divide the array into the smallest piecesreturn;int mid = left + (right - left)/2;//calculate the mid pointSort(a, left, mid);//sort the left sideSort(a, mid + 1, right);//sort the right sideMerge(a, left, mid, right);//merge from bottom to top
}void Merge(int*a, int left, int mid, int right)
{int size = right - left + 1;//the size of the array to store the ordered arrayint*arr = (int*)malloc(sizeof(int)*size); //allocate space to arrint i = left, j = mid + 1, k = 0; //i-the index of the left array//j-the index of the right array//k-the index of the new arrayfor(k = 0; k < size; k++){ if(i <= mid && j <= right)//if both of the two arrays are not traversed{//assign the smaller one to arr earlier, thus the arr is in increasing orderif(a[i] <= a[j]){arr[k] = a[i];i++;}else if(a[j] < a[i]){arr[k] = a[j];j++;}}//if one of the array is traversed, just assign the left part of the other array to arrelse if(i > mid && j <= right){arr[k] = a[j++];}else if(j > right && i <= mid){arr[k] = a[i++];}}//assign the value of arr one by one to a, which are already in increasing orderfor(k = left; k <= right; k++){a[k] = arr[k - left];}
}
日常编程笔记 | 2022.10.1 | 归并排序_一无序列相关推荐
- Google TensorFlow课程 编程笔记(10)———使用神经网络对手写数字进行分类
使用神经网络对手写数字进行分类 学习目标: 训练线性模型和神经网络,以对传统 MNIST 数据集中的手写数字进行分类 比较线性分类模型和神经网络分类模型的效果 可视化神经网络隐藏层的权重 我们的目标是 ...
- 少儿C++编程学习路线推荐(2022.10.31)
一.C++学习路线推荐 1.小学一二年级C++启蒙 (也可以同时加强数学,比如学一点儿小学奥数或提前学小学三四五六年级的数学知识) (1).<我的第一本算法书> 少儿C++编程如何入门 少 ...
- python随机生成100内的10个整数_用python随机生成数字教程_如何用Python编程随机产生10个随机整数,并输出这10个整数的和�9�3...
如何用Python编程随机产生10个随机整数,并输出这10个整数的和 用Python编程产生10个随机整数,并整数的和,方法如下 准备材料python.电脑 1.需要加载的模块是:random. 2. ...
- Java编程笔记2:初始化和清理
Java编程笔记2:初始化和清理 图源:Java Switch语句(用法详解)-java教程-PHP中文网 构造器 构造器,在编程领域也会被称作构造函数.事实上我觉得这个名称并不是很恰当,可能相当一部 ...
- 信奥中的数学学习资料汇总(2022.10.31)
信奥中的数学之入门组(面向小学四年级至六年级以及初一学生) 信奥中的数学之入门组(面向小学四年级至六年级以及初一学生)_dllglvzhenfeng的博客-CSDN博客 信奥中的数学学习:小学.初高中 ...
- 信奥中的数学:对数(2022.10.29)
[仙童小学]你真的认识对数吗 [仙童小学]你真的认识对数吗_哔哩哔哩_bilibili 小学生对数学的理解 5年级 小学生对数学的理解 5年级_哔哩哔哩_bilibili [秒懂数学]对数运算难如狗, ...
- linux常见命令以及shell入门编程(笔记总结记录)
linux常见命令以及shell入门编程(笔记总结记录) 文章目录 linux常见命令以及shell入门编程(笔记总结记录) 一.linux常见命令 1.不常用 2.常用 3.数据查找处理相关 1.g ...
- Go语言编程笔记18:软件测试
Go语言编程笔记18:软件测试 图源:wallpapercave.com 软件测试也是软件开发的重要组成部分,本篇文章将探讨如何使用Go的标准库和第三方库对程序进行测试. testing Go的标准库 ...
- 千峰教育2218期2022.10.18日
2022.10.18日 上课时间 早上8:50进直播间 9:00~10:20 10:40~11:50 13:50课程分享 自信 14:00~15:00 15:10~16:20 16:30~18:00 ...
最新文章
- Jquery php 点击td变成input,修改后失去焦点发送数据
- 基于Eclipse的TI集成开发环境IDE-CCSv5使用教程
- mac 终端里进入mysql和退出
- Linux shell脚本编程(一)
- book: Effective Java
- python怎么制作图像_python数字图像处理(5):图像的绘制
- eclipse各个版本的区别
- 进程间同步的几种方法
- mysql源码目录在哪_Mysql源码学习——源码目录结构
- mysql乱码加的一段代码_mysql乱码的解决方法
- SC || 记不住的正则
- python之路8-内置模块介绍
- Redis应用(四)——在Spring框架中的应用
- 【AI芯片】中国AI芯片爆发,架构创新迫在眉睫
- cmos与非门电路、或非门电路
- easyui datagrid deleteRow(删除行)的BUG!
- 顶层const和底层const
- 读取xml文件信息并存入数据库
- 2016年8月18日 星期四 --出埃及记 Exodus 16:19
- Covid-19 肺部 X 射线分类和 CT 检测演示