Sorting algorithm -- bubble sort

Keywords: C

Algorithm definition:

Bubble Sort is a simple sort algorithm in the field of computer science.
It repeatedly visits the element columns to be sorted, and compares two adjacent elements in turn. If the order (such as from large to small, and the first letter from Z to A) is wrong, it exchanges them. The work of visiting elements is repeated until no adjacent elements need to be exchanged, that is to say, the element column has been sorted.
The name of this algorithm comes from the fact that the smaller elements will slowly "float" to the top of the sequence (ascending or descending) through exchange, just like the bubbles of carbon dioxide in carbonated drinks will eventually float to the top, so it is called "bubble sorting".

Algorithm analysis:

Time complexity

If the initial state of the file is positive sequence, one scan can complete the sequence. Number of keyword comparisons requiredAnd record the number of movesAll reach the minimum value:

.
Therefore, the best time complexity of bubble sorting is
.
If the initial file is in reverse order, you need to
Order of times. Each sorting shall be carried out
Sub key comparison (1 ≤ i ≤ n-1), and each comparison must move the record three times to reach the exchange record position. In this case, the number of comparisons and moves reaches the maximum:
The worst time complexity of bubble sorting is
.
In conclusion, the total average time complexity of bubble sorting is
.

Algorithm stability

Bubble sort is to move small elements forward or to move large elements back. Comparison is the comparison of two adjacent elements, and exchange also occurs between them. Therefore, if two elements are equal, they will not be exchanged; if two equal elements are not adjacent, even if the two elements are adjacent through the previous two exchanges, they will not be exchanged at this time, so the sequence of the same elements has not changed, so bubble sorting is a stable sorting algorithm.  

 

C code

bubble_sort.c

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #include <string.h>
 5 #include <sys/time.h>
 6 
 7 #include "common.h"
 8 
 9 struct ArrayData
10 {
11     int iLen;
12     int Array[0];
13 };
14 
15 void swap(int *a, int *b)
16 {
17     int iTmp;
18     iTmp = *a;
19     *a = *b;
20     *b = iTmp;
21 }
22 
23 void bubble_sort(int *iArray, int len)
24 {
25     for(int index = 0; index < len - 1; index++)
26     {
27     for(int j = 0; j < len-1-index; j++)
28     {
29         if(iArray[j] > iArray[j+1])
30         {
31         swap(&iArray[j], &iArray[j+1]);
32         }
33     }
34     }
35 }
36 
37 int main(int argc, char **argv)
38 {
39     //int iArray[10000] = {0};
40     int size = atoi(argv[1]);
41     struct ArrayData *iArrayData = NULL;
42     struct timeval stTimeStart;
43     iArrayData = malloc(sizeof(struct ArrayData) + size * sizeof(int));
44     iArrayData->iLen = size;
45     int *iArray = iArrayData->Array;    
46     unsigned long ulTimeUse = 0;
47 
48     memset(&stTimeStart, 0, sizeof(stTimeStart));
49 
50     /* Initialize random number generator */
51     srand((unsigned)time(NULL));
52 
53     for(int i = 0; i < size; i++)
54     {
55     iArray[i] = rand() % 200 - 100;
56     }
57 
58     //printf("Before bubble sorting:\r\n");
59     //for(int i = 0; i < size; i++)
60     //{
61     //printf("%d ", iArray[i]);
62     //}
63 
64     COMMON_StartRecordTime(&stTimeStart);
65     bubble_sort(iArray, size);
66     ulTimeUse = COMMON_EndRecordTime(&stTimeStart);
67 
68     printf("sort%d Data, time consuming%ld usec.\r\n", size, ulTimeUse);
69     //printf("\r\n After bubble sorting:\r\n");
70     //for(int i = 0; i < size; i++)
71     //  {
72 //    printf("%d ", iArray[i]);
73     //}
74     //printf("\r\n");
75 
76     return 1;
77 }

common.c

 1 #include <stdio.h>
 2 #include <sys/time.h>
 3 
 4 void COMMON_StartRecordTime(struct timeval *pstTimeStart)
 5 {
 6     gettimeofday(pstTimeStart, NULL);
 7 }
 8 
 9 unsigned long COMMON_EndRecordTime(struct timeval *pstTimeStart)
10 {
11     struct timeval stTimeEnd;
12     gettimeofday(&stTimeEnd, NULL);
13     return 1000000 * (stTimeEnd.tv_sec - pstTimeStart->tv_sec) + (stTimeEnd.tv_usec - pstTimeStart->tv_usec);
14 }

comman.h

1 extern void COMMON_StartRecordTime(struct timeval *pstTimeStart);
2 extern unsigned long COMMON_EndRecordTime(struct timeval *pstTimeStart);

Posted by saint959 on Mon, 01 Jun 2020 04:47:15 -0700