当前位置:网站首页>Bubble sort - alternate sort at both ends
Bubble sort - alternate sort at both ends
2022-06-13 00:48:00 【Feiyichai】
Problem description
Write a two-way bubble sorting algorithm , Scan alternately in both positive and negative directions , That is, put the element with the largest keyword at the end of the first pass , The second time, put the element with the smallest keyword at the top of the sequence , It goes on and on .
Algorithmic thought
On the basis of the original bubbling sort, it is reformed
Transformation Division :① External cycle conditions , Because after a round of sorting, the sequence to be sorted , The start and end points of the next sort will change , So don't fill in the increment or decrement statements directly for(i=0,j=L.length-1;i<j;)
② Internal circulation conditions for(k=i+1;k<=j;k++) // The largest element is pushed to the last (j The point position is the end position )for(l=j-1;l>=i;--l)// The smallest element is pushed to the front
- Complete test code
#include<stdio.h>
int main(){
int a[7]={
111,112,6,15,3,118,6};
int i,j,k,l,temp;
for(i=0,j=6;i<j;){
int flag=0;// Judge whether there is reverse order
for(k=i+1;k<=j;k++){
if(a[k]<a[k-1]){
flag=1;
temp=a[k];
a[k]=a[k-1];
a[k-1]=temp;
}
}
if(!flag) break;// Is an ordered sequence exiting the loop
j--;
for(l=j-1;l>=i;--l){
if(a[l]>a[l+1]){
flag=1;
temp=a[l];
a[l]=a[l+1];
a[l+1]=temp;
}
}
if(!flag) break;
i++;// The subscript needs to be modified because the smallest element will be sorted from the back to the front
}
for(i=0;i<7;i++)
printf(" %d ",a[i]);
}
边栏推荐
- Aunt learning code sequel: ability to sling a large number of programmers
- Using fastjson to solve the problem of returning an empty array from a null value of a field string object
- Kali system -- fierce of DNS collection and analysis
- 从ADK的WinPE自己手动构建自己的PE
- Programming training 1
- 深度学习每周期的步数多少合适?
- Assembly language learning
- In / out / inout details of MySQL stored procedures
- Using com0com/com2tcp to realize TCP to serial port (win10)
- Kotlin collaboration, the life cycle of a job
猜你喜欢

Mysql批量插入数据时如何解决重复问题?
![[MRCTF2020]Ez_bypass --BUUCTF](/img/73/85262c048e177968be67456fa4fe02.png)
[MRCTF2020]Ez_bypass --BUUCTF

MySQL queries the quantity of each month and the year-on-year and month on month data of each month

Kotlin 协程的四种启动模式

Kotlin collaboration, the life cycle of a job

Antdpro - protable realizes the linkage effect of two selection boxes

Three column simple Typecho theme lanstar/ Blue Star Typecho theme

Cards are unpredictable

Kali system -- fierce of DNS collection and analysis

People and gods are angry. Details of Tangshan "mass beating of women incident"
随机推荐
antdPro - ProTable 实现两个选择框联动效果
kotlin 协程withContext切换线程
Browser console injection JS
pytorch是什么?解释pytorch的基本概念
什么是 dummy change?
1. Google grpc framework source code analysis Hello World
从ADK的WinPE自己手动构建自己的PE
Stack overflow learning summary
三角波与三角波卷积
[network protocol] problems and solutions in the use of LwIP
MCU serial port interrupt and message receiving and sending processing -- judge and control the received information
Solution to the problem of closing the watchdog of STM32 in the sleep mode (stop/standby)
Three kinds of thinking make me reborn
Ad14 component pin name disappeared
Arduino control soil moisture sensor
Kali system -- host, dig, dnsenum, imtry for DNS collection and analysis
今日在家休息
Aof persistence
MySQL query table field information
In / out / inout details of MySQL stored procedures