Interesting programming – how to sort 10 numbers in ascending order in C language?

Time:2021-9-14

Example: C language sorts 10 numbers from small to large, which requires bubble sorting.

Problem solving ideas: there are two sorting rules: one is “ascending order”, from small to large; The other is “descending order”, from large to small.

Source code demonstration:

#include//Header file

Int main() / / main function

{

int i,j,t;// Defining integer variables

int array[10];// Define array size

Printf (“please enter ten numbers:”)// Prompt statement

For (I = 0; I < 10; I + +) / / manually enter 10 numbers into the array

  {

scanf(“%d,”,&array[i]);// Attention & Symbols

  }

For (J = 0; J < 9; j + +) / / outer loop limit

  {

For (I = 0; I < 9-j; I + +) / / memory loop

If (array [i] > array [i + 1]) / / if the previous number is larger than the next one

    {

t=array[i]; // Assign small numbers to the front and large numbers to the back

      array[i]=array[i+1];

      array[i+1]=t;

    }

  }

Printf (“sort from small to large:”)// Prompt statement

For (I = 0; I < 10; I + +) / / loop out 10 numbers

  {

    printf(“%d “,array[i]);

  }

printf(“\n”);// Line feed

return 0;// The return value of the function is 0

}

The compilation results are as follows:

Please enter ten numbers: 9 8 4 1 6 2 7 4 10 9

Sort from small to large: 1 2 4 4 6 7 8 9 9 10

The above is the famous “bubbling sort”, also known as “bubbling sort”. I hope you can learn some enlightenment from this example for learning quick sort and heap sort in the future.

 

If you want to better improve your programming ability, learn C language and C + + programming well! Overtake at the corner, take a quick step!
C language c + + Learning Circle】, sharing (source code, project actual combat video, project notes, basic introductory tutorial)
Welcome to change careers and learn programming partners. Use more materials to learn and grow faster than you think!

Programming learning books:

 

Programming learning video: