Sorting Array
Intro
Every now and then, you encounter a situation where sorting data becomes necessary. Usually, I quickly hack together a simple but inefficient bubble sort. But wouldn't it be great if the standard C library provided an efficient, ready-made sorting function? Surprisingly, it does! It's called qsort. In this article, I'll share some practical examples of using qsort.
Example 1: sort an integer array
Let's start with the simplest case: sorting an array of integers. This example demonstrates basic usage of qsort.
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
int comparison_func(int *a, int *b) {
if (!a || !b) {
return 0;
}
if (*a < *b) {
return -1;
}
if (*a > *b) {
return 1;
}
return 0;
}
int main(void) {
int array[] = {9, 3, 2, 6, 5, 4, 7, 8, 1};
for (int i = 0; i < sizeof(array) / sizeof(int); i++) {
printf("array[%d]=%d\n", i, array[i]);
}
puts("Sorting...");
qsort(array, sizeof(array) / sizeof(int), sizeof(int),
(int (*)(const void *, const void *))comparison_func);
for (int i = 0; i < sizeof(array) / sizeof(int); i++) {
printf("array[%d]=%d\n", i, array[i]);
}
return 0;
}
Example 2: sort array of strings
Sorting strings alphabetically is also straightforward. Here we see how qsort can sort an array of character pointers.
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int comparison_func(char **a, char **b) {
if (!a || !*a || !b || !*b) {
return 0;
}
return strcmp(*a, *b);
}
int main(void) {
char *array[] = {"John von Neumann", "Thomas Edison", "Claude Shannon",
"Richard Feynman", "Stanislaw Ulam", "Julian Bigelow",
"Alan Turing"};
for (int i = 0; i < sizeof(array) / sizeof(char *); i++) {
printf("array[%d]=%s\n", i, array[i]);
}
puts("Sorting...");
qsort(array, sizeof(array) / sizeof(char *), sizeof(char *),
(int (*)(const void *, const void *))comparison_func);
for (int i = 0; i < sizeof(array) / sizeof(char *); i++) {
printf("array[%d]=%s\n", i, array[i]);
}
return 0;
}
Example 3: sort array of structures by field
Sometimes you have more complex data structures to sort, such as structs. This example illustrates sorting an array of structs by a specific field (in this case, the name).
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char name[50];
int age;
} Person;
int comparison_func(Person *a, Person *b) {
if (!a || !b) {
return 0;
}
return strcmp(a->name, b->name);
}
int main(void) {
Person array[] = {{"John von Neumann", 53}, {"Thomas Edison", 84},
{"Claude Shannon", 84}, {"Richard Feynman", 69},
{"Stanislaw Ulam", 75}, {"Julian Bigelow", 89},
{"Alan Turing", 41}};
for (int i = 0; i < sizeof(array) / sizeof(Person); i++) {
printf("array[%d] %20s, age=%d\n", i, array[i].name, array[i].age);
}
puts("Sorting...");
qsort(array, sizeof(array) / sizeof(Person), sizeof(Person),
(int (*)(const void *, const void *))comparison_func);
for (int i = 0; i < sizeof(array) / sizeof(Person); i++) {
printf("array[%d] %20s, age=%d\n", i, array[i].name, array[i].age);
}
return 0;
}
Conclusions
Now when you need to sort some data in C, your first choice will be qsort. It is especially convenient to use since it's included in the standard library.