当前位置:网站首页>选择排序与冒泡排序模板
选择排序与冒泡排序模板
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]<<" ";
}
}
边栏推荐
- Crawler practice website image batch download
- Solve the problems encountered by the laravel framework using mongodb
- Which product is better if you want to go abroad to insure Xinguan?
- Teach you how to optimize SQL
- I stepped on a foundation pit today
- Contest3145 - the 37th game of 2021 freshman individual training match_ D: Ranking
- Solve the problem that the tabbar navigation at the bottom of vantui does not correspond to the page (window.loading.hash)
- Is online futures account opening safe and reliable? Which domestic futures company is better?
- Experience summary of the 12th Blue Bridge Cup (written for the first time)
- Bugku Zhi, you have to stop him
猜你喜欢
GUI Graphical user interface programming (XIV) optionmenu - what do you want your girlfriend to wear on Valentine's day
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
This function has none of DETERMINISTIC, NO SQL..... (you *might* want to use the less safe log_bin_t
Bugku Zhi, you have to stop him
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
I stepped on a foundation pit today
If you have just joined a new company, don't be fired because of your mistakes
Redis notes (I) Linux installation process of redis
How to use websocket to realize simple chat function in C #
Session learning diary 1
随机推荐
Aperçu du code source futur - série juc
Stm32bug [the project references devices, files or libraries that are not installed appear in keilmdk]
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
What are the virtual machine software? What are their respective functions?
JSON string conversion in unity
The "two-way link" of pushing messages helps app quickly realize two-way communication capability
(column 23) typical C language problem: find the minimum common multiple and maximum common divisor of two numbers. (two solutions)
Love and self-discipline and strive to live a core life
Lichuang EDA learning notes 14: PCB board canvas settings
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
Learning video website
Day05 錶格
static hostname; transient hostname; pretty hostname
If you have just joined a new company, don't be fired because of your mistakes
[Valentine's Day confession code] - Valentine's Day is approaching, and more than 10 romantic love effects are given to the one you love
The first spring of the new year | a full set of property management application templates are presented, and Bi construction is "out of the box"
Unspeakable Prometheus monitoring practice
2006 translation
PID of sunflower classic
2022 examination summary of quality controller - Equipment direction - general basis (quality controller) and examination questions and analysis of quality controller - Equipment direction - general b