Bubble sort c language (Bubble Sort)

Bubble sort (Bubble Sort) is a sorting algorithm, mainly used to sort the elements on the array from small (large) to large (small), mainly to compare adjacent elements for exchange, and then repeat the work until there is no If the elements are to be exchanged, it means that the array of elements has been arranged.

General idea:

If there is an array: int arr[] ={9,8,7,6,5,4,3,2,1} Arrange it in ascending order;

Write the function void Bubble_Sort (int arr [], int sz) to receive this (arr []) array and sz (the number of elements in the array). Note that what Bubble_Sort (arr, sz) arr passes in is actually the first array address of an element.

In the function we start the sort:

Do the same for each pair of adjacent elements, from the first pair at the beginning to the last pair at the end, the last element should be the largest number.

Move 9 to the end a total of 8 times, that is, 9 numbers (at most) will move 8 times, that is, n numbers (at most) will move n-1 times.

If this action is called one trip, then one element will be arranged after each trip, and the arranged elements will not need to be moved again.

That is to say, one less number will be moved in the next trip (one less move).

In this trip, 8 moved 7 times, as can be seen from here: each trip is related to the (maximum) number of moves;

(The number of moves will decrease as each trip increases) From here, it can be written as two loops;

That is, if the array {9,8,7,6,5,4,3,2,1} is completely sorted into {1,2,3,4,5,6,7,8,9}, a total of 8 trip

(the smallest this element does not need to move)

Continue repeating the above steps for fewer and fewer elements each time until there are no pairs of numbers to compare.

int j=0;
    for (j = 0; j < sz - 1; j++) //here is how many times
    {     int i=0;

      for (i = 0; i < sz - 1 - j; i++) //here is how many times to move

{

}

};

The basic function idea here is almost correct, and the only thing left is how to make the elements inside exchange actions;

Because it was said earlier that arr in Bubble_Sort(arr, sz) is only the address of the first element

The first element can be found through the address, so how to compare it with the next element requires knowing a knowledge point of the array;

The addresses of each element in the array are stored adjacently.

The first time arr[0] is compared with arr[1];

If arr[0]>arr[1] then swap them:

int tmp = 0;
 tmp = arr[i];
 arr[i] = arr[i + 1];
 arr[i + 1] = tmp;

Finally, enter the function again; 

 In the end, it is possible to ask if it can be optimized.

Let's say if our array is {9,1,2,3,4,5,6,7,8}

It only takes one trip to complete the sorting, but our code will still compare one trip after another, which is wasteful.

Then if we do not exchange any more elements when he sorts, it means that the sorting has been completed, and the program can end at this time;

(I have thought about many possibilities for this optimization, but it is still not optimized enough, so I can't figure it out;)

Guess you like

Origin blog.csdn.net/m0_66780695/article/details/128743601