Experiment 1
1. Develop a program and measure the running time for Binary Search with Divide and
Conquer.
Aim: To develop a program and measure the running time for Binary Search with Divide and
Conquer.
Binary Search:
Searching is the process of finding some particular element in the list. If the element is present
in the list, then the process is called successful, and the process returns the location of that
element. Otherwise, the search is called unsuccessful.
Linear Search and Binary Search are the two popular searching techniques. Binary search is
the search technique that works efficiently on sorted lists. Hence, to search an element into
some list using the binary search technique, we must ensure that the list is sorted.
Binary search follows the divide and conquer approach in which the list is divided into two
halves, and the item is compared with the middle element of the list. If the match is found then,
the location of the middle element is returned. Otherwise, we search into either of the halves
depending upon the result produced through the match.
1. Time Complexity
Case Time Complexity
Best Case O(1)
Average Case O(logn)
Worst Case O(logn)
2. Space Complexity
Space Complexity O(1)
o The space complexity of binary search is O(1).
PROGRAM CODE:
#include <stdio.h>
// Function to perform bubble sort
void B_sort(int a[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
int main() {
int i, low, high, mid, n, key, a[100];
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter the elements of the array: ");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
B_sort(a, n);
printf("The sorted array: ");
for (i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
printf("Enter value to find: ");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low + high) / 2;
while (low <= high) {
if (a[mid] < key)
low = mid + 1;
else if (a[mid] == key) {
printf("%d found at location %d\n", key, mid + 1);
break;
} else
high = mid - 1;
mid = (low + high) / 2;
if (low > high)
printf("Not found! %d isn't present in the list.\n", key);
return 0;
OUTPUT:
Experiment 2
Develop a program and measure the running time for Merge Sort with Divide and Conquer
Aim: To Develop a program and measure the running time for Merge Sort with Divide
and Conquer
Description:
Merge Sort with Divide and Conquer
Merge Sort is a popular sorting algorithm that uses the divide and conquer strategy to
sort an array. It works by recursively dividing the array into smaller subarrays, sorting
those subarrays, and then merging them back together to form the sorted array.
Key Steps in Merge Sort:
1. Divide:
o Split the array into two halves, recursively continue to divide these halves
until each subarray has only one element.
2. Conquer:
o Sort the subarrays. Since each subarray now has only one element, they
are already sorted by default.
3. Combine:
o Merge the sorted subarrays back together to form the final sorted array.
1. Time Complexity
Case Time Complexity
Best Case O(n log n)
Average Case O(n log n)
Worst Case O(n log n)
2. Space Complexity
Space Complexity O(n)
#include <stdio.h>
// Function to merge two subarrays
void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int i, j, k;
int leftArray[n1], rightArray[n2];
// Copy data to temporary arrays
for (i = 0; i < n1; i++)
leftArray[i] = arr[left + i];
for (j = 0; j < n2; j++)
rightArray[j] = arr[mid + 1 + j];
i = 0;
j = 0;
k = left;
while (i < n1 && j < n2) {
if (leftArray[i] <= rightArray[j]) {
arr[k] = leftArray[i];
i++;
} else {
arr[k] = rightArray[j];
j++;
k++;
while (i < n1) {
arr[k] = leftArray[i];
i++;
k++;
while (j < n2) {
arr[k] = rightArray[j];
j++;
k++;
// Function to perform merge sort
void mergeSort(int arr[], int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
// Function to print the array
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
// Main function to test merge sort with user input
int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
printf("Given array is:\n");
printArray(arr, n);
mergeSort(arr, 0, n - 1);
printf("\nSorted array is:\n");
printArray(arr, n);
return 0;
}
Experinment- 3: Develop a program and measure the running time for Quick Sort with Divide
and Conquer
Aim : Develop a program and measure the running time for Quick Sort with Divide and Conquer
Quick Sort is a highly efficient and widely used sorting algorithm that follows the divide and conquer
strategy. It works by selecting a "pivot" element from the array and partitioning the other elements
into two subarrays, according to whether they are less than or greater than the pivot. The subarrays are
then recursively sorted.
Key Steps in Quick Sort:
1. Choose a Pivot:
o Select a pivot element from the array. The choice of pivot can vary (first element, last
element, middle element, random element, etc.).
2. Partitioning:
o Rearrange the array so that all elements less than the pivot are on the left, and all
elements greater than the pivot are on the right. The pivot element is placed in its
correct position.
3. Recursively Sort:
o Apply the same process to the subarrays on the left and right of the pivot.
. Time Complexity
Case Time Complexity
Best Case O(n log n)
Average Case O(n log n)
Worst Case O(n2)
2. Space Complexity
Space Complexity O(log n)
#include <stdio.h>
// Function to swap two elements
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
// Function to partition the array
int partition(int arr[], int low, int high) {
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high - 1; j++) {
// If current element is smaller than or equal to pivot
if (arr[j] <= pivot) {
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
swap(&arr[i + 1], &arr[high]);
return (i + 1);
// Function to implement Quick Sort
void quickSort(int arr[], int low, int high) {
if (low < high) {
// pi is partitioning index, arr[pi] is now at right place
int pi = partition(arr, low, high);
// Separately sort elements before and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
// Function to print the array
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
// Main function to test Quick Sort with user input
int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
printf("Given array is:\n");
printArray(arr, n);
quickSort(arr, 0, n - 1);
printf("\nSorted array is:\n");
printArray(arr, n);
return 0;