-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertionSort.cpp
79 lines (66 loc) · 1.7 KB
/
insertionSort.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
76
77
78
79
#include <iostream>
#include <stdlib.h>
#include <ctime>
using namespace std;
void printArray(int arr[], int sizeOfArray);
void insertionSort(int arr[], int sizeOfArray);
bool isSorted(int arr[], int sizeOfArray);
int main()
{
int arr[5] = {5,10,4,2,1}; // size of testing array = 5
int sizeOfArray = 5;
printArray(arr,sizeOfArray);
insertionSort(arr, sizeOfArray);
printArray(arr, sizeOfArray);
return 0;
}
void insertionSort(int arr[], int sizeOfArray)
{
/* insertion sort works as follows: its going to run from the beginning
* of the array by checking the right order of each element and
* inserting them in the right location*/
int iterationNumber = 1;
// list will be use to traverse the entire array starting from
// index 2
for(int list = 1; list < sizeOfArray; list++)
{
// loop that will iterate backwards checking if the order of the
// array matches, it will be swapping the values until the array
// its in order then it will exit
int check = list;
while(arr[check] < arr[check-1])
{
printArray(arr, sizeOfArray);
cout<<"Iteration number: "<<iterationNumber<<endl;
// swap the values
int temp = arr[check];
arr[check] = arr[check - 1];
arr[check - 1] = temp;
check--;
}
iterationNumber++;
}
}
bool isSorted(int arr[], int sizeOfArray)
{
/* for loop starting at index 1 to avoid going out of bounce*/
for(int n = 1; n < sizeOfArray; n++)
{
if(arr[n] < arr[n-1]) return false;
}
return true;
}
void printArray(int arr[], int sizeOfArray)
{
cout<<"Presented Array: ";
for(int n = 0; n < 5; n++)
{
cout<<arr[n]<<" ";
}
cout<<"\n"<<"is ";
if(isSorted(arr,sizeOfArray) == 0)
{
cout<<"Not Sorted "<<endl;
}
else {cout<<"Sorted "<<endl;}
}