The implementation of bubbling sort method in C language (ascending sort method)

Time:2020-3-22

Task Code:

Sorting of numbers:

#include <stdio.h>
 
#define SIZE 10
int main()
{
  Int a [size] = {12, 43, 9, 13, 67, 98101, 89, 3, 35}; // disordered series of ten numbers
  int i,j,t;
  Printf ("this program uses bubble sorting to arrange the unordered sequence! \n "";
  //Bubble sorting
  For (I = 0; I < 10-1; I + +) // n-1 scans in total
  {
    For (J = 0; J < 10-i-1; j + +) // each scan ends when a [n-i-2] is compared with a [n-i-1]
    {
      If (a [J] > a [J + 1]) // if the last digit is smaller than the previous digit, the positions of the two numbers are swapped (ascending)
      {
        t=a[j+1];
        a[j+1]=a[j];
        a[j]=t;
      }
    }
  }
 
  Printf ("the arranged sequence is: \ n");
  //The output is in good order
  for(i=0;i<10;i++)
  {
 
    printf("%d ",a[i]);
  }
  return 0;
}

Character sorting:

#include <stdio.h>
 
#define SIZE 10
int main()
{
  Char a [size] = {'I', 'l', 'o', 'V', 'e', 'y', 'o', 'U', 'y', 'x'}; // the disordered sequence of ten numbers
  int i,j;
  char t;
  Printf ("this program uses bubble sorting to arrange the unordered sequence! \n "";
  //Bubble sorting
  For (I = 0; I < 10-1; I + +) // n-1 scans in total
  {
    For (J = 0; J < 10-i-1; j + +) // each scan ends when a [n-i-2] is compared with a [n-i-1]
    {
      If (a [J] > a [J + 1]) // if the last digit is smaller than the previous digit, the positions of the two numbers are swapped (ascending)
      {
        t=a[j+1];
        a[j+1]=a[j];
        a[j]=t;
      }
    }
  }
 
  Printf ("the arranged character set is: \ n");
  //The output is in good order
  for(i=0;i<10;i++)
  {
 
    printf("%c ",a[i]);
  }
  return 0;
}

Use functions to solve this problem:

#include <stdio.h>
 
 
Void function (char a [], int); // in particular, the function declaration here must be char a [], because the address is worn here, and cannot only use char
 
int main()
{
  int i;
  Char a [10] = {'I', 'l', 'o', 'V', 'e', 'y', 'o', 'U', 'y', 'x'}; // ten disordered character sequences
  Printf ("this program uses bubble sorting to arrange the unordered sequence! \n "";
  Function (a, 10); // call bubble sort
  Printf ("the arranged character set is: \ n");
  //The output is in good order
  for(i=0;i<10;i++)
  {
 
    printf("%c ",a[i]);
  }
  return 0;
}
 
void function(char a[],int m)
{
  //Bubble sorting
  int i,j;
  char t;
  For (I = 0; I < M-1; I + +) // n-1 scans in total
  {
    For (J = 0; J < m-i-1; j + +) // each scan ends when a [n-i-2] is compared with a [n-i-1]
    {
      If (a [J] > a [J + 1]) // if the last digit is smaller than the previous digit, the positions of the two numbers are swapped (ascending)
      {
        t=a[j+1];
        a[j+1]=a[j];
        a[j]=t;
      }
    }
  }
  return;
}

Implementation:

Knowledge summary:

Bubble sort method: also called ascending sort method, but compared with dichotomy search can only be applied to ordered sequence, how to change an ordered sequence can use bubble sort method!!!

Summarize the above process:

The solution of this idea is as follows:

Example:

Bubble sorting can be applied not only to numbers, but also to letters:

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.

Recommended Today

Python basics Chinese series tutorial · translation completed

Original: Python basics Python tutorial Protocol: CC by-nc-sa 4.0 Welcome anyone to participate and improve: a person can go very fast, but a group of people can go further. Online reading Apache CN learning resources catalog introduce Seven reasons to learn Python Why Python is great Learn Python introduction Executing Python scripts variable character string […]