forked from DrWeird12/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertionSort.cpp
92 lines (84 loc) · 1.91 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
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <bits/stdc++.h>
using namespace std;
void insertionSort(int arr[], int n)
{
int count = 0;
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
count++;
}
arr[j + 1] = key;
}
cout << "the freaquency is" << count << endl;
}
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
void random_input(int arr[], int a)
{
for (int i = 0; i < a; i++)
{
arr[i] = rand();
}
insertionSort(arr, a);
printArray(arr, a);
// N = sizeof(arr) / sizeof(arr[0]);
}
void ascending_input(int arr[], int a)
{
cout << "**********************************************************************" << endl
<< "ascending inputs" << endl;
insertionSort(arr, a);
printArray(arr, a);
}
void descending_input(int arr[], int a)
{
cout << "**********************************************************************" << endl
<< "descending inputs" << endl;
int arr1[a];
for (int i = a, j = 0; i >= 0, j < a; i--, j++)
{
arr1[j] = arr[i];
}
insertionSort(arr1, a);
printArray(arr1, a);
}
int main()
{
int loop_is_on = 1;
int a;
cout << "enter the number of elements in array";
cin >> a;
int arr[a];
while (loop_is_on == 1)
{
cout << "enter 1 for sorting analysis" << endl;
cout << "enter 2 to end the loop " << endl;
int prompt;
cin >> prompt;
switch (prompt)
{
case 1:
random_input(arr, a);
ascending_input(arr, a);
descending_input(arr, a);
break;
case 2:
loop_is_on = 0;
}
// insertionSort(arr, N);
// printArray(arr, N);
}
return 0;
}