当前位置:网站首页>The sorting algorithm including selection, bubble, and insertion
The sorting algorithm including selection, bubble, and insertion
2022-08-04 06:55:00 【SUNNY_CHANGQI】
Three sorting algorithms
The selection algorithm
From my views, I think the best way to implement the selection algorithm is to think the procedures to execute the steps of the selection algorithm.
The description of the selection algorithm
Suppose there is a unsorted array with integers. The intuition for this algorithm recrusively pick out the minimum value from the decreasing size of the array and put it on the first position of the array. The concrete steps are as follows.
- From the A[0, n-1], pick out the minimum entry from the array and put it in the position indexed by 0;
- From the A[1, n-1], pick out the minimum entry from the array and put it in the position indexed by 1;
- From the A[2, n-1], pick out the minimum entry from the array and put it in the position indexed by 2;
- ⋯ \cdots ⋯
- From the A[n-1, n-1], pick out the minimum entry from the array and put it in the position indexed by n-1
The key points to implement it
Variable 1:x:conrol starting point
variable 2:y:control traverse
key technique: log minimum value by its index not value
The corresponding codes for this
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<int> sortArray(vector<int>& nums) {
// select sort
vector<int> res(nums);
int n = res.size();
for (int i = 0; i < n; i++) {
int min_index = i;
for (int j = i+1; j < n; j++) {
if (res[j] < res[min_index]) min_index = j;
}
swap(res[i], res[min_index]);
}
return res;
}
};
int main() {
srand(time(NULL));
vector<int> nums;
// initialize nums 100 random numbers
for (int i = 0; i < 100; ++i) {
nums.push_back(rand() % 1000);
}
Solution s;
vector<int> res = s.sortArray(nums);
for (auto i : res) {
cout << i << " ";
}
cout << endl;
return 0;
}
The bubble sort
The intuition for this algorithm
the intutition: the opposite of the selection
primay technique: swap
The corresponding codes for this
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<int> sortArray(vector<int>& nums) {
vector<int> res(nums);
int n = res.size();
/* bubble sort */
for (int i = n-1; i >= 0; i--) {
for (int j = i; j > 0; j--) {
if (res[j] < res[j-1]) swap(res[j], res[j-1]);
}
}
return res;
/*solution2*/
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < n - i - 1; j++) {
// if (res[j] > res[j + 1]) {
// swap(res[j], res[j + 1]);
// }
// }
// }
// return res;
}
};
int main() {
srand(time(NULL));
vector<int> nums;
// initialize nums 100 random numbers
for (int i = 0; i < 100; ++i) {
nums.push_back(rand() % 1000);
}
Solution s;
vector<int> res = s.sortArray(nums);
for (auto i : res) {
cout << i << " ";
}
cout << endl;
return 0;
}
The insertion algorithm
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<int> sortArray(vector<int>& nums) {
vector<int> res(nums);
int n = res.size();
/* insertion sort */
for (int i = 1; i < n; i++) {
for (int j = i; j > 0; j--) {
if (res[j] < res[j-1]) {
swap(res[j], res[j-1]);
}
}
}
return res;
for (int i = 1; i < n; i++) {
// int j = i;
// while (j > 0 && res[j] < res[j - 1]) {
// swap(res[j], res[j - 1]);
// j--;
// }
// }
// return res;
}
};
int main() {
srand(time(NULL));
vector<int> nums;
// initialize nums 100 random numbers
for (int i = 0; i < 100; ++i) {
nums.push_back(rand() % 1000);
}
Solution s;
vector<int> res = s.sortArray(nums);
for (auto i : res) {
cout << i << " ";
}
cout << endl;
return 0;
}
The corresponding results
5 31 43 58 59 79 89 111 123 129 154 158 160 173 182 190 201 203 208 209 209 210 227 228 243 249 250 254 255 267 270 270 303 312 332 345 363 387 395 400 426 443 444 446 451 481 482 488 489 493 505 507 534 544 586 587 596 598 609 610 614 619 639 642 676 691 693 716 718 727 732 746 754 756 776 810 814 814 815 839 848 857 880 881 899 924 933 942 945 951 954 955 957 966 969 976 990 991 992 994
边栏推荐
- Centos通过Docker搭建MySQL的PXC集群
- fanuc机器人IO分配报警信号分配无效
- Verilog“七宗罪”
- 分布式计算实验4 随机信号分析系统
- Praat:语音标注工具【保存为TextGrid文件】
- 分布式计算实验1 负载均衡
- 有人试过用NPGsql驱动连接openGauss开发应用的吗?
- MMDeploy部署实战系列【第三章】:MMdeploy pytorch模型转换onnx,tensorrt
- unity webgl报 Uncaught SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON
- Distributed Computing Experiment 1 Load Balancing
猜你喜欢
C语言实现-华为太空人手表
反射与枚举
详解CAN总线:常用CAN连接器的使用方法
DropBlock: Regularization method and reproduction code for convolutional layers
fanuc机器人IO分配报警信号分配无效
matlab让我的旧手机起死回生
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
unity webgl报 Uncaught SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON
七夕情人节:中英文祝福短信送给你
电商系统PC商城模块介绍
随机推荐
data:image/jpg;base64格式数据转化为图片
MySQL错误-this is incompatible with sql_mode=only_full_group_by完美解决方案
有趣的USB接口和颜色分类
MySQL配置文件配置
DropBlock: Regularization method and reproduction code for convolutional layers
MySQL复制表结构、表数据的方法
使用腾讯云发送短信 ---- 手把手教你搞定所有步骤
ExoPlayer添加Ffmpeg扩展实现软解功能
LeetCode 135. 分发糖果
Distributed Computing Experiment 1 Load Balancing
国内外知名源码商城系统盘点
53个全球免费学术资源数据库整理,查资料写论文必备【开学必备】
专题讲座7 计算几何 学习心得
用matlab打造的摩斯电码加解码器音频版,支持包括中文在内的任意字符
The school to apply for link
likeshop外卖点餐系统开源啦100%开源无加密
Transform 相对位置变换,坐标系转换
字节跳动岗位薪酬体系曝光,看完我真的酸了...
C语言实现-华为太空人手表
七夕送礼,心愿直抵!