当前位置:网站首页>选择排序与冒泡排序模板
选择排序与冒泡排序模板
2022-07-04 03:32:00 【CTGU-Yoghurt】
备注:sizeof(a)/sizeof(a[0])的作用为求数组的元素个数
选择排序:
void xuanzhe()
{
for (int i = 0; i < sizeof(a)/sizeof(a[0])-1; i++)
{
for (int j = i+1; j < sizeof(a)/sizeof(a[0]); j++) {
if (a[i] > a[j])
{
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
}
冒泡排序:
void maopao()
{
for (int i = 0; i < sizeof(a) / sizeof(a[0])-1; i++)
{
for (int j = 0 ; j < sizeof(a) / sizeof(a[0])-i-1; j++) {
if (a[j] > a[j+1])
{
int t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
}
}
测试代码:
#include<iostream>
using namespace std;
int a[] = { 123,12,312,3,124,3,4534,5,423,4};
void xuanzhe()
{
for (int i = 0; i < sizeof(a)/sizeof(a[0])-1; i++)
{
for (int j = i+1; j < sizeof(a)/sizeof(a[0]); j++) {
if (a[i] > a[j])
{
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
}
void maopao()
{
for (int i = 0; i < sizeof(a) / sizeof(a[0])-1; i++)
{
for (int j = 0 ; j < sizeof(a) / sizeof(a[0])-i-1; j++) {
if (a[j] > a[j+1])
{
int t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
}
}
int main()
{
maopao();
for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
{
cout << a[i]<<" ";
}
}
边栏推荐
- 1day vulnerability pushback skills practice (3)
- Jenkins configures IP address access
- Leetcode51.n queen
- Problems and solutions of several concurrent scenarios of redis
- Rhcsa day 2
- MySQL data query optimization -- data structure of index
- I stepped on a foundation pit today
- Love and self-discipline and strive to live a core life
- Solve the problem that the tabbar navigation at the bottom of vantui does not correspond to the page (window.loading.hash)
- CSCI 2134
猜你喜欢
In my spare time, I like to write some technical blogs and read some useless books. If you want to read more of my original articles, you can follow my personal wechat official account up technology c
PHP database connection succeeded, but data cannot be inserted
150 ppt! The most complete "fair perception machine learning and data mining" tutorial, Dr. AIST Toshihiro kamishima, Japan
Unity controls the selection of the previous and next characters
Add token validation in swagger
Unity knapsack system (code to center and exchange items)
Leetcode51.n queen
What are the virtual machine software? What are their respective functions?
I stepped on a foundation pit today
The "message withdrawal" of a push message push, one click traceless message withdrawal makes the operation no longer difficult
随机推荐
Problems and solutions of several concurrent scenarios of redis
Contest3145 - the 37th game of 2021 freshman individual training match_ 1: Origami
Why is it recommended that technologists write blogs?
What is the difference between enterprise wechat applet and wechat applet
XSS prevention
This function has none of DETERMINISTIC, NO SQL..... (you *might* want to use the less safe log_bin_t
Apple submitted the new MAC model to the regulatory database before the spring conference
Experience summary of the 12th Blue Bridge Cup (written for the first time)
The difference between MCU serial communication and parallel communication and the understanding of UART
Unspeakable Prometheus monitoring practice
Recent learning fragmentation (14)
In my spare time, I like to write some technical blogs and read some useless books. If you want to read more of my original articles, you can follow my personal wechat official account up technology c
Have you entered the workplace since the first 00???
Rhcsa day 3
2022 attached lifting scaffold worker (special type of construction work) free test questions and attached lifting scaffold worker (special type of construction work) examination papers 2022 attached
1day vulnerability pushback skills practice (3)
MySQL query
What are the conditions for the opening of Tiktok live broadcast preview?
Zigzag scan
Record a problem that soft deletion fails due to warehouse level error