Sorting_2
Sorting_2
Insertion Sort
Selection Sort
The selection sort algorithm sorts an array by repeatedly finding the
minimum element (considering ascending order) from unsorted part
and putting it at the beginning. The algorithm maintains two subarrays
in a given array.
1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.
1. Consider a situation where swap operation is very costly. Which of the following
sorting algorithms should be preferred so that the number of swap operations are
minimized in general?->SELECTION
2. Which sorting algorithm will take least time when all elements of input array are
identical? Consider typical implementations of sorting algorithms.->INSERTION
3. Which of the following sorting algorithms has the lowest worst-case complexity?-
>MERGE
4. https://www.geeksforgeeks.org/time-and-space-complexity-analysis-of-merge-
sort/
Insertion Sort
Insertion Sort
#include <stdio.h>
#include <math.h> // A utility function to print an array
of
/* Function to sort an array using insertion size n
sort*/ void printArray(int arr[], int n)
void insertionSort(int arr[], int n) {
{ int i;
int i, key, j; for (i=0; i < n; i++)
for (i = 1; i < n; i++) printf("%d ", arr[i]);
{ printf("\n");
key = arr[i]; }
j = i-1;