Basic Sorting Algorithm

Learn basic sorting algorithms like Bubble, Selection, and Insertion Sort with simple explanations and examples
3 min read

Basic Sorting Algorithm
Basic Sorting Algorithm
Sorting

Sorting is a process to arrenge the array elements in ascending or descending order.
Sorting is used to organise data.
Example :- array[] = { 5,4,6,7,8};
                   array[] = {4,5,6,7,8};

Types of Sorting

1. Bubble sort
2. Selection sort
3. Insertion sort

Bubble sort

It is simplest sorting but slow, It Compare nearest elements repeatedly.

Steps for bubble sorting 

Start from first element and compare at first and second element.
If the first element is greater, then is second element there are swap.

Now, compare second and third element and swap them if there are not in order.
This process repeated the above until the last elements. 

Bubble sort
Bubble sort

Example:

#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]) {
                // Swap
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}
void printArray(int arr[], int n) {
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("\n");
}
int main() {
    int arr[] = {5, 3, 8, 1, 2};
    int n = sizeof(arr) / sizeof(arr[0]);
    bubbleSort(arr, n);
    printf("Sorted array: ");
    printArray(arr, n);
return 0;
}

Result: Sorted array: 1 2 3 5 8

Selection sort

In this sort, set the 1st elements as sorted and Find the smallest element and swap it with the first element, then repeat.

Steps for selection sort

Set the first element as the sorted and remaining elements as unsorted.
Compare the sorted elements with the remaining unsorted elements and swap with the sorted element.

Repeat this step for all remaining elements.
After each element as swap on the sorted elements of unsorted list.

Selection sort
Selection sort

Example

#include <stdio.h>
void selectionSort(int arr[], int n) {
    for (int i = 0; i < n - 1; i++) {
        int minIndex = i;
        for (int j = i + 1; j < n; j++) {
            if (arr[j] < arr[minIndex])
                minIndex = j;
        }
        // Swap
        int temp = arr[minIndex];
        arr[minIndex] = arr[i];
        arr[i] = temp;
    }
}
void printArray(int arr[], int n) {
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("\n");
}
int main() {
    int arr[] = {5, 3, 8, 1, 2};
    int n = sizeof(arr) / sizeof(arr[0]);
    selectionSort(arr, n);
    printf("Sorted array: ");
    printArray(arr, n);
    return 0;
}

Result: Sorted array: 1 2 3 5 8

Insertion sort

In this sort, Take one element and insert it at the correct position in the sorted part of the array.

Steps for insertion sort:

Divide into two part first element as sorted and remaining elements as unsorted.
Now compare the sorted element with the unsorted elements.

Find the correct position and swap the sorted elements and comparing element
Repeat this process until the remain all elements of unsorted part.

Insertion sort
Insertion sort

Example
#include <stdio.h>
void insertionSort(int arr[], int n) {
    for (int i = 1; i < n; i++) {
        int key = arr[i];
        int j = i - 1;
        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j--;
        }
        arr[j + 1] = key;
    }
}
void printArray(int arr[], int n) {
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("\n");
}
int main() {
    int arr[] = {5, 3, 8, 1, 2};
    int n = sizeof(arr) / sizeof(arr[0]);
    insertionSort(arr, n);
    printf("Sorted array: ");
    printArray(arr, n);
    return 0;
}

Result: Sorted array: 1 2 3 5 8

You may like these posts

  • Strings in C language StringsString is a sequence of character that is considered as a single data item.String is representing of character array.Declaration char string …
  • Basic Sorting AlgorithmSortingSorting is a process to arrenge the array elements in ascending or descending order.Sorting is used to organise data.Example :- array[] = { 5,4,6…
  • Loops in C LanguageLoopsLoops is statement. Which is use for to reduce the code and also reduce the run time of the code. If we have sequentially statement then use a loop.Typ…
  • Operators and Operands in C LanguageOperators Operators is a symbol that is used to perform certain mathematical or logical operations on data and variables.  They&n…
  • Pointers in C LanguagePointersPointer is a derived data type in c language. It can store the address of memory where program instructions or data are stored.For example:- if  …
  • Function and Recursion in C languageUses of Function and Recursion When sometime Our program bigger in size and it becomes difficult to know what the program does. To sol…

Post a Comment