The principle of bubble sort algorithm

  • Compare adjacent elements. If the first one is bigger than the second, swap them both.
  • Do the same for each pair of adjacent elements, starting with the first pair and ending with the last pair. At this point, the last element should be the largest number.
  • Repeat this step for all elements except the last one.
  • Keep repeating the above steps for fewer and fewer elements at a time until there are no more pairs to compare.

Code implementation

/** * Created by Cheongge on 2020.11.28 * wechat focus public number [C Language Chinese community], free 200G quality learning materials */
#include<stdio.h>

int *bubble_sort(int arr[], int len);

int main(a) {
    int *result, len;
    int data[] = {12.43.23.13.65.17.98.45.67.88};
    len = (int)sizeof(data) / sizeof(*data);
    printf("Raw data before bubble sort was used:");
    for (int i = 0; i < len; i++) {
        printf("%3d", data[i]);
    }
    printf("\n");
    result = bubble_sort(data, len);
    printf("The data after bubble sort is:");
    for (int j = 0; j < len; j++) {
        printf("%3d", *(result + j));
    }
    return 0;
}

/ *! * Loop over adjacent elements. If the first one is larger than the second, swap them both * @param arr * @param len * @return arr */
int *bubble_sort(int arr[], int len) {
    int temp;

    for (int i = 0; i < len - 1; i++) {
        for (int j = 0; j < len - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp; }}printf("Result after loop sort %d:", i + 1);
        for (int k = 0; k < len; k++) {
            printf("%3d", arr[k]);
        }
        printf("\n");
    }
    return arr;
}
Copy the code

The results

The raw data before bubble sort was used was:12 43 23 13 65 17 98 45 67 881Results after sub-loop sorting:12 23 13 43 17 65 45 67 88 982Results after sub-loop sorting:12 13 23 17 43 45 65 67 88 983Results after sub-loop sorting:12 13 17 23 43 45 65 67 88 984Results after sub-loop sorting:12 13 17 23 43 45 65 67 88 985Results after sub-loop sorting:12 13 17 23 43 45 65 67 88 986Results after sub-loop sorting:12 13 17 23 43 45 65 67 88 987Results after sub-loop sorting:12 13 17 23 43 45 65 67 88 988Results after sub-loop sorting:12 13 17 23 43 45 65 67 88 989Results after sub-loop sorting:12 13 17 23 43 45 65 67 88 98The data after bubble sort is:12 13 17 23 43 45 65 67 88 98
Copy the code

Diagram bubble sort

The raw data before bubble sort is: 12 43 23 13 65 17 98 45 67 88

Only the next cycle is analyzed here

  • The first comparison

  • Second comparison

  • The third comparison

  • The fourth comparison

  • The fifth comparison

  • The sixth comparison

  • The seventh comparison

  • The eighth comparison

  • The ninth comparison

The results after the first cycle sorting are as follows: 12, 23, 13, 43, 17, 65, 45, 67, 88, 98

In turn, you can go through the second cycle, the third cycle… The firstlen-1The second loop yields the final data