diff --git a/d5/d91/namespacesorting.html b/d5/d91/namespacesorting.html index 6be87fa04..6c6d6d518 100644 --- a/d5/d91/namespacesorting.html +++ b/d5/d91/namespacesorting.html @@ -102,6 +102,13 @@ $(document).ready(function(){initNavTree('d5/d91/namespacesorting.html','../../' + + + + + + + @@ -135,6 +142,108 @@ Functions

Detailed Description

Sorting algorithms.

Function Documentation

+ +

◆ insertionSort() [1/2]

+ +
+
+
+template<typename T >
+

Functions

template<typename T >
void insertionSort (T *arr, int n)
 Insertion Sort Function. More...
 
template<typename T >
void insertionSort (std::vector< T > *arr)
 
template<class Iterator >
void merge (Iterator l, Iterator r, const Iterator e, char b[])
 merges 2 sorted adjacent segments into a larger sorted segment More...
+ + + + + + + +
void sorting::insertionSort (std::vector< T > * arr)
+
+

Insertion Sort Function

+
Template Parameters
+ + +
Ttype of array
+
+
+
Parameters
+ + +
[in,out]arrpointer to array to be sorted
+
+
+
77  {
+
78  size_t n = arr->size();
+
79 
+
80  for (size_t i = 1; i < n; i++) {
+
81  T temp = arr[0][i];
+
82  int32_t j = i - 1;
+
83  while (j >= 0 && temp < arr[0][j]) {
+
84  arr[0][j + 1] = arr[0][j];
+
85  j--;
+
86  }
+
87  arr[0][j + 1] = temp;
+
88  }
+
89 }
+
+
+ + +

◆ insertionSort() [2/2]

+ +
+
+
+template<typename T >
+ + + + + + + + + + + + + + + + + + +
void sorting::insertionSort (T * arr,
int n 
)
+
+ +

Insertion Sort Function.

+
Template Parameters
+ + +
Ttype of array
+
+
+
Parameters
+ + + +
[in,out]arrArray to be sorted
nSize of Array
+
+
+
59  {
+
60  for (int i = 1; i < n; i++) {
+
61  T temp = arr[i];
+
62  int j = i - 1;
+
63  while (j >= 0 && temp < arr[j]) {
+
64  arr[j + 1] = arr[j];
+
65  j--;
+
66  }
+
67  arr[j + 1] = temp;
+
68  }
+
69 }
+
+
+

◆ merge()

diff --git a/d7/db9/hill__cipher_8cpp.html b/d7/db9/hill__cipher_8cpp.html index d66d94324..a9c6d7cb3 100644 --- a/d7/db9/hill__cipher_8cpp.html +++ b/d7/db9/hill__cipher_8cpp.html @@ -415,6 +415,7 @@ Here is the call graph for this function:
ans
ll ans(ll n)
Definition: matrix_exponentiation.cpp:91
FenwickTree
Definition: fenwick_tree.cpp:17
print
void print(uint32_t N)
Definition: sieve_of_eratosthenes.cpp:40
+
sorting::insertionSort
void insertionSort(T *arr, int n)
Insertion Sort Function.
Definition: insertion_sort.cpp:59
HASHMAX
#define HASHMAX
Determines the length of the hash table.
Definition: hash_search.cpp:22
PRIME
#define PRIME
Prime modulus for hash functions.
Definition: rabin_karp.cpp:16
sorting::partition
int partition(int arr[], int low, int high)
Definition: quick_sort.cpp:37
@@ -435,7 +436,7 @@ Here is the call graph for this function:
std::reverse
T reverse(T... args)
main
int main(void)
Definition: qr_decomposition.cpp:23
main
int main()
Definition: palindrome_of_number.cpp:19
-
sorting
Sorting algorithms.
Definition: non_recursive_merge_sort.cpp:11
+
sorting
Sorting algorithms.
main
int main()
Definition: vector_important_functions.cpp:11
std::queue
STL class.
sorting::merge
void merge(Iterator, Iterator, const Iterator, char[])
merges 2 sorted adjacent segments into a larger sorted segment
Definition: non_recursive_merge_sort.cpp:57
@@ -483,6 +484,7 @@ Here is the call graph for this function:
std::cout
qr_decompose.h
Library functions to compute QR decomposition of a given matrix.
merge
void merge(int *arr, int l, int m, int r)
Definition: merge_sort.cpp:33
+
create_random_array
static void create_random_array(T *arr, int N)
Create a random array objecthelper function to create a random array.
Definition: insertion_sort.cpp:101
h
int h(int key)
Definition: hash_search.cpp:45
std::ofstream
STL class.
sorting::non_recursive_merge_sort
void non_recursive_merge_sort(const Iterator first, const Iterator last)
bottom-up merge sort which sorts elements in a non-decreasing order
Definition: non_recursive_merge_sort.cpp:86
@@ -551,7 +553,6 @@ Here is the call graph for this function:
std::left
T left(T... args)
create_list
void create_list(int key)
Definition: hash_search.cpp:55
fact
double fact(double x)
Definition: poisson_dist.cpp:30
-
insertionSort
void insertionSort(int *arr, int n)
Insertion Sort Function.
Definition: insertion_sort.cpp:65
std::exp
T exp(T... args)
std::string::begin
T begin(T... args)
std::getline
T getline(T... args)
@@ -567,14 +568,14 @@ Here is the call graph for this function:
power
vector< vector< ll > > power(const vector< vector< ll >> &A, ll p)
Definition: matrix_exponentiation.cpp:76
main
int main()
Definition: successive_approximation.cpp:20
std::count
T count(T... args)
-
tests
void tests()
Definition: insertion_sort.cpp:78
+
tests
void tests()
Definition: insertion_sort.cpp:109
LinearSearch
int LinearSearch(int *array, int size, int key)
Definition: linear_search.cpp:16
std::vector::assign
T assign(T... args)
IsPrime
bool IsPrime(int number)
Definition: primality_test.cpp:18
nCr
double nCr(double n, double r)
Definition: binomial_dist.cpp:47
tower::values
int values[10]
Values in the tower.
Definition: tower_of_hanoi.cpp:13
qr_algorithm::qr_decompose
void qr_decompose(const std::valarray< std::valarray< T >> &A, std::valarray< std::valarray< T >> *Q, std::valarray< std::valarray< T >> *R)
Definition: qr_decompose.h:146
-
main
int main()
Definition: insertion_sort.cpp:91
+
main
int main()
Definition: insertion_sort.cpp:150
genArray
void genArray(int **a, int r, int c)
Definition: spiral_print.cpp:12
main
int main()
Definition: comb_sort.cpp:88
main
int main()
Definition: interpolation_search.cpp:37
diff --git a/db/df0/insertion__sort_8cpp__incl.map b/db/df0/insertion__sort_8cpp__incl.map index 0bbd9bbb2..653234842 100644 --- a/db/df0/insertion__sort_8cpp__incl.map +++ b/db/df0/insertion__sort_8cpp__incl.map @@ -1,6 +1,7 @@ - + + diff --git a/db/df0/insertion__sort_8cpp__incl.md5 b/db/df0/insertion__sort_8cpp__incl.md5 index ab433d07e..c60b1f3b3 100644 --- a/db/df0/insertion__sort_8cpp__incl.md5 +++ b/db/df0/insertion__sort_8cpp__incl.md5 @@ -1 +1 @@ -9e35aee147204c0a635514e39ddc5a84 \ No newline at end of file +61d26c1e856b0067efd687b37909c348 \ No newline at end of file diff --git a/db/df0/insertion__sort_8cpp__incl.svg b/db/df0/insertion__sort_8cpp__incl.svg index 220d5d03e..da1e5c022 100644 --- a/db/df0/insertion__sort_8cpp__incl.svg +++ b/db/df0/insertion__sort_8cpp__incl.svg @@ -4,17 +4,17 @@ - + sorting/insertion_sort.cpp - + Node1 - -sorting/insertion_sort.cpp + +sorting/insertion_sort.cpp @@ -30,8 +30,8 @@ Node1->Node2 - - + + @@ -45,8 +45,8 @@ Node1->Node3 - - + + @@ -60,8 +60,23 @@ Node1->Node4 - - + + + + + +Node5 + + +vector + + + + + +Node1->Node5 + + diff --git a/dd/d0d/insertion__sort_8cpp.html b/dd/d0d/insertion__sort_8cpp.html index dab29d37c..a2f515f34 100644 --- a/dd/d0d/insertion__sort_8cpp.html +++ b/dd/d0d/insertion__sort_8cpp.html @@ -91,6 +91,7 @@ $(document).ready(function(){initNavTree('dd/d0d/insertion__sort_8cpp.html','../
+Namespaces | Functions
insertion_sort.cpp File Reference
@@ -102,18 +103,33 @@ $(document).ready(function(){initNavTree('dd/d0d/insertion__sort_8cpp.html','../
#include <algorithm>
#include <cassert>
#include <iostream>
+#include <vector>
Include dependency graph for insertion_sort.cpp:
-
+
+ + + + +

+Namespaces

 sorting
 Sorting algorithms.
 
- - - + + + + + + + + + + + @@ -121,41 +137,55 @@ Functions

Functions

void insertionSort (int *arr, int n)
 Insertion Sort Function. More...
 
template<typename T >
void sorting::insertionSort (T *arr, int n)
 Insertion Sort Function. More...
 
template<typename T >
void sorting::insertionSort (std::vector< T > *arr)
 
template<typename T >
static void create_random_array (T *arr, int N)
 Create a random array objecthelper function to create a random array. 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()

+

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. +
  3. For small set of data it is quite efficient
  4. +
  5. More efficient that other Quadratic complexity algorithms like Selection sort or bubble sort.
  6. +
  7. It's stable that is it does not change the relative order of elements with equal keys
  8. +
  9. Works on hand means it can sort the array or list as it receives.
  10. +
+

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.

+

Example execution steps:

    +
  1. Suppose initially we have

    +\begin{bmatrix}4 &3 &2 &5 &1\end{bmatrix} +

    +
  2. +
  3. 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

    +\begin{bmatrix}3 &4 &2 &5 &1\end{bmatrix} +

    +
  4. +
  5. 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

    +\begin{bmatrix}2 &3 &4 &5 &1\end{bmatrix} +

    +
  6. +
  7. We do 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

    +\begin{bmatrix}1 &2 &3 &4 &5\end{bmatrix} +

    +
  8. +
+

Function Documentation

+ +

◆ create_random_array()

+
+template<typename T >
+ + + + + +
- + - + - + @@ -163,28 +193,40 @@ becomes
2 3 4 5 1
we does not do anything with 5 and move on
           
void insertionSort static void create_random_array (int * T *  arr,
int n N 
+
+static
-

Insertion Sort Function.

-
Parameters
- - - +

Create a random array objecthelper function to create a random array.

+
Template Parameters
+
arrArray to be sorted
nSize of Array
+
Ttype 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 }
-
+
Parameters
+ + + +
arrarray to fill (must be pre-allocated)
Nnumber of array elements
+
+
+
101  {
+
102  while (N--) {
+
103  double r = (std::rand() % 10000 - 5000) / 100.f;
+
104  arr[N] = static_cast<T>(r);
+
105  }
+
106 }
+
+Here is the call graph for this function:
+
+
+
+
+
@@ -204,36 +246,40 @@ becomes
2 3 4 5 1
we does not do anything with 5 and move on
 

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 }
+
150  {
+
151  /// Running predefined tests to test algorithm
+
152  tests();
+
153 
+
154  /// For user insteraction
+
155  size_t n;
+
156  std::cout << "Enter the length of your array (0 to exit): ";
+
157  std::cin >> n;
+
158  if (n == 0) {
+
159  return 0;
+
160  }
+
161 
+
162  int *arr = new int[n];
+
163  std::cout << "Enter any " << n << " Numbers for Unsorted Array : ";
+
164 
+
165  for (int i = 0; i < n; i++) {
+
166  std::cin >> arr[i];
+
167  }
+
168 
+
169  sorting::insertionSort(arr, n);
+
170 
+
171  std::cout << "\nSorted Array : ";
+
172  for (int i = 0; i < n; i++) {
+
173  std::cout << arr[i] << " ";
+
174  }
+
175 
+
176  std::cout << std::endl;
+
177  delete[] arr;
+
178  return 0;
+
179 }
Here is the call graph for this function:
-
+
@@ -254,21 +300,49 @@ Here is the call graph for this function:

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 }
+
109  {
+
110  int arr1[10] = {78, 34, 35, 6, 34, 56, 3, 56, 2, 4};
+
111  std::cout << "Test 1... ";
+
112  sorting::insertionSort(arr1, 10);
+
113  assert(std::is_sorted(arr1, arr1 + 10));
+
114  std::cout << "passed" << std::endl;
+
115 
+
116  int arr2[5] = {5, -3, 7, -2, 1};
+
117  std::cout << "Test 2... ";
+
118  sorting::insertionSort(arr2, 5);
+
119  assert(std::is_sorted(arr2, arr2 + 5));
+
120  std::cout << "passed" << std::endl;
+
121 
+
122  float arr3[5] = {5.6, -3.1, -3.0, -2.1, 1.8};
+
123  std::cout << "Test 3... ";
+
124  sorting::insertionSort(arr3, 5);
+
125  assert(std::is_sorted(arr3, arr3 + 5));
+
126  std::cout << "passed" << std::endl;
+
127 
+
128  std::vector<float> arr4({5.6, -3.1, -3.0, -2.1, 1.8});
+
129  std::cout << "Test 4... ";
+
130  sorting::insertionSort(&arr4);
+
131  assert(std::is_sorted(std::begin(arr4), std::end(arr4)));
+
132  std::cout << "passed" << std::endl;
+
133 
+
134  int arr5[50];
+
135  std::cout << "Test 5... ";
+
136  create_random_array(arr5, 50);
+
137  sorting::insertionSort(arr5, 50);
+
138  assert(std::is_sorted(arr5, arr5 + 50));
+
139  std::cout << "passed" << std::endl;
+
140 
+
141  float arr6[50];
+
142  std::cout << "Test 6... ";
+
143  create_random_array(arr6, 50);
+
144  sorting::insertionSort(arr6, 50);
+
145  assert(std::is_sorted(arr6, arr6 + 50));
+
146  std::cout << "passed" << std::endl;
+
147 }
Here is the call graph for this function:
-
+
@@ -276,11 +350,16 @@ Here is the call graph for this function:
+
std::vector
STL class.
+
sorting::insertionSort
void insertionSort(T *arr, int n)
Insertion Sort Function.
Definition: insertion_sort.cpp:59
std::is_sorted
T is_sorted(T... args)
std::cout
+
create_random_array
static void create_random_array(T *arr, int N)
Create a random array objecthelper function to create a random array.
Definition: insertion_sort.cpp:101
+
std::rand
T rand(T... args)
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::begin
T begin(T... args)
+
tests
void tests()
Definition: insertion_sort.cpp:109
+
std::end
T end(T... args)
std::cin
-
Here is a list of all documented namespace members with links to the namespaces they belong to: