The code may be redundant, but for the sake of easier understanding, some parts can be simplified. However, after simplification, it may be difficult for those who have just started learning C language to understand.
Reference address:How to perform bubble sort on a two-dimensional array
It is very simple to bubble sort a one-dimensional array, but it is a bit troublesome to bubble sort a two-dimensional array. However, as long as you know how to bubble sort a one-dimensional array, it is very simple to read these contents. In fact, you can directly bubble sort a two-dimensional array, but for the sake of easy understanding, I used array conversion, which took me an hour to research. When my classmates asked me how to bubble sort a two-dimensional array, although I had never done it before, I had this idea in my mind at that time, but in the actual process of writing code, the problem was not as smooth as I thought. Conversion between arrays is a problem, so before bubble sorting a two-dimensional array, I took the time to study the array conversion problem, and finally integrated them together, so the work was done!
I have added the ideas, points to note and explanations in the comments next to the code, please think about it carefully!
This log is absolutely a mystery to those who don’t know C language. It looks very boring and you can just glance at it and skip it! !
/*The basic idea is to convert the two-dimensional array into a one-dimensional array, and then perform bubble sort on the one-dimensional array.
Then convert the sorted one-dimensional array into a two-dimensional array, and finally output it in the format of two rows and three columns.
Conversion issues between arrays*/
#include
main()
{
int a[2][3]={3,2,1,6,5,4};
int i,j,k,b[6];
int temp;
int y,z; //Used when converting a one-dimensional array into a two-dimensional array
k=0; //Initialize the value of k
//Convert to a one-dimensional array
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
b[k]=a[i][j];
k++;
}
}
//Bubble sort the one-dimensional array b[6]
for(j=0;j<5;j++)
{
for(i=0;i<5-j;i++)
{
if(b[i]>b[i+1])
{
temp=b[i];
b[i]=b[i+1];
b[i+1]=temp;
}
}
}
//Then convert the sorted one-dimensional array b[6] into a two-dimensional array a[2][3] and output it in a certain format
//Redefine a variable y z to avoid conflicts between variables
for(y=0;y<2;y++)
{
for(z=0;z<3;z++)
{
a[y][z]=b[y*3+z]; //This step needs to be understood carefully. Why is it y*3+z? This is a very critical step.
}
}
// Output a two-dimensional array with two rows and three columns
for(i=1;i<=2;i++)
{
for(j=1;j<=3;j++)
{
printf("%3d",a[i-1][j-1]);
if(j%3==0) //Wrap when there are three numbers in each line
printf(" ");
}
}
}
If you can truly understand the code I wrote, then one-dimensional array bubble sort, two-dimensional array bubble sort, and conversion between arrays will be no problem!!!
It’s the same old saying: read more, learn more, and think more! This is my guide to learning programming!
Previous article:XS128 MCU Experiment: Nokia 5110 LCD Display
Next article:How to use printf in single chip program
Recommended Content
Latest Microcontroller Articles
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
Guess you like
- About MSP430-Timer WDT
- Does anyone know the pin map of the YC1021 chip?
- How to select all device bit numbers or nominal values in the SCH file for AD1904 version
- Testing methods and diagnostic analysis for DSP-containing circuit boards
- Fundamentals of Circuits and Analog Electronics Technology
- Need a low current charging IC and a Bluetooth chip with integrated MCU
- PCB Layout Guidelines
- LAUNCHXL-F28379D comprehensive routine
- 【DWIN Serial Port Screen】One of the Nucleic Acid Sampling and Registration Systems
- Live broadcast at 10 am today [TI's new generation MSP430 helps low-cost ultrasonic water meter]