C language Bubble sort for AWS Lightsail (or other Linux machines)

C language sort created on an AWS Lightsail instance

Here’s a C program for the Bubble Sort algorithm that sorts an array of integers in ascending order:

#include <stdio.h>

void bubbleSort(int arr[], int n) {
    int i, j, temp;
    for (i = 0; i < n - 1; i++) {
        for (j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                // Swap arr[j] and arr[j+1]
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

int main() {
    int arr[] = {6, 2, 9, 1, 5, 3};
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("Original Array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    // Sort the array using Bubble Sort
    bubbleSort(arr, n);

    printf("\nSorted Array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    printf("\n");

    return 0;
}

To run this program on your AWS Lightsail server, follow these steps: You can probably run this on most Linux distributions but I’ve created and tested this only on a lightsail instance.

  1. Connect to your AWS Lightsail server via SSH.
  2. Use a text editor (e.g., nano, vim, emacs) to create a new file. Let’s call it bubble_sort.c.
  3. Copy and paste the C code into the bubble_sort.c file.
  4. Save the file and close the text editor.
  5. Compile the C program using a C compiler like gcc:
gcc -o bubble_sort bubble_sort.c
  1. Once the compilation is successful, execute the program:
./bubble_sort

The program will print the original array, sort it using the Bubble Sort algorithm, and then print the sorted array on the console.

Here’s the program with the bubble_sort code inline.

#include <stdio.h>

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");

    // Bubble sort logic integrated into the main code
    for (int i = 0; i < n - 1; i++) {
        int swapped = 0;
        for (int j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                // Swap arr[j] and arr[j + 1]
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
                swapped = 1;
            }
        }

        // If no two elements were swapped in the inner loop, the array is already sorted
        if (swapped == 0) {
            break;
        }
    }

    printf("Sorted array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    return 0;
}

This code will sort the arr array using the bubble sort algorithm without explicitly calling a separate function for it. The bubble sort logic is implemented directly within the main function.

Leave a Reply

Your email address will not be published. Required fields are marked *