Preface
In daily programming, sorting algorithms are a very common and important tool. Although there are many sorting algorithms to choose from, if you need a sorting algorithm that can handle different data types, how can you design a general sorting algorithm? Today we will implement a general bubble sorting algorithm that supports sorting of different data types and uses function pointers to provide flexible comparison methods.
1. Introduction to Bubble Sorting Algorithm
Bubble sorting is a simple sorting algorithm that works by constantly exchanging adjacent elements so that each traversal can "bubble" the largest element to the end of the array. Its time complexity is O(n²), and although it is not suitable for sorting large data volumes, it is still a very useful tool when learning sorting algorithms due to its simplicity of implementation.
2. Implementation ideas for general bubble sorting
We want to implement a general bubble sort, that is, we can process arrays of any type (integrals, floating point numbers, strings, etc.). To achieve this, we need to consider the following points:
-
Type irrelevance:use
void *
to represent array elements, so that the function can support processing of any type of data. - Comparison function: Use function pointers to allow users to define comparison logic, ensuring that sorting can be done according to user needs.
-
Memory operation: We will use
memcpy
To swap array elements, so that elements of any size can be processed.
3. Code implementation
#include <> #include <> #include <> // Types of general comparison functionstypedef int (*CompareFunc)(const void *, const void *); // General bubble sorting functionvoid bubbleSort(void *base, size_t num, size_t size, CompareFunc compare) { unsigned char *arr = (unsigned char *)base; for (size_t i = 0; i < num - 1; i++) { int swapped = 0; for (size_t j = 0; j < num - i - 1; j++) { unsigned char *a = arr + j * size; unsigned char *b = arr + (j + 1) * size; if (compare(a, b) > 0) { unsigned char temp[size]; memcpy(temp, a, size); memcpy(a, b, size); memcpy(b, temp, size); swapped = 1; } } if (!swapped) { break; } } } // Example comparison function: used to sort integersint compareInt(const void *a, const void *b) { return (*(int *)a - *(int *)b); } // Example comparison function: used to sort floating point numbersint compareFloat(const void *a, const void *b) { if (*(float *)a < *(float *)b) return -1; if (*(float *)a > *(float *)b) return 1; return 0; } // Functions that print arraysvoid printArray(void *base, size_t num, size_t size, void (*printElem)(const void *)) { unsigned char *arr = (unsigned char *)base; for (size_t i = 0; i < num; i++) { printElem(arr + i * size); } printf("\n"); } // Print integer array elementsvoid printInt(const void *a) { printf("%d ", *(int *)a); } // Print floating point array elementsvoid printFloat(const void *a) { printf("%.2f ", *(float *)a); } int main() { // Test integer array int arrInt[] = {64, 34, 25, 12, 22, 11, 90}; size_t numInt = sizeof(arrInt) / sizeof(arrInt[0]); printf("Array of integers before sorting: "); printArray(arrInt, numInt, sizeof(int), printInt); bubbleSort(arrInt, numInt, sizeof(int), compareInt); printf("Sorted integer array: "); printArray(arrInt, numInt, sizeof(int), printInt); // Test floating point array float arrFloat[] = {64.5, 34.2, 25.1, 12.9, 22.7, 11.6, 90.3}; size_t numFloat = sizeof(arrFloat) / sizeof(arrFloat[0]); printf("Array of floating point numbers before sorting: "); printArray(arrFloat, numFloat, sizeof(float), printFloat); bubbleSort(arrFloat, numFloat, sizeof(float), compareFloat); printf("Sorted floating point array: "); printArray(arrFloat, numFloat, sizeof(float), printFloat); return 0; }
Code parsing
-
bubbleSort
Function:- We use
void *base
To represent an array pointer, so that this function can handle arrays of different types. -
size_t size
Denote the size of each element,CompareFunc compare
is a function pointer that allows the user to pass in a custom comparison function. - During the sorting process, we pass
memcpy
Come to exchange elements, becausevoid *
is an uncertain type pointer, and direct operation may cause errors.
- We use
-
compareInt
andcompareFloat
Function:-
compareInt
Functions are used to compare integers,compareFloat
Functions are used to compare floating point numbers. You can define more comparison functions to support other data types as needed.
-
-
printArray
Function:- This function is used to print arrays and supports any type of elements. Print function by passing in
printElem
, we can print different elements according to different data types.
- This function is used to print arrays and supports any type of elements. Print function by passing in
Sample output
Array of integers before sorting: 64 34 25 12 22 11 90
Sorted integer array: 11 12 22 25 34 64 90
Floating point array before sorting: 64.50 34.20 25.10 12.90 22.70 11.60 90.30
Sorted floating point array: 11.60 12.90 22.70 25.10 34.20 64.50 90.30
4. Summary
This article implements a general bubble sorting function that supports sorting arrays of any type. By usingvoid *
Pointer and function pointers, we make sorting functions have good flexibility and scalability. Whether it is an integer, floating point number or other types of arrays, you only need to provide the appropriate comparison function to easily sort it.
This general sorting implementation can be applied in many scenarios, especially in library functions that process different types of data. If you are developing a library and need to support different types of data, a similar implementation can be very useful.
This is the article about how to implement a general bubble sorting algorithm in C language. For more relevant contents of general bubble sorting algorithms in C language, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!