ASSIGNMENT 3
1. Binary Search Iterative Method
CODE
#include <iostream>
using namespace std;
int BinSearch(int a[], int n, int x) {
int low = 0;
int high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (x < a[mid]) {
high = mid - 1;
} else if (x > a[mid]) {
low = mid + 1;
} else {
return mid;
}
}
return -1;
}
int main() {
int n, x;
cout << "Enter the number of elements in the array: ";
cin >> n;
int a[n];
cout << "Enter " << n << " elements in non-decreasing order: ";
for (int i = 0; i < n; i++) {
cin >> a[i];
}
cout << "Enter the element to search: ";
cin >> x;
int result = BinSearch(a, n, x);
if (result != -1) {
cout << "Element found at index: " << result << endl;
} else {
cout << "Element not found." << endl;
}
return 0;
}
OUTPUT
Algorithm
2. MaxMin Straight Forward
CODE
#include <iostream>
using namespace std;
void StraightMaxMin(int a[], int n, int &max, int &min) {
max = min = a[0];
for (int i = 1; i < n; i++) {
if (a[i] > max) {
max = a[i];
}
if (a[i] < min) {
min = a[i];
}
}
}
int main() {
int n;
cout << "Enter the number of elements in the array: ";
cin >> n;
int *a = new int[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int max, min;
StraightMaxMin(a, n, max, min);
cout << "Maximum element: " << max << endl;
cout << "Minimum element: " << min << endl;
delete[] a;
return 0;
}
OUTPUT
Algorithm
3. MaxMin Recursion
CODE
#include <iostream>
#include <algorithm>
using namespace std;
void MaxMin(int a[], int i, int j, int &max, int &min) {
if (i == j) {
max = min = a[i];
} else if (i == j - 1) {
if (a[i] < a[j]) {
max = a[j];
min = a[i];
} else {
max = a[i];
min = a[j];
}
} else {
int mid = (i + j) / 2;
int max1, min1;
MaxMin(a, i, mid, max, min);
MaxMin(a, mid + 1, j, max1, min1);
if (max < max1) max = max1;
if (min > min1) min = min1;
}
}
int main() {
int n;
cout << "Enter the number of elements in the array: ";
cin >> n;
int a[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int max, min;
MaxMin(a, 0, n - 1, max, min);
cout << "Maximum element: " << max << endl;
cout << "Minimum element: " << min << endl;
return 0;
}
OUTPUT
Algorithm
4. Merge Sort
CODE
#include <iostream>
using namespace std;
void Merge(int a[], int low, int mid, int high) {
int n1 = mid - low + 1;
int n2 = high - mid;
int left[n1], right[n2];
for (int i = 0; i < n1; i++)
left[i] = a[low + i];
for (int i = 0; i < n2; i++)
right[i] = a[mid + 1 + i];
int i = 0, j = 0, k = low;
while (i < n1 && j < n2) {
if (left[i] <= right[j]) {
a[k] = left[i];
i++;
} else {
a[k] = right[j];
j++;
}
k++;
}
while (i < n1) {
a[k] = left[i];
i++;
k++;
}
while (j < n2) {
a[k] = right[j];
j++;
k++;
}
}
void MergeSort(int a[], int low, int high) {
if (low < high) {
int mid = low + (high - low) / 2;
MergeSort(a, low, mid);
MergeSort(a, mid + 1, high);
Merge(a, low, mid, high);
}
}
int main() {
int n;
cout << "Enter the number of elements in the array: ";
cin >> n;
int a[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++) {
cin >> a[i];
}
MergeSort(a, 0, n - 1);
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << endl;
return 0;
}
OUTPUT
Algorithm
5. Quick Sort
CODE
#include <iostream>
using namespace std;
int Partition(int a[], int p, int q) {
int pivot = a[p];
int i = p;
int j = q;
while (true) {
while (a[i] < pivot) {
i++;
}
while (a[j] > pivot) {
j--;
}
if (i >= j) {
return j;
}
swap(a[i], a[j]);
i++;
j--;
}
}
void QuickSort(int a[], int p, int q) {
if (p < q) {
int j = Partition(a, p, q);
QuickSort(a, p, j);
QuickSort(a, j + 1, q);
}
}
int main() {
int n;
cout << "Enter the number of elements in the array: ";
cin >> n;
int a[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++) {
cin >> a[i];
}
QuickSort(a, 0, n - 1);
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << endl;
return 0;
}
OUTPUT
Algorithm
TIME COMPLEXITY
Binary Search : O(log n)
Straight MaxMin : O (n) Iteration (2n-2)
Recursive MaxMin: O(n) Iteration (3n/2 -2)
Quick Sort: O(n logn)
Merge Sort: O(n logn)