Implementing Sorting Algorithms in Linux
Now that we have a basic understanding of sorting algorithms, let's explore how to implement them in the Linux environment. In this section, we will walk through several examples of implementing popular sorting algorithms using the C programming language and the Linux command-line tools.
Implementing Bubble Sort in Linux
Bubble Sort is a simple comparison-based sorting algorithm that repeatedly swaps adjacent elements if they are in the wrong order. Here's an example of implementing Bubble Sort in C on Ubuntu 22.04:
#include <stdio.h>
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
bubbleSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
This code demonstrates the implementation of the Bubble Sort algorithm in C, which can be compiled and executed on an Ubuntu 22.04 system.
Implementing Quick Sort in Linux
Quick Sort is a comparison-based sorting algorithm that works by selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. Here's an example of implementing Quick Sort in C on Ubuntu 22.04:
#include <stdio.h>
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
quickSort(arr, 0, n - 1);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
This code demonstrates the implementation of the Quick Sort algorithm in C, which can be compiled and executed on an Ubuntu 22.04 system.
In addition to implementing sorting algorithms in code, Linux also provides several built-in command-line tools that can be used for sorting data. One of the most commonly used tools is the sort
command, which can be used to sort the contents of a file or the output of other commands.
Here's an example of using the sort
command to sort a list of numbers in ascending order:
echo "64 34 25 12 22 11 90" | sort -n
This will output the sorted list of numbers:
11 12 22 25 34 64 90
The -n
option in the sort
command tells it to sort the input numerically, rather than alphabetically.