Question

In: Computer Science

(a) Illustrate how the list 5, 13, 2, 25, 7, 17, 20, 8, 4 is sorted...

(a) Illustrate how the list 5, 13, 2, 25, 7, 17, 20, 8, 4 is sorted with Mergesort. Count the number of comparisons made. (b) Illustrate how the list 5, 13, 2, 25, 7, 17, 20, 8, 4 is sorted with Heapsort.

Solutions

Expert Solution

Program in C:

a)

#include<stdio.h>

void mergesort(int a[],int i,int j);
void merge(int a[],int i1,int j1,int i2,int j2);

int main()
{
   int a[30],n,i;
   printf("Enter no of elements:");
   scanf("%d",&n);
   printf("Enter array elements:");
  
   for(i=0;i<n;i++)
       scanf("%d",&a[i]);
      
   mergesort(a,0,n-1);
  
   printf("\nSorted array is :");
   for(i=0;i<n;i++)
       printf("%d ",a[i]);
      
   return 0;
}

void mergesort(int a[],int i,int j)
{
   int mid;
      
   if(i<j)
   {
       mid=(i+j)/2;
       mergesort(a,i,mid);       //left recursion
       mergesort(a,mid+1,j);   //right recursion
       merge(a,i,mid,mid+1,j);   //merging of two sorted sub-arrays
   }
}

void merge(int a[],int i1,int j1,int i2,int j2)
{
   int temp[50];   //array used for merging
   int i,j,k;
   i=i1;   //beginning of the first list
   j=i2;   //beginning of the second list
   k=0;
  
   while(i<=j1 && j<=j2)   //while elements in both lists
   {
       if(a[i]<a[j])
           temp[k++]=a[i++];
       else
           temp[k++]=a[j++];
   }
  
   while(i<=j1)   //copy remaining elements of the first list
       temp[k++]=a[i++];
      
   while(j<=j2)   //copy remaining elements of the second list
       temp[k++]=a[j++];
      
   //Transfer elements from temp[] back to a[]
   for(i=i1,j=0;i<=j2;i++,j++)
       a[i]=temp[j];
}

output:

Enter no of elements:9                                                                                                

Enter array elements:5 13 2 25 7 17 20 8 4                                                                            

                                                                                                                      

Sorted array is :2 4 5 7 8 13 17 20 25                                                                                

                                                                                                                      

...Program finished with exit code 0   


Related Solutions

(a) Illustrate how the list 5, 13, 2, 25, 7, 17, 20, 8, 4 is sorted...
(a) Illustrate how the list 5, 13, 2, 25, 7, 17, 20, 8, 4 is sorted with Mergesort. Count the number of comparisonsmade. (b) Illustrate how the list 5, 13, 2, 25, 7, 17, 20, 8, 4 is sorted with Heapsort
Problem 1 (4+3 marks). (a) Illustrate how the list 5, 13, 2, 25, 7, 17, 20,...
Problem 1 (4+3 marks). (a) Illustrate how the list 5, 13, 2, 25, 7, 17, 20, 8, 4 is sorted with Mergesort. Count the number of comparisons made. (b) Illustrate how the list 5, 13, 2, 25, 7, 17, 20, 8, 4 is sorted with Heapsort.
Data: 7,-5, -8, 7, 9, 15, 0, 2, 13, 8, 6, -2, 4 (a) Mean= Mode=...
Data: 7,-5, -8, 7, 9, 15, 0, 2, 13, 8, 6, -2, 4 (a) Mean= Mode= median= (b) Variance= Standard deviation= (c) Range= IQR(Interquartilerange)= (d) Mid-Range= Mid-Hinge=
5.) For the data set 2 4 4 5 7 8 9 10 12 12 13...
5.) For the data set 2 4 4 5 7 8 9 10 12 12 13 13 16 16 16 16 17 19 19 20 23 24 24 24 25 26 26 27 28 28 29 31 32 34 34 36 37 38 42 44 45 46 47 47 48 50 52 53 53 54 55 56 56 57 58 (a) Find the 80th percentile. The 80t percentile is =    (a) Find the 42nd percentile. The 42nd percentile is...
A=[ 7 8 -2 -6 7 4 1 ; 2 4 -4 -13 9 9 -12...
A=[ 7 8 -2 -6 7 4 1 ; 2 4 -4 -13 9 9 -12 ; 6 6 0 -9 8 9 -4 ; 1 8 -14 -22 5 8 -1 ; 4 9 -10 -14 7 4 -1] B=[ 19 4 4 14 -3 -7 -5 ; 21 -6 -5 10 14 -2 4 ; 22 -4 5 13 5 -6 4 ; 41 20 0 26 11 -1 -27 ; 29 14 -2 20 3 -4 -19]...
A = [4, 5, 9] B = [-4, 5, -7] C = [2, -7, -8, 5]...
A = [4, 5, 9] B = [-4, 5, -7] C = [2, -7, -8, 5] D = [1, -9, 5, -3] E = [3, 3, -1] Uz = 1/|z| ^z d(X,Y) = (Rθ) d = diameter R = Radius θ = Theta Find a. Uc b. d (D, C) c. Let P = B + 3E, UP = d. A x B e. 3B x E f. C x D
Using the following data set: 10, 5, 2, 7, 20, 3, 13, 15, 8, 9 Apply...
Using the following data set: 10, 5, 2, 7, 20, 3, 13, 15, 8, 9 Apply the Merge sort algorithm. [Show to sorting process of the first half only] Apply the Quick sort algorithm [Show the first partitioning implementation]
Q4: . The sorted values array contains the sixteen integers 1, 2, 3, 13, 13, 20,...
Q4: . The sorted values array contains the sixteen integers 1, 2, 3, 13, 13, 20, 24, 25, 30, 32, 40, 45, 50, 52, 57, 60. How many recursive calls are made by our binarySearch method given an initial invocation of binarySearch(45, 0, 15)? Answer Choices : 2     0     3     4     1
Match No. 1 2 3 4 5 6 7 8 9 10 11 12 13 14...
Match No. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Player A 8 42 56 68 91 123 12 46 57 137 5 80 14 10 19 Player B 38 44 46 59 57 61 48 42 51 39 58 41 55 45 68 1. For the given data set representing the runs scored by two players in last 15 matches, conduct the following analysis: i. Which average you will use to summarize...
A = (1 −7 5 0 0 10 8 2 2 4 10 3 −4 8...
A = (1 −7 5 0 0 10 8 2 2 4 10 3 −4 8 −9 6) (1) Count the number of rows that contain negative components. (2) Obtain the inverse of A and count the number of columns that contain even number of positive components. (3) Assign column names (a,b,c,d) to the columns of A. (4) Transform the matrix A into a vector object a by stacking rows. (5) Replace the diagonal components of A with (0,0,2,3). Hint:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT