-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab3.cpp
182 lines (162 loc) · 3.5 KB
/
Lab3.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*Question 1). Input: nums = [3,2,1,5,6,4], k = 2
Output: 5
*/
/*Q1* test case Input: nums = [3,2,3,1,2,4,5,5,6], k = 2
Output: 5
*/
// #include<bits/stdc++.h>
/*Input: nums = [3,2,1,5,6,4], k = 2
Output: 5
*/
/*Q1* test case Input: nums = [3,2,3,1,2,4,5,5,6], k = 2
Output: 5
*/
// #include<bits/stdc++.h>
#include<iostream>
using namespace std;
void insertionSort(int arr[], int n)
{
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;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
int arr[] = {3,2,3,1,2,4,5,5,6};
int N = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, N);
printArray(arr, N);
if (N >= 2) {
cout << "Second highest number : " << arr[N - 2] << endl;
} else {
cout << "NO second highest no...because of the length in 2 :" << endl;
}
return 0;
}
/*Question2 ). Example:
Input: nums = [2,0,2,1,1,0]
*/
#include <bits/stdc++.h>
using namespace std;
void bubbleSort(int arr[], int n) {
bool isUnsorted;
do {
isUnsorted = false;
for (int i = 0; i < (n - 1); i++) {
if (arr[i] > arr[i + 1]) {
isUnsorted = true;
for (; i < (n - 1); i++) {
if (arr[i] > arr[i + 1]) {
std::swap(arr[i], arr[i + 1]);
}
}
}
}
} while (isUnsorted);
}
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout <<" "<< arr[i];
}
int main()
{
int arr[] ={2,0,2,1,1,0} ;
int N = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, N);
printArray(arr, N);
return 0;
}
/*Question 3 ).Example:
Input : A[ ] = { 2, 3, 8, -1, 7, 10 }
Output : -1, 2, 3, 7, 8, 10
Test case
Input : A[ ] = {-4, 6, 9, -1, 3 }
Output : -4, -1, 3, 6, 9
*/
#include <bits/stdc++.h>
using namespace std;
void merge(int array[], int const left, int const mid,
int const right)
{
int const subArrayOne = mid - left + 1;
int const subArrayTwo = right - mid;
auto *leftArray = new int[subArrayOne],
*rightArray = new int[subArrayTwo];
for (auto i = 0; i < subArrayOne; i++)
leftArray[i] = array[left + i];
for (auto j = 0; j < subArrayTwo; j++)
rightArray[j] = array[mid + 1 + j];
auto Iof1stsubarr = 0, Iof2ndsubarr = 0;
int Iofmergedarr = left;
while (Iof1stsubarr < subArrayOne
&& Iof2ndsubarr < subArrayTwo) {
if (leftArray[Iof1stsubarr]
<= rightArray[Iof2ndsubarr]) {
array[Iofmergedarr]
= leftArray[Iof1stsubarr];
Iof1stsubarr++;
}
else {
array[Iofmergedarr]
= rightArray[Iof2ndsubarr];
Iof2ndsubarr++;
}
Iofmergedarr++;
}
while (Iof1stsubarr < subArrayOne) {
array[Iofmergedarr]
= leftArray[Iof1stsubarr];
Iof1stsubarr++;
Iofmergedarr++;
}
while (Iof2ndsubarr < subArrayTwo) {
array[Iofmergedarr]
= rightArray[Iof2ndsubarr];
Iof2ndsubarr++;
Iofmergedarr++;
}
delete[] leftArray;
delete[] rightArray;
}
void mergeSort(int array[], int const begin, int const end)
{
if (begin >= end)
return;
int mid = begin + (end - begin) / 2;
mergeSort(array, begin, mid);
mergeSort(array, mid + 1, end);
merge(array, begin, mid, end);
}
void printArray(int A[], int size)
{
for (int i = 0; i < size; i++)
cout << A[i] << " ";
cout << endl;
}
int main()
{
int arr[] = {-4, 6, 9, -1, 3 };
int SizeOfarr = sizeof(arr) / sizeof(arr[0]);
cout << "Given array is \n";
printArray(arr, SizeOfarr);
mergeSort(arr, 0, SizeOfarr - 1);
cout << "\nSorted array is \n";
printArray(arr, SizeOfarr);
return 0;
}