How to perform bubble sort on a two-dimensional array

Publisher:橙子1234Latest update time:2015-05-27 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
   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.
    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!
Reference address:How to perform bubble sort on a two-dimensional array

Previous article:XS128 MCU Experiment: Nokia 5110 LCD Display
Next article:How to use printf in single chip program

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号