TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
insertion_sort.cpp
Go to the documentation of this file.
1
41
42
#include <algorithm>
43
#include <cassert>
44
#include <iostream>
45
#include <vector>
46
50
namespace
sorting
{
58
template
<
typename
T>
59
void
insertionSort
(T *arr,
int
n) {
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
}
70
76
template
<
typename
T>
77
void
insertionSort
(std::vector<T> *arr) {
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
}
90
91
}
// namespace sorting
92
100
template
<
typename
T>
101
static
void
create_random_array
(T *arr,
int
N) {
102
while
(N--) {
103
double
r = (std::rand() % 10000 - 5000) / 100.f;
104
arr[N] =
static_cast<
T
>
(r);
105
}
106
}
107
109
void
tests
() {
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
}
148
150
int
main
() {
152
tests
();
153
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
}
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
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
sorting
for working with vectors
sorting::insertionSort
void insertionSort(T *arr, int n)
Insertion Sort Function.
Definition
insertion_sort.cpp:59
tests
Testcases to check Union of Two Arrays.
sorting
insertion_sort.cpp
Generated by
1.18.0