Sorting algorithm 01------------------------------------ bubble sorting

Posted by adige72 on Fri, 20 Mar 2020 20:04:06 +0100

1. Sorting algorithm is necessary and often used in programming, which must be learned.

2. In my opinion, the most suitable language for practicing various algorithms is not c language. The syntax of C language itself is simple and straightforward, and there is not much encapsulation, so it is suitable to describe the steps of the algorithm.

1, Bubble sorting

1) bubble sorting is common and simple in sorting algorithm, which is suitable for programs with small data volume and daily use.

2) principle of bubble sorting (take ascending order as an example): compare two adjacent elements. If the former element is larger than the latter, the two elements will be exchanged. Otherwise, the next two adjacent elements will be compared.

In this way, after a comparison, you can put the largest element at the end. Do the same for the remaining elements, and you can sort them. This sort is just like bubbling, and the corresponding elements slowly "float" to the top, so it's called bubbling sort.

3) the code is as follows, and the notes have been written.

  

 1 #include<stdio.h>
 2 //arr Array, num Element number 
 3 void bubbleSort(int *arr,int num)
 4 {
 5     int i,j;
 6     int temp;
 7     for(i=0;i<num-1;i++)//Yes num Elements, need to compare num-1 second 
 8     {
 9         for(j=0;j<num-i-1;j++)//Every comparison needs to be made num-i-1 Secondary comparison 
10         {                        //There i Indicates the number of elements that have been sorted
11                  //Ascending sort
12             if(arr[j]>arr[j+1])//If the front one is larger than the back one, exchange 
13             {
14                 temp=arr[j];
15                 arr[j]=arr[j+1];
16                 arr[j+1]=temp;
17             }
18         }
19     }
20 }
21 int main()
22 {    
23     int i; 
24     int arr[10]={1,3,-9,0,10,2,8,9,19,-1};
25     bubbleSort(arr,10);//Bubble sort
26     for(i=0;i<10;i++)
27         printf("%d\n",arr[i]);
28     return 0;
29 }

 

Algorithm time complexity: O(n? 2). N elements, n-1 comparisons are required, and each comparison requires n-i-1 comparisons.

Two. Expand

Once saw a question, asked: what method can improve the efficiency of bubble sorting?

The answer is: set a flag flag to indicate whether there are elements to exchange in each comparison. If there is no exchange, it means that these elements have been arranged in order, so there is no need to continue the comparison, and the algorithm is over.

The modified bubbleSort is as follows

 1 void bubbleSort(int *arr,int num)
 2 {
 3     int i,j;
 4     int temp;
 5     int flag=0;//0 Indicates there is no exchange, 1 indicates there is exchange 
 6     for(i=0;i<num-1;i++)//Yes num Elements, need to compare num-1 second 
 7     {
 8         for(j=0;j<num-i-1;j++)//Every comparison needs to be made num-i-1 Secondary comparison 
 9         {                        //There i Indicates the number of elements that have been sorted
10                  //Ascending sort
11             if(arr[j]>arr[j+1])//If the front one is larger than the back one, exchange 
12             {
13                 temp=arr[j];
14                 arr[j]=arr[j+1];
15                 arr[j+1]=temp;
16                 flag=1; 
17             }
18         }
19         if(flag==0)//No exchange, ordered, end cycle directly 
20             break;
21         flag=0; 
22     }
23 }

Topics: C Programming