Please Provide Code in Either JAVA or C and provide CLEAR Ex

Please Provide Code in Either JAVA or C++ and provide CLEAR! Explanatin on what you are doing in the code. For goodfeedbackand full points. 9

Problem The problem of sorting a list of numbers lends itself immediately to a divide-and-conquer s split the list into two halves, recursively sort each half, and then merge the two sorted sublists recall the problem Merge Two Sorted Arrays Source: Algorithms by Dasgupta, Papadimitriou, Vazirani. McGraw-Hill. 2006. Given: A positive integer n s 105 and an array A1..nl of integers from -10s to 10 Return: A sorted array A1..n Sample Dataset 10 20 19 35 18 17 20 20 1 4 4 Sample output 200 -18 1 4 4 17 19 20 20 35

Solution

#include <iostream>
using namespace std;
void merge(int a[],int l,int r)
{
   int c[r-l+1];
   int mid = l + (r-l)/2;
   int p=l,q=mid+1,r1=0;
   int n = mid,m=r;
   //cout<<l<<\' \'<<mid<<\' \'<<r<<endl;
   /*for(int i=l;i<=r;i++)
   {
       //a[i]=c[i];
       cout<<a[i]<<\' \';
   }
   cout<<endl;*/
   while(p<=n&&q<=m)
   {
       if(a[p]<a[q])
       {
           c[r1] = a[p];
           p++;
       }
       else
       {
           c[r1] = a[q];
           q++;
       }
       r1++;
   }
   while(p<=n)
   {
       c[r1] = a[p];
           p++;
           r1++;
   }
   while(q<=m)
   {
       c[r1] = a[q];
           q++;
           r1++;
   }
   for(int i=l,j=0;i<=r;i++,j++)
   {
       a[i]=c[j];
       //cout<<c[j]<<\' \';
   }
   //cout<<endl;
}
void mergesort(int a[],int l,int r)
{
   if(l>=r)
   {
       return;
   }
   int mid = l + (r-l)/2;
   mergesort(a,l,mid);
   mergesort(a,mid+1,r);
   merge(a,l,r);
}
int main()
{
   int n,m;
   cin>>n;
   int a[n];
   for(int i=0;i<n;i++)
   {
       cin>>a[i];
   }
   mergesort(a,0,n-1);
   for(int i=0;i<n;i++)
   {
       cout<<a[i]<<\' \';
   }
   cout<<endl;
}

Please Provide Code in Either JAVA or C++ and provide CLEAR! Explanatin on what you are doing in the code. For goodfeedbackand full points. 9 Problem The proble
Please Provide Code in Either JAVA or C++ and provide CLEAR! Explanatin on what you are doing in the code. For goodfeedbackand full points. 9 Problem The proble

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site