logo of SA Coder
        Menu
sign in

      References



Sort



Sorting is an essential operation in computer programming that arranges elements in a specific order. Sorting algorithms are used to sort various data types, such as numbers, characters, and strings, in ascending or descending order. In this post, we will discuss three common sorting algorithms in C programming: bubble sort, selection sort, and insertion sort.


Bubble Sort



Bubble sort is a simple and intuitive algorithm that compares adjacent elements and swaps them if they are not in the right order. The algorithm repeats this process until all elements are in the correct order. Here is an example of how to implement bubble sort in C.


copy

#include <stdio.h>

int main()
{
    int n, temp;
    printf("Enter the array size\n");
    scanf("%d", &n);
    int num[n];
    printf("Enter %d number\n", n);

    // input n number
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &num[i]);
    }

    // sort by bubble sort
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n - i - 1; j++)
        {
            if (num[j] > num[j + 1]) // swapping the value
            {
                temp = num[j];
                num[j] = num[j + 1];
                num[j + 1] = temp;
            }
        }
    }

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

    return 0;
}


Selection Sort



Selection sort is an algorithm that selects the smallest element and places it in the first position. Then, it selects the second smallest element and places it in the second position, and so on until all elements are sorted. Here is an example of how to implement selection sort in C.


copy

#include <stdio.h>

int main()
{
    int n;
    printf("Enter size of array\n");
    scanf("%d", &n);
    int sel[n];
    printf("\nEnter %d array :\n\n", n);

    // input n number
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &sel[i]);
    }

    // selection sort algorithm
    for (int i = 0; i < n - 1; i++)
    {
        int min_idx = i;
        for (int j = i + 1; j < n; j++)
        {
            if (sel[j] < sel[min_idx])
            {
                min_idx = j;
            }
        }
        int temp = sel[min_idx];
        sel[min_idx] = sel[i];
        sel[i] = temp;
    }

    // display sorted array
    printf("\nSorted Array:\n");
    for (int i = 0; i < n; i++)
    {
        printf(" %d ", sel[i]);
    }
    return 0;
}


Insertion Sort



The Insertion Sort Algorithm is a comparison-based sorting algorithm in which an array is divided into a sorted and an unsorted part. Initially, the sorted part contains the first element of the array, and the unsorted part contains the rest of the elements. The algorithm then picks one element from the unsorted part and inserts it into its correct position in the sorted part by shifting the larger elements to the right. This process is repeated until all the elements are sorted.



Example Code


Let's take a look at an example code that implements the Insertion Sort Algorithm in C language:


copy

#include <stdio.h>
#include <conio.h>

void main() {
    int n, temp, j, i, a = 0;
    printf("Enter size of array\n");
    scanf("%d", &n);
    int sort[n];
    printf("\nEnter %d array :\n\n", n);

    // input n number
    for (i = 0; i < n; i++) {
        scanf("%d", &sort[i]);
    }

    // sort by insertion sort
    for (i = 1; i < n; i++) {
        temp = sort[i];
        j = i - 1;
        for(;(temp < sort[j]) && (j >= 0);j--,a++) {
            sort[j + 1] = sort[j];
        }
        sort[j + 1] = temp;
    }

    // display sorted array
    for (i = 0; i < n; i++) {
        printf(" %d ", sort[i]);
    }
    getch();
}



The above code asks the user to enter the size of the array and the array elements. It then sorts the array using the Insertion Sort Algorithm and displays the sorted array.




Please login first to comment.