0% found this document useful (0 votes)
7 views

sort and binary search assignment 1

The document contains multiple sorting algorithms implemented in C++, including Binary Search, Bubble Sort, Heap Sort, Insertion Sort, Merge Sort, Quick Sort, and Selection Sort. Each algorithm is accompanied by a main function that demonstrates sorting an array of integers in descending order. Additionally, there are utility functions for printing the arrays before and after sorting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

sort and binary search assignment 1

The document contains multiple sorting algorithms implemented in C++, including Binary Search, Bubble Sort, Heap Sort, Insertion Sort, Merge Sort, Quick Sort, and Selection Sort. Each algorithm is accompanied by a main function that demonstrates sorting an array of integers in descending order. Additionally, there are utility functions for printing the arrays before and after sorting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Binary Search:

#include <bits/stdc++.h>

using namespace std;

int binarySearchDescending(int arr[], int size, int target) {

int left = 0, right = size - 1;

while (left <= right) {

int mid = left + (right - left) / 2;

if (arr[mid] == target) {

return mid;

if (arr[mid] < target) {

right = mid - 1;

else {

left = mid + 1;

return -1;

void printArray(int arr[], int size) {

for (int i = 0; i < size; i++) {


cout << arr[i] << " ";

cout << endl;

int main() {

int arr[] = {94, 90, 51, 43, 32, 28, 22};

int size = sizeof(arr) / sizeof(arr[0]);

int target = 51;

cout << "Array: ";

printArray(arr, size);

int result = binarySearchDescending(arr, size, target);

if (result != -1) {

cout << "Element " << target << " found at index " << result << endl;

} else {

cout << "Element " << target << " not found in the array." << endl;

return 0;

}
Bubble sort

#include <bits/stdc++.h>

using namespace std;

void bubbleSort(int arr[], int n) {

bool swapped;

for (int i = 0; i < n - 1; i++) {

swapped = false;

for (int j = 0; j < n - i - 1; j++) {

if (arr[j] < arr[j + 1]) {

swap(arr[j], arr[j + 1]);

swapped = true;

if (!swapped)

break;

void printArray(int arr[], int n) {

for (int i = 0; i < n; i++)

cout << arr[i] << " ";

cout << endl;

int main() {
int arr[] = {94, 43, 32, 22, 28, 51, 90};

int n = sizeof(arr) / sizeof(arr[0]);

cout << "Original array: ";

printArray(arr, n);

bubbleSort(arr, n);

cout << "Sorted array (Descending Order): ";

printArray(arr, n);

return 0;

}
Heap sort

#include <bits/stdc++.h>

using namespace std;

void heapify(int arr[], int n, int i) {

int smallest = i;

int l = 2 * i + 1;

int r = 2 * i + 2;

if (l < n && arr[l] < arr[smallest])

smallest = l;

if (r < n && arr[r] < arr[smallest])

smallest = r;

if (smallest != i) {

swap(arr[i], arr[smallest]);

heapify(arr, n, smallest);

void heapSort(int arr[], int n) {

for (int i = n / 2 - 1; i >= 0; i--)

heapify(arr, n, i);

for (int i = n - 1; i > 0; i--) {


swap(arr[0], arr[i]);

heapify(arr, i, 0);

void printArray(int arr[], int n) {

for (int i = 0; i < n; ++i)

cout << arr[i] << " ";

cout << endl;

int main() {

int arr[] = {94, 43, 32, 22, 28, 51, 90};

int n = sizeof(arr) / sizeof(arr[0]);

cout << "Original array: ";

printArray(arr, n);

heapSort(arr, n);

cout << "Sorted array (Descending Order): ";

printArray(arr, n);

return 0;

}
Insertion sort

#include <bits/stdc++.h>

using namespace std;

void insertionSort(int arr[], int n) {

for (int i = 1; i < n; ++i) {

int key = arr[i];

int 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) {

for (int i = 0; i < n; ++i)

cout << arr[i] << " ";

cout << endl;

int main() {

int arr[] = {94, 43, 32, 22, 28, 51, 90};

int n = sizeof(arr) / sizeof(arr[0]);


cout << "Original array: ";

printArray(arr, n);

insertionSort(arr, n);

cout << "Sorted array (Descending Order): ";

printArray(arr, n);

return 0;

}
Merge Sort
#include <bits/stdc++.h>

using namespace std;

void merge(int arr[], int left, int mid, int right) {

int n1 = mid - left + 1, n2 = right - mid;

int L[n1], R[n2];

for (int i = 0; i < n1; i++) L[i] = arr[left + i];

for (int j = 0; j < n2; j++) R[j] = arr[mid + 1 + j];

int i = 0, j = 0, k = left;

while (i < n1 && j < n2) arr[k++] = (L[i] > R[j]) ? L[i++] : R[j++];

while (i < n1) arr[k++] = L[i++];

while (j < n2) arr[k++] = R[j++];

void mergeSort(int arr[], int left, int right) {

if (left >= right) return;

int mid = left + (right - left) / 2;

mergeSort(arr, left, mid);

mergeSort(arr, mid + 1, right);

merge(arr, left, mid, right);

void printArray(int arr[], int n) {

for (int i = 0; i < n; i++) cout << arr[i] << " ";

cout << endl;


}

int main() {

int arr[] = {94, 43, 32, 22, 28, 51, 90}, n = sizeof(arr) / sizeof(arr[0]);

cout << "Given array: "; printArray(arr, n);

mergeSort(arr, 0, n - 1);

cout << "Sorted array in descending order: "; printArray(arr, n);

return 0;

}
Quick Sort
#include <bits/stdc++.h>

using namespace std;

int partition(int arr[], int low, int high) {

int pivot = arr[high];

int i = low - 1;

for (int j = low; j <= high - 1; j++) {

if (arr[j] > pivot) {

i++;

swap(arr[i], arr[j]);

swap(arr[i + 1], arr[high]);

return i + 1;

void quickSort(int arr[], int low, int high) {

if (low < high) {

int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);

quickSort(arr, pi + 1, high);

}
void printArray(int arr[], int n) {

for (int i = 0; i < n; i++) {

cout << arr[i] << " ";

cout << endl;

int main() {

int arr[] = {94, 43, 32, 22, 28, 51, 90};

int n = sizeof(arr) / sizeof(arr[0]);

cout << "Original array: ";

printArray(arr, n);

quickSort(arr, 0, n - 1);

cout << "Sorted array (Descending Order): ";

printArray(arr, n);

return 0;

}
Selection Sort

#include <bits/stdc++.h>

using namespace std;

void selectionSort(int arr[], int n) {

for (int i = 0; i < n - 1; ++i) {

int max_idx = i;

for (int j = i + 1; j < n; ++j) {

if (arr[j] > arr[max_idx]) {

max_idx = j;

swap(arr[i], arr[max_idx]);

void printArray(int arr[], int n) {

for (int i = 0; i < n; i++) {

cout << arr[i] << " ";

cout << endl;

int main() {

int arr[] = {94, 43, 32, 22, 28, 51, 90};

int n = sizeof(arr) / sizeof(arr[0]);


cout << "Original array: ";

printArray(arr, n);

selectionSort(arr, n);

cout << "Sorted array (Descending Order): ";

printArray(arr, n);

return 0;

You might also like