Singly Linked List in C with Code and Explanation

Image
                        Singly LinkedList (SLL)   A Singly Linked List is a linear data structure where each element (called a node ) points to the next node in the sequence.  It is a dynamic structure , meaning it can grow or shrink in size during runtime .  Structure of a Node:  Each node contains: - Data : The value stored in the node. - Next : A pointer / reference to the next node in the list . There are Five operation in SLL: 1) Insert - Insert the element in our node like, insert(34);                                                                            insert(3);                                       ...

Selection sort (code & Algorithm)

In this blog you have to know all about the Selection sort, Step by step how to work Selection sort Algorithm and also you have to know how to write code in an easy way for selection sort.

In Selection sort we are arranging unordered number in a correct order,
Examples: This is unordered number (9,6,4,7,3,2,4,5,) but by using selection sort we can arrange in ascending order (2,3,4,5,6,7,9) this is our selection sort, to arrange in an order of given unorder number.

      Algorithm:


f
Code for Selection sort:

#include <stdio.h>

// Function to perform selection sort on an array
void selectionSort(int arr[], int n) {
    int i, j, min_idx, temp;

    // One by one move boundary of unsorted subarray
    for (i = 0; i < n-1; i++) {
        // Find the minimum element in unsorted array
        min_idx = i;
        for (j = i+1; j < n; j++) {
            if (arr[j] < arr[min_idx]) {
                min_idx = j;
            }
        }

        // Swap the found minimum element with the first element
        temp = arr[min_idx];
        arr[min_idx] = arr[i];
        arr[i] = temp;
    }
}

// Function to print an array
void printArray(int arr[], int n) {
    int i;
    for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main() {
    int arr[] = {64, 25, 12, 22, 11};
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("Original array: \n");
    printArray(arr, n);

    selectionSort(arr, n);

    printf("Sorted array: \n");
    printArray(arr, n);

    return 0;
}

Output:
                     Original array: 64 25 12 22 11 
                      Sorted array: 11 12 22 25 64 

 Up to this it's all about Selection Sort.


Comments

Popular posts from this blog

Multiplication of two number by using C Programs

Addition of two Number using c language code