## 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

**Selection Sort****Bubble sort****Insertion sort****Quick Sort****Shell Sort****Merge Sort etc.**

## Insertion Sort Algorithm

In **insertion sort, **the element is **insert at an appropriate place **similar to card insertion.

So, Here the** list is divid**e into two parts sort and **unsort sub-lists.**

Than, n each pass, the **first element of the unsorted** sublist is pick up and move into the sorted sub-list by inserting it in **a suitable position.**

Suppose we have ‘n’ elements, we **need n-1 passes **to sort the elements.

## Insertion Sort Algorithm Steps

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

## Example:

## Insertion Sort 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
}
}
```

## The complexity of Insertion sort: O(n_{2})

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

So, try it your self on online compiler.

```
#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[0]);
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
```

## Insertion sort

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

Moreover, The array is search sequentially and unsort items are move and insert into the sort sub-list (in the same array). So, 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.

Than, If you want to read more …….

so, visit our website and click here:-https://learningpoints.in/

thank you for coming to our website please share with your friends And do comment

Thank you.