Tuesday 15 October 2024

Insertion Sort

  Sort the following data by using insertion sort. 18, 7, 22, 3, 14, 2

Insertion Sort Algorithm in C

  • Start with the second element (index 1) as the key.

  • Compare the key with the elements before it.

  • If the key is smaller, shift the compared element right.

  • Insert the key where the shifting of elements stops.

  • Repeat steps 2-4 for all elements in the unsorted part of the list.

Insertion Sort Logic:

  • For each element, we compare it with the elements before it.

  • If the previous elements are larger, we shift them right.

  • We insert the current element (key) in the correct position.

Sorting Process:

Step-by-Step Execution

  • Initial Array: {18, 7, 22, 3, 14, 2}

Pass 1:

  • Key = 7, compare with 18 → Shift 18 → Insert 7
    Array: {7, 18, 22, 3, 14, 2}

Pass 2:

  • Key = 22, no shifting needed → Array stays the same

Pass 3:

  • Key = 3, compare with 22, 18, 7 → Shift all → Insert 3
    Array: {3, 7, 18, 22, 14, 2}

Pass 4:

  • Key = 14, compare with 22, 18 → Shift 22, 18 → Insert 14
    Array: {3, 7, 14, 18, 22, 2}

Pass 5:

  • Key = 2, compare with all → Shift all → Insert 2
    Array: {2, 3, 7, 14, 18, 22}


#include <stdio.h>

int main() {

    int A[6] = {18, 7, 22, 3, 14, 2}; // Initialize the array

    int i, j, key;


   // Sorting the array using Insertion Sort logic 

for (i = 1; i < 6; i++)

 {

 key = A[i]; // Store the current element as the key 


 for (j = i-1;  A[j] > key; j--) // for (j = i - 1; j >= 0 && A[j] > key; j--)

 {

 A[j + 1] = A[j]; // Move element to the right

 }

 A[j + 1] = key; // Insert the key at the correct position

 }

    // Display the sorted array

    printf("After Sorting: ");

    for (i = 0; i < 6; i++) {

        printf("%d ", A[i]);

    }

    printf("\n");


    return 0;

}

Example of j in Action

  • When i = 1 (key = 7):

    • j starts at 0: Compare key (7) with A[0] (18).

    • Since 18 > 7, shift 18 to the right (A[1] = A[0]).

  • When i = 2 (key = 22):

    • j starts at 1: Compare key (22) with A[1] (18).

    • Since 22 is not less than 18, no shifting occurs; 22 remains in place.

  • When i = 3 (key = 3):

    • j starts at 2: Compare key (3) with A[2] (22).

    • Since 22 > 3, shift 22 to the right (A[3] = A[2]).

    • j becomes 1: Compare key (3) with A[1] (18).

    • Since 18 > 3, shift 18 to the right (A[2] = A[1]).

    • j becomes 0: Compare key (3) with A[0] (7).

    • Since 7 > 3, shift 7 to the right (A[1] = A[0]).

    • Insert key (3) at A[0].

  • When i = 4 (key = 14):

    • j starts at 3: Compare key (14) with A[3] (22).

    • Since 22 > 14, shift 22 to the right (A[4] = A[3]).

    • j becomes 2: Compare key (14) with A[2] (18).

    • Since 18 > 14, shift 18 to the right (A[3] = A[2]).

    • j becomes 1: Compare key (14) with A[1] (7).

    • Since 7 is not greater than 14, insert key (14) at A[2].

  • When i = 5 (key = 2):

    • j starts at 4: Compare key (2) with A[4] (22).

    • Since 22 > 2, shift 22 to the right (A[5] = A[4]).

    • j becomes 3: Compare key (2) with A[3] (18).

    • Since 18 > 2, shift 18 to the right (A[4] = A[3]).

    • j becomes 2: Compare key (2) with A[2] (14).

    • Since 14 > 2, shift 14 to the right (A[3] = A[2]).

    • j becomes 1: Compare key (2) with A[1] (7).

    • Since 7 > 2, shift 7 to the right (A[2] = A[1]).

    • j becomes 0: Compare key (2) with A[0] (3).

    • Since 3 > 2, shift 3 to the right (A[1] = A[0]).

    • Insert key (2) at A[0].



Selection sort

 Sort the following data by using selection sort 12, 11, 13, 5, 6

Steps for Selection sort:

  1. Find the minimum element in the array and swap it with the first element.

  2. Repeat the process for the remaining unsorted part of the array.

Sorting Process:

Initial array: 12, 11, 13, 5, 6

  1. Step 1 (Find min in 12, 11, 13, 5, 6):

    • Min = 5

    • Swap 5 with the first element (12).

    • Array: 5, 11, 13, 12, 6

  2. Step 2 (Find min in 11, 13, 12, 6):

    • Min = 6

    • Swap 6 with the second element (11).

    • Array: 5, 6, 13, 12, 11

  3. Step 3 (Find min in 13, 12, 11):

    • Min = 11

    • Swap 11 with the third element (13).

    • Array: 5, 6, 11, 12, 13

  4. Step 4 (Find min in 12, 13):

    • Min = 12

    • No need to swap as 12 is already in place.

    • Array: 5, 6, 11, 12, 13

Now the array is sorted: 5, 6, 11, 12, 13



#include <stdio.h>


int main() {

    int A[5] = {12, 11, 13, 5, 6}; // Initialize the array

    int i, j, x;


    // Sorting the array using Selection Logic

    for (i = 0; i < 5; i++) // outer loop i = 0 to i < 5 (i.e., 0 to 4).

      {

        for (j = i + 1; j < 5; j++)//inner loop for comparison 

       {

            if (A[i] > A[j]) {

                // Swap elements A[i] and A[j]

                x = A[i];

                A[i] = A[j];

                A[j] = x;

            }

        }

    }


    // Display the sorted array

    printf("After Sorting: ");

    for (i = 0; i < 5; i++) {

        printf("%d ", A[i]);

    }

    printf("\n");


    return 0;

}




Explanation 

  1. Step 1 (Find minimum in 12, 11, 13, 5, 6):

    • Outer loop (i = 0): The first element is 12.

    • Inner loop compares it with the rest:

      • Compare 12 with 11 → 12 > 11, so no swap yet.

      • Compare 12 with 13 → No change.

      • Compare 12 with 5 → 12 > 5, swap 12 with 5.

      • Array becomes: {5, 11, 13, 12, 6}


  1. Step 2 (Find minimum in 11, 13, 12, 6):

    • Outer loop (i = 1): The current element is 11.

    • Inner loop compares it with the rest:

      • Compare 11 with 13 → No change.

      • Compare 11 with 12 → No change.

      • Compare 11 with 6 → 11 > 6, swap 11 with 6.

      • Array becomes: {5, 6, 13, 12, 11}


  1. Step 3 (Find minimum in 13, 12, 11):

    • Outer loop (i = 2): The current element is 13.

    • Inner loop compares it with the rest:

      • Compare 13 with 12 → 13 > 12, swap 13 with 12.

      • Array becomes: {5, 6, 12, 13, 11}

      • Compare 12 with 11 → 12 > 11, swap 12 with 11.

      • Array becomes: {5, 6, 11, 13, 12}

  1. Step 4 (Find minimum in 13, 12):

    • Outer loop (i = 3): The current element is 13.

    • Inner loop compares it with 12:

      • 13 > 12, so swap 13 with 12.

      • Array becomes: {5, 6, 11, 12, 13}


  1. Step 5 (Only one element left):

    • Outer loop (i = 4): No comparison needed, as only one element remains.