forked from abhijithshaji17/cpp-mastery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path26.cpp
More file actions
65 lines (54 loc) · 1.38 KB
/
Copy path26.cpp
File metadata and controls
65 lines (54 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Sorting - #1. Insertion Sort
// insertsort algoway
#include <iostream>
using namespace std;
void insertsort(int a[], int n) {
int i, j, key;
// Start from the second element (index 1) since a single element is already "sorted"
for (i = 1; i < n; i++) {
key = a[i]; // The element we want to insert into the sorted subarray
j = i - 1; // Start comparing with the element right before 'i'
// Shift elements of a[0..i-1] that are greater than key to one position ahead
while (j >= 0 && a[j] > key) {
a[j + 1] = a[j];
j = j - 1;
}
// Place key in its correct sorted location
a[j + 1] = key;
}
cout << "\n\nAfter insertion sort:";
}
void printarr(int a[], int n) {
cout << "\nArray:\n[";
for (int i = 0; i < n; i++) {
cout << " " << a[i];
}
cout << " ]\n";
}
int main() {
int a[20], n, i;
cout << "Enter the no. of array elements: ";
cin >> n;
cout << "Enter elements:\n";
for (i = 0; i < n; i++) {
cout << "Element " << i + 1 << ": ";
cin >> a[i];
}
printarr(a, n);
insertsort(a, n);
printarr(a, n);
return 0;
}
/*
Algorithm:
1. for i = lb + 1 to ub or 1 to n
2. key = a[i]
3. j = i - 1
4. while j >= 0 or lb and a[j] > key
5. a[j+1] = a[j]
6. j = j - 1
7. end while
8. a[j+1] = key
9. end for
10. end
*/