Question

Using divide and conquer algorithm approach on a binary tree, show how the following list of...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

  • Divide the given array into two halves and find the middle point

middle = (left+right)/2

  •    Call mergeSort for first half as array,left and middle  

mergeSort (A, left, middle)

  •    Call mergeSort for second half as array,middle, left and right

mergeSort (A, middle+1, right)

  • Now Merge the two halves sorted in step 2 and 3:

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   

Add a comment
Know the answer?
Add Answer to:
Using divide and conquer algorithm approach on a binary tree, show how the following list of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT