# Bubble algorithm of C language, sorting data and library function, use and Simulation of qsort function

Posted by joe_C_nice on Sun, 06 Mar 2022 12:56:28 +0100

1. # Bubble algorithm of C language, sorting data and library function, use and Simulation of qsort function

Bubble algorithm for data sorting

### Article catalogue

• Bubble algorithm sorting integer array
• The use of c language library function qsort
• Analog implementation of qsort function

# 1, Bubble algorithm sorting integer data

1. Bubble algorithm realizes sorting, that is, two adjacent data are compared. If the former is greater than the latter, two data will be exchanged (taking ascending order as an example)

2. For a group of data to complete a comparison and exchange from left to right, the largest data can be placed on the far right (taking ascending order as an example)

3. If there are 10 data, 9 times of comparison and exchange are required

4. For each trip, the number to be compared shall be reduced by 1 in turn, because part of the data on the right has been arranged in order, so there is no need to compare again

```#include <stdio.h>

//Sort integer data from large to small. The algorithm used for sorting - Bubble algorithm, that is, the adjacent two numbers are compared in turn. If they are large, they are exchanged

void Bubble_sort(int arr[], int sz)
{
int i = 0;
int j = 0;
int ret = 0;
for (j = 0; j < sz - 1; j++)//Control the number of times. / / note that sz-1 times are required to complete the sorting if the sz number is sorted. j starts from 0 and sz-1 times. Therefore, the less than sign is used instead of less than or equal to
{
for (i = 0; i < sz - 1 - j; i++)//Control the number of comparisons each time. / / note that sz here means that the array size is 9. Since i and i+1 occur at the same time, the maximum i is sz-2 and sz-1
{
if (arr[i] > arr[i + 1])
{
ret = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = ret;

}
}
}
}

void print_result(int arr[],int sz)
{
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr[] = {2,4,5,1,6,7,8,9,3 };
int sz = sizeof(arr) / sizeof(arr[0]);//The array is passed into the function as an argument. In fact, only the address of the first element is passed in, so it needs to be matched with the number of array elements
Bubble_sort(arr, sz);
print_result(arr, sz);
return 0;
}```
1. # 2, The use of c language library function qsort

## 1. Introduction to qsort function

1. Before using the qsort function, you should introduce the header file < stdlib h>

2. Parameter introduction

```void qsort (void* base, size_t num, size_t size,
int (*compar)(const void*,const void*));```

The first parameter is the first address of the incoming data to be sorted; The second parameter is the number of data to be sorted; The third parameter is the number of bytes occupied by a data; The fourth parameter is the function pointer, pointing to the self-defined comparison function. The return type of the function is int, and the two parameters are const void *. The addresses of the two data to be compared are passed in. At the same time, pay attention to the internal logic of the function, that is, to achieve ascending order, if the former is smaller than the latter, it will return - 1, if the former is greater than the latter, it will return 1, and if the two are equal, it will return 0 (ps: to achieve descending order, you can reverse the internal logic of the comparison function defined by yourself)

3. Qsort function can sort any type of data, such as integer, structure, etc. the following two examples use qsort function to realize the ascending order of shaping data and the ascending order of structure data by name

```//Use the qsort function to realize the ascending order of integer data. If you want to realize the descending order, you can use the logic of the comparison function designed by yourself
int Compare(const void* p1, const void* p2)
{
if (*(int*)p1 < *(int*)p2) return -1;
if (*(int*)p1 == *(int*)p2) return 0;
if (*(int*)p1 > *(int*)p2) return 1;

}

void print_result(int arr[],int sz)
{
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
}
#include <stdlib.h>
int main()
{
int arr[] = { 2,4,3,1,5,9,7,8,6 };
int sz = sizeof(arr) / sizeof(arr[0]);
qsort(arr, sz, sizeof(arr[0]), Compare);
print_result(arr, sz);

}
```
```
//qsort function is not only fast, but also can arrange any type of data
#include <stdlib.h>
#include <string.h>
struct stu
{
char name[20];
int age;

};
//Note that when comparing two strings, the strcmp function is used, and the parameters are two const char * type pointers
int Compare_name(const void* p1,const void* p2)
{
if (strcmp(((struct stu*)p1)->name, ((struct stu*)p2)->name)<0) return -1;
if (strcmp(((struct stu*)p1)->name, ((struct stu*)p2)->name) > 0) return 1;
if (strcmp(((struct stu*)p1)->name, ((struct stu*)p2)->name)==0) return 0;

}

void print_result(struct stu arr[], int sz)
{
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%s %d\n", arr[i].name,arr[i].age);
}
}

int main()
{
struct stu arr[] = { {"Li Ming",20} ,{"Zhang San",15}, {"Hutu",14} };
int sz = sizeof(arr) / sizeof(arr[0]);
qsort(arr, sz, sizeof(arr[0]), Compare_name);
print_result(arr, sz);

}```

