Wednesday, July 26, 2023

Selection Sort Using C programming.

Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting the smallest (or largest) element from the unsorted portion of the list and moving it to the sorted portion of the list. 

The algorithm repeatedly selects the smallest (or largest) element from the unsorted portion of the list and swaps it with the first element of the unsorted part. This process is repeated for the remaining unsorted portion until the entire list is sorted. 

Souce Code:-

#include<stdio.h>

void SelectionSort(int a[], int n) {
    int i, j, small, temp;
    for (i=0; i<n-1; i++){
        small = i;
        for(j=i+1; j<n; j++){
            if (a[j] < a[small]){
                small = j;
            }
        }
        temp = a[i];
        a[i] = a[small];
        a[small] = temp;
    }
}

int main() {
    int n;
    printf("Enter the length of the array : ");
    scanf("%d", &n);
    int arr[n];
    printf("Enter the elements : ");
    for (int i=0; i<n; i++){
        scanf("%d", &arr[i]);
    }
    SelectionSort(arr, n);
    printf("The sorted array is :\n");
    for (int i=0; i<n; i++){
        printf("%d ", arr[i]);
    }
    return 0;
}

Output:-
Enter the length of the array : 5 Enter the elements : 7 3 9 4 1 The sorted array is : 1 3 4 7 9

Insertion Sort Using C programming.

 Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards in your hands. The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed at the correct position in the sorted part.


Insertion Sort Algorithm

To sort an array of size N in ascending order iterate over the array and compare the current element (key) to its predecessor, if the key element is smaller than its predecessor, compare it to the elements before. Move the greater elements one position up to make space for the swapped element.

Source Code:-

#include<stdio.h>

void InsertionSort(int a[], int n){
    int i, j, temp;
    for (int i=1; i<n; i++){
        temp = a[i];
        j=i-1;
        while(j>=0 && a[j]>temp){
            a[j+1] = a[j];
            j--;
        }
        a[j+1] = temp;
    }
}

int main() {
    int n;
    printf("Enter the length of the array : ");
    scanf("%d", &n);
    int arr[n];
    printf("Enter the elements : ");
    for (int i=0; i<n; i++){
        scanf("%d", &arr[i]);
    }
    InsertionSort(arr, n);
    printf("The sorted array is :\n");
    for (int i=0; i<n; i++){
        printf("%d ", arr[i]);
    }
    return 0;
}

Output :-
Enter the length of the array : 5 Enter the elements : 4 7 3 1 9 The sorted array is : 1 3 4 7 9

Bubble Sort Using C programming.

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity is quite high.


Bubble Sort Algorithm

In this algorithm, 

i. traverse from left and compare adjacent elements and the higher one is placed at right side. 

ii. In this way, the largest element is moved to the rightmost end at first. 

iii. This process is then continued to find the second largest and place it and so on until the data is sorted.


 Source Code:-

#include<stdio.h>

void BubbleSort(int *a, int n) {
    int i, j, temp;
    for (i=0; i<n-1; i++) {
        for (j=0; j<n-i-1; j++) {
            if (a[j] > a[j+1]) {
                temp = a[j];
                a[j] = a[j+1];
                a[j+1] = temp;
            }
        }
    }
}

int main() {
    int n;
    printf("Enter the length of the array : ");
    scanf("%d", &n);
    int arr[n];
    printf("Enter the elements : ");
    for (int i=0; i<n; i++){
        scanf("%d", &arr[i]);
    }
    BubbleSort(arr, n);
    printf("The sorted array is :\n");
    for (int i=0; i<n; i++){
        printf("%d ", arr[i]);
    }
    return 0;
}

output:
Enter the length of the array : 5
Enter the elements : 7 3 9 1 10
The sorted array is :
1 3 7 9 10

Selection Sort Using C programming.

Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting the smallest (or largest) element from the un...