2020-07-01 06:24:01 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* \file
|
|
|
|
* \brief [Insertion Sort Algorithm
|
|
|
|
* (Insertion Sort)](https://en.wikipedia.org/wiki/Insertion_sort)
|
|
|
|
*
|
|
|
|
* \details
|
|
|
|
* 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
|
2020-07-01 22:10:51 +08:00
|
|
|
* 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.
|
2020-07-01 06:24:01 +08:00
|
|
|
*
|
|
|
|
* 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.
|
2020-07-01 06:26:08 +08:00
|
|
|
*
|
2020-07-01 22:10:51 +08:00
|
|
|
* Execution example steps:
|
|
|
|
* 1. Suppose initially we have
|
|
|
|
* <pre>4 3 2 5 1</pre>
|
|
|
|
* 2. We start traversing from 4 till we reach 1
|
2020-07-01 06:24:01 +08:00
|
|
|
* 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
|
2020-07-01 22:10:51 +08:00
|
|
|
* <pre>3 4 2 5 1</pre>
|
|
|
|
* 3. In the next iteration we are at 2 we find that this is also misplaced so
|
2020-07-01 06:24:01 +08:00
|
|
|
* we place it at the correct sorted position thus the array in this iteration
|
|
|
|
* becomes
|
2020-07-01 22:10:51 +08:00
|
|
|
* <pre>2 3 4 5 1</pre>
|
|
|
|
* 4. 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
|
|
|
|
* <pre>1 2 3 4 5</pre>
|
2020-07-01 06:24:01 +08:00
|
|
|
*/
|
2020-06-20 00:04:56 +08:00
|
|
|
|
2020-07-01 06:33:13 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
2020-07-01 06:35:45 +08:00
|
|
|
#include <iostream>
|
2020-07-01 22:10:51 +08:00
|
|
|
#include <vector>
|
2020-06-20 00:04:56 +08:00
|
|
|
|
2020-07-01 22:10:51 +08:00
|
|
|
/** \namespace sorting
|
|
|
|
* \brief Sorting algorithms
|
|
|
|
*/
|
|
|
|
namespace sorting {
|
2020-07-01 06:24:01 +08:00
|
|
|
/** \brief
|
|
|
|
* Insertion Sort Function
|
2020-07-01 06:26:08 +08:00
|
|
|
*
|
2020-07-01 22:19:18 +08:00
|
|
|
* @param [in,out] arr Array to be sorted
|
2020-07-01 06:24:01 +08:00
|
|
|
* @param n Size of Array
|
|
|
|
*/
|
2020-07-01 22:10:51 +08:00
|
|
|
template <typename T>
|
|
|
|
void insertionSort(T *arr, int n) {
|
2020-06-20 00:04:56 +08:00
|
|
|
for (int i = 1; i < n; i++) {
|
2020-07-01 22:10:51 +08:00
|
|
|
T temp = arr[i];
|
2020-06-20 00:04:56 +08:00
|
|
|
int j = i - 1;
|
2020-07-01 06:24:01 +08:00
|
|
|
while (j >= 0 && temp < arr[j]) {
|
|
|
|
arr[j + 1] = arr[j];
|
2020-06-20 00:04:56 +08:00
|
|
|
j--;
|
|
|
|
}
|
2020-07-01 06:24:01 +08:00
|
|
|
arr[j + 1] = temp;
|
2020-06-20 00:04:56 +08:00
|
|
|
}
|
2020-07-01 06:24:01 +08:00
|
|
|
}
|
|
|
|
|
2020-07-01 22:19:18 +08:00
|
|
|
/** Insertion Sort Function
|
|
|
|
*
|
|
|
|
* @param [in,out] arr pointer to array to be sorted
|
|
|
|
*/
|
2020-07-01 22:10:51 +08:00
|
|
|
template <typename T>
|
|
|
|
void insertionSort(std::vector<T> *arr) {
|
|
|
|
size_t n = arr->size();
|
|
|
|
|
|
|
|
for (size_t i = 1; i < n; i++) {
|
|
|
|
T temp = arr[0][i];
|
|
|
|
int32_t j = i - 1;
|
|
|
|
while (j >= 0 && temp < arr[0][j]) {
|
|
|
|
arr[0][j + 1] = arr[0][j];
|
|
|
|
j--;
|
|
|
|
}
|
|
|
|
arr[0][j + 1] = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace sorting
|
|
|
|
|
2020-07-01 06:33:13 +08:00
|
|
|
/** Test Cases to test algorithm */
|
|
|
|
void tests() {
|
2020-07-01 06:35:45 +08:00
|
|
|
int arr1[10] = {78, 34, 35, 6, 34, 56, 3, 56, 2, 4};
|
2020-07-01 22:10:51 +08:00
|
|
|
std::cout << "Test 1... ";
|
|
|
|
sorting::insertionSort(arr1, 10);
|
2020-07-01 06:35:45 +08:00
|
|
|
assert(std::is_sorted(arr1, arr1 + 10));
|
2020-07-01 22:10:51 +08:00
|
|
|
std::cout << "passed" << std::endl;
|
2020-07-01 06:33:13 +08:00
|
|
|
|
2020-07-01 06:35:45 +08:00
|
|
|
int arr2[5] = {5, -3, 7, -2, 1};
|
2020-07-01 22:10:51 +08:00
|
|
|
std::cout << "Test 2... ";
|
|
|
|
sorting::insertionSort(arr2, 5);
|
2020-07-01 06:35:45 +08:00
|
|
|
assert(std::is_sorted(arr2, arr2 + 5));
|
2020-07-01 22:10:51 +08:00
|
|
|
std::cout << "passed" << std::endl;
|
|
|
|
|
|
|
|
float arr3[5] = {5.6, -3.1, -3.0, -2.1, 1.8};
|
|
|
|
std::cout << "Test 3... ";
|
|
|
|
sorting::insertionSort(arr3, 5);
|
|
|
|
assert(std::is_sorted(arr3, arr3 + 5));
|
|
|
|
std::cout << "passed" << std::endl;
|
|
|
|
|
|
|
|
std::vector<float> arr4({5.6, -3.1, -3.0, -2.1, 1.8});
|
|
|
|
std::cout << "Test 4... ";
|
|
|
|
sorting::insertionSort(&arr4);
|
|
|
|
assert(std::is_sorted(std::begin(arr4), std::end(arr4)));
|
|
|
|
std::cout << "passed" << std::endl;
|
2020-07-01 06:33:13 +08:00
|
|
|
}
|
|
|
|
|
2020-07-01 06:24:01 +08:00
|
|
|
/** Main Function */
|
|
|
|
int main() {
|
2020-07-01 06:35:45 +08:00
|
|
|
/// Running predefined tests to test algorithm
|
|
|
|
tests();
|
2020-07-01 06:33:13 +08:00
|
|
|
|
2020-07-01 06:35:45 +08:00
|
|
|
/// For user insteraction
|
2020-07-01 22:10:51 +08:00
|
|
|
size_t n;
|
|
|
|
std::cout << "Enter the length of your array (0 to exit): ";
|
2020-07-01 06:35:45 +08:00
|
|
|
std::cin >> n;
|
2020-07-01 22:10:51 +08:00
|
|
|
if (n == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-01 06:35:45 +08:00
|
|
|
int *arr = new int[n];
|
|
|
|
std::cout << "Enter any " << n << " Numbers for Unsorted Array : ";
|
2020-07-01 06:24:01 +08:00
|
|
|
|
2020-07-01 18:10:51 +08:00
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
std::cin >> arr[i];
|
|
|
|
}
|
2020-07-01 06:24:01 +08:00
|
|
|
|
2020-07-01 22:10:51 +08:00
|
|
|
sorting::insertionSort(arr, n);
|
2020-06-20 00:04:56 +08:00
|
|
|
|
2020-07-01 18:10:51 +08:00
|
|
|
std::cout << "\nSorted Array : ";
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
std::cout << arr[i] << " ";
|
|
|
|
}
|
2020-06-20 00:04:56 +08:00
|
|
|
|
2020-07-01 06:35:45 +08:00
|
|
|
std::cout << std::endl;
|
|
|
|
delete[] arr;
|
|
|
|
return 0;
|
2020-06-20 00:04:56 +08:00
|
|
|
}
|