DSA Sorting

 Selection Sort


In selection sort, the smallest value among the unsorted elements of the array is selected in every pass and inserted to its appropriate position into the array. It is also the simplest algorithm. It is an in-place comparison sorting algorithm. In this algorithm, the array is divided into two parts, first is sorted part, and another one is the unsorted part. Initially, the sorted part of the array is empty, and unsorted part is the given array. Sorted part is placed at the left, while the unsorted part is placed at the right.


In selection sort, the first smallest element is selected from the unsorted array and placed at the first position. After that second smallest element is selected and placed in the second position. The process continues until the array is entirely sorted.


Algorithm

SELECTION SORT(arr, n)  

  

Step 1: Repeat Steps 2 and 3 for i = 0 to n-1  

Step 2: CALL SMALLEST(arr, i, n, pos)  

Step 3: SWAP arr[i] with arr[pos]  

[END OF LOOP]  

Step 4: EXIT  

  

SMALLEST (arr, i, n, pos)  

Step 1: [INITIALIZE] SET SMALL = arr[i]  

Step 2: [INITIALIZE] SET pos = i  

Step 3: Repeat for j = i+1 to n  

if (SMALL > arr[j])  

     SET SMALL = arr[j]  

SET pos = j  

[END OF if]  

[END OF LOOP]  

Step 4: RETURN pos  



program to implement selection sort in C++ language.


#include <iostream>  

using namespace std;  

  

void selection(int arr[], int n)  

{  

    int i, j, small;  

    for (i = 0; i < n-1; i++)    

// One by one move boundary of unsorted subarray  

    {  

        small = i; //minimum element in unsorted array  

          

        for (j = i+1; j < n; j++)  

        if (arr[j] < arr[small])  

            small = j;  

// Swap the minimum element with the first element  

    int temp = arr[small];  

    arr[small] = arr[i];  

    arr[i] = temp;  

    }  

}  

  

void printArr(int a[], int n) /* function to print the array */  

{  

    int i;  

    for (i = 0; i < n; i++)  

        cout<< a[i] <<" ";  

}  

  

int main()  

{  

    int a[] = { 80, 10, 29, 11, 8, 30, 15 };  

    int n = sizeof(a) / sizeof(a[0]);  

    cout<< "Before sorting array elements are - "<<endl;  

    printArr(a, n);  

    selection(a, n);  

    cout<< "\nAfter sorting array elements are - "<<endl;    

    printArr(a, n);  

  

    return 0;  

  

*******


Insertion Sort


Insertion sort works similar to the sorting of playing cards in hands. It is assumed that the first card is already sorted in the card game, and then we select an unsorted card. If the selected unsorted card is greater than the first card, it will be placed at the right side; otherwise, it will be placed at the left side. Similarly, all unsorted cards are taken and put in their exact place.


The same approach is applied in insertion sort. The idea behind the insertion sort is that first take one element, iterate it through the sorted array. Although it is simple to use, it is not appropriate for large data sets as the time complexity of insertion sort in the average case and worst case is O(n2), where n is the number of items. Insertion sort is less efficient than the other sorting algorithms like heap sort, quick sort, merge sort, etc.


Algorithm

The simple steps of achieving the insertion sort are listed as follows -


Step 1 - If the element is the first element, assume that it is already sorted. Return 1.


Step2 - Pick the next element, and store it separately in a key.


Step3 - Now, compare the key with all elements in the sorted array.


Step 4 - If the element in the sorted array is smaller than the current element, then move to the next element. Else, shift greater elements in the array towards the right.


Step 5 - Insert the value.


Step 6 - Repeat until the array is sorted.


program to implement insertion sort in C++ language.


#include <iostream>  

using namespace std;  

  

void insert(int a[], int n) /* function to sort an aay with insertion sort */  

{  

    int i, j, temp;  

    for (i = 1; i < n; i++) {  

        temp = a[i];  

        j = i - 1;  

  

        while(j>=0 && temp <= a[j])  /* Move the elements greater than temp to one position ahead from their current position*/  

        {    

            a[j+1] = a[j];     

            j = j-1;    

        }    

        a[j+1] = temp;    

    }  

}  

  

void printArr(int a[], int n) /* function to print the array */  

{  

    int i;  

    for (i = 0; i < n; i++)  

        cout << a[i] <<" ";  

}  

  

int main()  

{  

    int a[] = { 89, 45, 35, 8, 12, 2 };  

    int n = sizeof(a) / sizeof(a[0]);  

    cout<<"Before sorting array elements are - "<<endl;  

    printArr(a, n);  

    insert(a, n);  

    cout<<"\nAfter sorting array elements are - "<<endl;  

    printArr(a, n);  

  

    return 0;  

}  

No comments:

Post a Comment