08/16/2022 ## What is Sorting?

Sorting refers to arranging data in a particular format. The sorting algorithm specifies the way to.
arrange data in a particular order.
Sorting is nothing but storage of data in sorted order, Types of Sorting descending
order.
Sorting arranges data in a sequence which makes searching easier.

## Complexity of Sorting algorithm

The complexity of the sorting algorithm calculates the running time of a function in which ‘n’ number of items is to be sorting.
The choice for which sorting method is suitable for a problem depends on several dependencies.
Configurations for different problems.
The most noteworthy of these considerations are:
The length of time spent by the programmer in.
So, Programming a specific sorting program.
Then, the Amount of machine time necessary for running the program.
The amount of memory necessary for running the program.

## Types of Sorting

1. Selection Sort
2. Bubble sort
3. Insertion sort
4. Quick Sort
5. Shell Sort
6. Merge Sort etc.

## Insertion Sort Algorithm

So, In insertion sort, the element is inserting at an appropriate place similar to card insertion.
moreover, Here the list is divide into two parts sort and unsorted sub-lists.
So, n each pass, the first element of the unsorted sublist is pick up and move into the sorted sub-list by inserting it in the suitable position.
Suppose we have ‘n’ elements, we need n-1 passes to sort the elements.

## Insertion Sort Algorithm Steps

1. Firstly, Pass 1: A by itself is trivially sorted.
2. Secondly, Pass 2: A is inserted either before or after A so that; A, A is Sorted.
3. Thirdly, Pass 3: A is inserted into its proper place in A, A that is before A , between A and A, or after A, So that A, A, A is sorted.
4. Pass N: A[N-1] is inserted into the proper place in A, A, A….A[N-1] is Sorted.

## Algorithm

``````Insertion_sort(A, n)
{
For i=1 to n-1
{
Value= A[i]
Key=i
While(key>=0 && A[key-1]>value)
{
A[key]=A[key-1]
Key=key-1
}
A[key]=value
}
}``````

### Then, Using C programming to perform this sorting operation you can follow any language.

So, try it your self on online compiler.

``````// C program for insertion sort
#include <math.h>
#include <stdio.h>

/* Function to sort an array using insertion
sort*/
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;

/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
// A utility function to print an array of size n
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

/* Driver program to test insertion sort */
int main()
{
int arr[] = { 12, 11, 13, 5, 6 };
int n = sizeof(arr) / sizeof(arr);

insertionSort(arr, n);
printArray(arr, n);

return 0;
}

``````

## Insertion sort

So, This is an in-place comparison-base sorting algorithm. Than, Here, a sub-list is maintain which is always sorting. For example, the lower part of an array is maintain to be sorting. An element that is to be ‘inserting in this sorted sub-list, has to find its appropriate place, and then it has to be inserted there. Hence the name, insertion sort.

So, The array is search sequentially and unsorted items are move and insert into the sorted sub-list (in the same array). Than, This algorithm is not suitable for large data sets as its average and worst-case complexity are of Ο(n2), where n is the number of items.