1. ## Three, simulation to achieve the qsort function (here bubble algorithm to simulate the implementation of qsort function).

1. The general idea of the algorithm is to use two cyclic variables to control the number of trips and the offset of the comparison object of each trip respectively, and compare the two adjacent data. If it does not meet the ascending order, it will be exchanged

2. Because the data type is not known in advance, the pointer of the function parameter should be a typeless pointer, and the parameter of the comparison function Compare should also be a typeless pointer. It should be noted that the typeless pointer should be forcibly converted to other typed pointers before use

3. During comparison, if the data type is unknown, the offset is realized by using the character pointer and the size of a data (i.e. how many bytes it occupies)

4. When two data exchanges are carried out, if the data type is unknown, the data can only be exchanged one byte by one using the smallest unit in the memory

2. ```//Using the bubbling algorithm to simulate and realize the qsort function, although qsort is not realized by the bubbling algorithm
#include<string.h>
void Swap(void* p1, void* p2, int data_size);
struct stu
{
char name[20];
int age;

};

void print_result(struct stu arr[], int sz)
{
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%s %d\n", arr[i].name, arr[i].age);
}
}

void bubble_qsort(void* data, int sz, int data_size, int (*compare)(const void*, const void*))
{
int i = 0;
int j = 0;
for (j = 0; j < sz - 1; j++)
{
for (i = 0; i < sz - 1 - j; i++)
{
if (compare((char*)data + i * data_size, (char*)data + (i + 1) * data_size) > 0)//Since the sorting code should be applicable to all data types, to offset the size of a data, the untyped pointer should be forcibly converted to char *, and then a data size data in the formal parameter should be used_ Size implementation offset
{
Swap((char*)data + i * data_size, (char*)data + (i + 1) * data_size,data_size);
}
}
}
}

void Swap(void* p1, void* p2,int data_size)
{
int i = 0;
char ret = 0;//Note that char* ret=NULL is not used here, because subsequent codes involve accessing and modifying ret values. Unless the null pointer is changed before use, the modification cannot be accessed directly
for (i = 0; i < data_size; i++)
{
ret = *((char*)p1 + i);
*((char*)p1 + i) = *((char*)p2 + i);//In the case of unknown data type, to exchange data, only the smallest unit in the memory can be used to exchange one byte by one; In the case of unknown data type, to offset the data, the character pointer and the size of a data (i.e. how many bytes to occupy) are used to realize the offset
*((char*)p2 + i) = ret;
}
}

int Compare_name(const void* p1,const void* p2)
{
if (strcmp(((struct stu*)p1)->name, ((struct stu*)p2)->name)<0) return -1;
if (strcmp(((struct stu*)p1)->name, ((struct stu*)p2)->name) > 0) return 1;
if (strcmp(((struct stu*)p1)->name, ((struct stu*)p2)->name)==0) return 0;

}

int main()
{
struct stu arr[] = { {"Li Ming",20} ,{"Zhang San",15}, {"Hutu",14} };
int sz = sizeof(arr) / sizeof(arr[0]);
bubble_qsort(arr, sz, sizeof(arr[0]), Compare_name);
print_result(arr, sz);
return 0;
}```

Topics: C Algorithm data structure