Daa Experiment1,2,3
Daa Experiment1,2,3
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
2. Space Complexity
PROGRAM CODE:
#include <stdio.h>
int i, j, temp;
temp = a[j];
a[j + 1] = temp;
int main() {
scanf("%d", &n);
scanf("%d", &a[i]);
B_sort(a, n);
printf("\n");
scanf("%d", &key);
low = 0;
high = n - 1;
low = mid + 1;
break;
} else
high = mid - 1;
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 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.
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
2. Space Complexity
int i, j, k;
i = 0;
j = 0;
k = left;
arr[k] = leftArray[i];
i++;
} else {
arr[k] = rightArray[j];
j++;
k++;
arr[k] = leftArray[i];
i++;
k++;
arr[k] = rightArray[j];
j++;
k++;
printf("\n");
int main() {
int n;
int arr[n];
scanf("%d", &arr[i]);
printArray(arr, n);
mergeSort(arr, 0, n - 1);
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.
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
2. Space Complexity
#include <stdio.h>
*a = *b;
*b = temp;
swap(&arr[i], &arr[j]);
return (i + 1);
quickSort(arr, pi + 1, high);
}
}
printf("\n");
int main() {
int n;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
printArray(arr, n);
quickSort(arr, 0, n - 1);
printArray(arr, n);
return 0;