Using divide and conquer algorithm approach on a binary tree, show how the following list of elements in an array can be sorted in ascending order:
38, 27,43,3,9,82,10
Answer:
Merge sort technique is based on Divide and Conquer algorithm. It divides the elements in the input array into two halves and then merges the two sorted halves. The mergesort () function is used for merging two halves in the below program. The mergesort (A, left, middle, right) is key process that assumes that A[left……middle] and A[middle+left……right] are sorted and merges the two sorted sub-arrays into one.
Algorithm:
MergeSort (A[], left, right)
If right > left
middle = (left+right)/2
mergeSort (A, left, middle)
mergeSort (A, middle+1, right)
mergeSort (A, left, middle, right)
List of elements in an array can be sorted in ascending order(C++ IMPLEMENTATION):
#include <iostream>
using namespace std;
void mergesort(int A[], int left, int middle, int right)
{
int i, j, k;
int n1 = middle - left + 1;
int n2 = right - middle;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = A[left + i];
for (j = 0; j < n2; j++)
R[j] = A[middle + 1+ j];
i = 0; j = 0; k = left;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
A[k] =
L[i];
i++;
}
else
{
A[k] =
R[j];
j++;
}
k++;
}
while (i < n1)
{
A[k] = L[i];
i++;
k++;
}
while (j < n2)
{
A[k] = R[j];
j++;
k++;
}
}
void mergeSort(int A[], int left, int right)
{
if (left < right)
{
int middle =
left+(right-left)/2;
mergeSort(A, left, middle);
mergeSort(A, middle+1,
right);
mergesort(A, left, middle, right);
}
}
void Display(int A[], int size)
{
int i;
for (i=0; i < size; i++)
cout<<A[i]<<" ";
cout << endl;
}
int main()
{
int A[] = {38, 27,43,3,9,82,10};
int size = sizeof(A)/sizeof(A[0]);
cout<<"Enter the number of elements in the array
:\n";
Display(A, size);
mergeSort(A, 0, size - 1);
cout<<"Sort the elements in the array:\n";
Display(A, size);
return 0;
}
OUTPUT:
Enter the number of elements in the array :
38 27 43 3 9 82 10
Sort the elements in the array:
3 9 10 27 38 43 82
Binary Search Approach:
Given array is divided into two halves (one is left child and
other is right child)
38,27,43,3,9,82,10---------------------------Given
list
/ \
38,27,43,3 9,82,10
/ \ / \
38,27 43,3 9,82 10
/ \ / \ / \ \
38 27 43 3 9 82 10
\ / \ / \ / \
27,38 3,43 9,82 10
\ / \ /
3,27,38,43 9,10,82
\ /
3,9,10,27,38,43,82 --------sorted list
Using divide and conquer algorithm approach on a binary tree, show how the following list of...
Design a divide-and-conquer algorithm for computing the number of levels in a binary tree. In particular, the algorithm should return 0 and 1 for the empty and single-node trees respectively. Please provide the pseudocode for your algorithm. What is the running time of your algorithm in the worst case using O() notation? Design a divide-and-conquer algorithm for computing the number of levels in a COMPLETE binary tree. In particular, the algorithm should return 0 and 1 for the empty and...
Design a divide-and-conquer algorithm in pseudocode for computing the number of levels in a binary tree. In particular, your algorithm must return 0 and 1 for the empty and single-node trees, respectively. What is the time efficiency class of your algorithm?
1. Design and write a Divide& Conquer algorithm that, given an array A of n distinct integers which is already sorted into ascending order, will find if there is some i such that Ali] in worst-case 0(log n) time.
Q1) Using Divide and conquer approach, solve the kth element in 2 sorted arrays problem. Given two sorted arrays both of size n find the element in k’th position of the combined sorted array. 1. Mention the steps of Divide, Conquer and Combine (refer to L5- Divide and Conquer Lecture notes, slide 3, to see an example on merge sort) 2. Draw the recursive tree. 3. What is the recurrence equation? 4. Guess a solution based on the recursive tree...
Which of the following is the main requirement for a binary search algorithm? a. The list must have an odd number of elements. b. The list must be sorted in ascending order. C. The list must be sorted in descending order. d. The list must have an even number of elements.
In the text box below, write down a divide and conquer algorithm for counting the number of entries in a sorted array of ints that are smaller than a given value. In other words, the function takes as input an array A and an int value and returns the number of ints in A that are less than value. To get any credit, your solution must use the divide and conquer technique. To get full credit, your solution should run in time in the...
6.3.1 [10] <§6.2> Consider the following binary search algorithm (a classic divide and conquer algorithm) that searches for a value X in a sorted N-element array A and returns the index of matched entry: BinarySearch(A[0..N−1], X) { low = 0 high = N −1 while (low <= high) { mid = (low + high) / 2 if (A[mid] >X) high = mid −1 else if (A[mid] <X) low = mid + 1 else return mid // found } return −1...
1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...
Divide and Conquer & Algorithm Design 5. (20 points) Consider the following algorithm Precondition: S is a sorted list index mystery (index low, index high, const Array S[], number x) if low S high then mid = (low + high) / 2 if x = Smid] then return mid elsif x < s[mid] then return mystery (low, mid-1, s, x) else return mystery (mid+1, high, s, x) else return 0 end What does the recursive algorithm above compute? Explain why?
2. Using Python, implement the Divide-and-Conquer algorithm to count the number of inversions between two arrays. The algorithm is based on Mergesort and counts the inversions while merging the sorted lists.