-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertion.cpp
75 lines (64 loc) · 1.75 KB
/
Insertion.cpp
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
66
67
68
69
70
71
72
73
74
75
#include <iostream>
using namespace std;
void printArray();
void swap(int a, int b);
void insertionSort();
const int arrSize = 10;
int arr[arrSize];
int main()
{
// Filling the array with random numbers...
for (int i = 0; i < arrSize; i++)
arr[i] = rand() % 100;
cout << "Array Before Sorting:\n";
printArray();
cout << "\nSorting...\n";
insertionSort();
cout << "\nArray After Sorting:\n";
printArray();
return 0;
}
void printArray()
{
for (int i = 0; i < arrSize; i++)
cout << arr[i] << "\t";
cout << "\n";
}
void swap(int a, int b)
{
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
void insertionSort()
{
/**
* This outer for loop starts from the second element (index 1) and iterates through the entire array.
* This loop represents the card you are currently trying to place in the sorted portion of the array.
*/
for (int i = 1; i < arrSize; i++)
{
/**
* This if statement that checks if the current element is smaller than the previous one.
* If it is, it means that this element is not in its correct position
* and needs to be moved back to the correct place in the sorted part of the array.
*/
if (arr[i] < arr[i - 1])
{
/**
* To find the correct position, this for loop starts from the current element
* and goes backwards to the start of the array.
*/
for (int j = i; j > 0; j--)
{
/**
* This if statement that checks if the current element is smaller than the previous one.
* If it is, the swap function is called to swap these two elements.
* (This process continues until the element is in its correct position.)
*/
if (arr[j] < arr[j - 1])
swap(j, j - 1);
}
}
}
}