Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
insertion_sort.cpp File Reference

Insertion Sort Algorithm (Insertion Sort) More...

#include <algorithm>
#include <cassert>
#include <iostream>
Include dependency graph for insertion_sort.cpp:

Functions

void insertionSort (int *arr, int n)
 Insertion Sort Function. More...
 
void tests ()
 
int main ()
 

Detailed Description

Insertion Sort Algorithm (Insertion Sort)

Author

Insertion sort is a simple sorting algorithm that builds the final sorted array one at a time. It is much less efficient compared to other sorting algorithms like heap sort, merge sort or quick sort. However it has several advantages such as

1 - easy to implement
2 - For small set of data it is quite efficient
3 - More efficient that other Quadratic complexity algorithms like
    Selection sort or bubble sort.
4 - It's stable that is it does not change the relative order of
    elements with equal keys
5 - Works on hand means it can sort the array or list as it receives.

It is based on the same idea that people use to sort the playing cards in their hands. the algorithms goes in the manner that we start iterating over the array of elements as soon as we find a unsorted element that is a misplaced element we place it at a sorted position.

Suppose initially we have

4 3 2 5 1
we start traversing from 4 till we reach 1
when we reach at 3 we find that it is misplaced so we take 3 and place
it at a correct position thus the array will become
3 4 2 5 1
in the next iteration we are at 2 we find that this is also misplaced so
we place it at the correct sorted position thus the array in this iteration
becomes
2 3 4 5 1
we does not do anything with 5 and move on to the next iteration and select
1 which is misplaced and place it at correct position. Thus, we have
1 2 3 4 5

Function Documentation

◆ insertionSort()

void insertionSort ( int *  arr,
int  n 
)

Insertion Sort Function.

Parameters
arrArray to be sorted
nSize of Array
65  {
66  for (int i = 1; i < n; i++) {
67  int temp = arr[i];
68  int j = i - 1;
69  while (j >= 0 && temp < arr[j]) {
70  arr[j + 1] = arr[j];
71  j--;
72  }
73  arr[j + 1] = temp;
74  }
75 }

◆ main()

int main ( )

Main Function

Running predefined tests to test algorithm

For user insteraction

91  {
92  /// Running predefined tests to test algorithm
93  tests();
94 
95  /// For user insteraction
96  int n;
97  std::cout << "Enter the length of your array : ";
98  std::cin >> n;
99  int *arr = new int[n];
100  std::cout << "Enter any " << n << " Numbers for Unsorted Array : ";
101 
102  for (int i = 0; i < n; i++) {
103  std::cin >> arr[i];
104  }
105 
106  insertionSort(arr, n);
107 
108  std::cout << "\nSorted Array : ";
109  for (int i = 0; i < n; i++) {
110  std::cout << arr[i] << " ";
111  }
112 
113  std::cout << std::endl;
114  delete[] arr;
115  return 0;
116 }
Here is the call graph for this function:

◆ tests()

void tests ( )

Test Cases to test algorithm

78  {
79  int arr1[10] = {78, 34, 35, 6, 34, 56, 3, 56, 2, 4};
80  insertionSort(arr1, 10);
81  assert(std::is_sorted(arr1, arr1 + 10));
82  std::cout << "Test 1 Passed" << std::endl;
83 
84  int arr2[5] = {5, -3, 7, -2, 1};
85  insertionSort(arr2, 5);
86  assert(std::is_sorted(arr2, arr2 + 5));
87  std::cout << "Test 2 Passed" << std::endl;
88 }
Here is the call graph for this function:
std::is_sorted
T is_sorted(T... args)
std::cout
std::endl
T endl(T... args)
insertionSort
void insertionSort(int *arr, int n)
Insertion Sort Function.
Definition: insertion_sort.cpp:65
tests
void tests()
Definition: insertion_sort.cpp:78
std::cin