Question

In: Computer Science

#1. There is an error in the code. Not a grave error – the sort still...

#1. There is an error in the code. Not a grave error – the sort still works correctly. What is the
error?
( no code needs to be added or deleted to correct the error )
#2. The algorithm can be improved in terms of memory use. How? ( no new code is necessary )
# 3. What do lines 26-27 do ? ( copying arryptr into temp is not the complete answer. Be
specific )
#4.What do lines 30-31 do ? (copying arryptr into temp is not the complete answer. Be
specific )
#5. Draw a tree diagram ( as above ) illustrating the order of calls ( along with the mid ) for the
array
77 44 99 11 -666 22 55 -23

mergesortcode

template
void mergesort(T* arrayptr, const int& arraySize )
{
sortmerge( arrayptr, arraySize, 0, arraySize − 1 );
}

template
void sortmerge( T *arrayptr, int arraySize, int l, int r )
{

int mid, i, j, k;

T* temp = new T[ arraySize ];

if ( l < r )
{
mid = (r + l)/2;

sortmerge( arrayptr, arraySize, l, mid );
sortmerge( arrayptr, arraySize, mid + 1, r );


for ( i = mid + 1; i > l; i−− )
temp[ i − 1 ]= arrayptr[ i − 1 ];

for ( j = mid; j < r; j++ )
temp[ r + mid − j ] = arrayptr[ j + 1 ];

for ( k = l; k <= r; k++)
arrayptr[k] = ( temp[i] < temp[j] ) ? temp[i++] : temp[j−−];

temp = 0;
delete [] temp;
}

Solutions

Expert Solution

So I have put the code which I use for MergeSort. You can try using this if you want.

#include <stdio.h>
#include <conio.h>
#define size 100
void merge(int a[], int, int, int);
void merge_sort(int a[],int, int);
void main()
{
int arr[size], i, n;
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
printf("\n Enter the elements of the array: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
merge_sort(arr, 0, n-1);
printf("\n The sorted array is: \n");
for(i=0;i<n;i++)
printf(" %d\t", arr[i]);
getch();
}
void merge(int arr[], int beg, int mid, int end)
{
int i=beg, j=mid+1, index=beg, temp[size], k;
while((i<=mid) && (j<=end))
{
if(arr[i] < arr[j])
{
temp[index] = arr[i];
i++;
}
else
{
temp[index] = arr[j];
j++;
}
index++;
}
if(i>mid)
{
while(j<=end)
{

  temp[index] = arr[j];
j++;
index++;
}
}
else
{
while(i<=mid)
{
temp[index] = arr[i];
i++;
index++;
}
}
for(k=beg;k<index;k++)
arr[k] = temp[k];
}
void merge_sort(int arr[], int beg, int end)
{
int mid;
if(beg<end)
{
mid = (beg+end)/2;
merge_sort(arr, beg, mid);
merge_sort(arr, mid+1, end);
merge(arr, beg, mid, end);
}
}

1.) For the error, from what I could infer, there is a curly bracket missing. But if you are saying that your code is working then I do not think that is the error.

2.) For improving the code in terms of memory use, you are using a lot of for loops and for each iteration in each loop it will occupy more memory. Thus find a way to reduce the number of for loops. Maybe go through my code for reference.

3.) The code in lines 26-27 is just merging your arrays. For merge sort, we divide the given array in 2 halves and recursively sort it and then we need to merge it.

4.) The code in lines 30-31 is checking whether the elements are sorted in the array or not. Accordingly the value is incremented.


Related Solutions

this code still shows that it still has an syntax error in it can you please...
this code still shows that it still has an syntax error in it can you please fix it. I will put an error message next to the line that is wrong and needs fixed, other than that the other lines seems to be ok. public static void main() { /** * main method - makes this an executable program. */ public static void main("String[]args"); this is the error line that I get and needs fixed // create a client with...
Add bubble sort, radix sort, insertion sort, and merge sort to the code provided. Import a...
Add bubble sort, radix sort, insertion sort, and merge sort to the code provided. Import a data set (txt file) then do the sorting algorithm to measure how long it took and how many movements occurred. Please write codes in C++ Here's data set (should be stored in txt file) 7426 4524 4737 9436 3997 2757 6288 5414 9590 5968 6638 3199 9514 1541 9866 2144 6731 911 2171 6135 6437 912 9417 2662 6606 6349 707 2890 5386 9718...
IN C ++ PLEASE CODE FOR BUBBLE SORT---Add code to sort the bowlers. You have to...
IN C ++ PLEASE CODE FOR BUBBLE SORT---Add code to sort the bowlers. You have to sort their parallel data also. Print the sorted bowlers and all their info . You can use a bubble sort or a shell sort. Make sure to adjust your code depending on whether or not you put data starting in row zero or row one. Sort by Average across, lowest to highest.  The highest average should then be on the last row.. When you sort...
All code in JAVA please 1. Implement Insertion Sort 2. Implement Selection Sort *For problem 1...
All code in JAVA please 1. Implement Insertion Sort 2. Implement Selection Sort *For problem 1 and 2, please: a. Let the program generate a random array. b. Output both the original random array and the sorted version of it
Consider a sorting algorithm that combines merge sort and insertion sort algorithm. We still use divide...
Consider a sorting algorithm that combines merge sort and insertion sort algorithm. We still use divide and conquer like merge sort, however when the number of elements in an array is at most k elements (k is a parameter), we stop dividing the elements as the regular merge sort, instead, we call the insertion sort. Assuming we have defined the following two procedures: insertion-sort(A[p..q]) which sort the subarray A[p..q] merge(A[p,q,r]) which merges the sorted subarray A[p..r] and A[r+1..q] Try to...
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers....
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers. Record the time. Run Bubble Check time (compute the processing time) do it 100 times (random numbers) Take the average Insertion: Compare] (some explanations please)
Write code for A counting sort is a technique to sort an array of n positive...
Write code for A counting sort is a technique to sort an array of n positive integers that lie between 0 and m, inclusive. You need m + 1 counters. Then, making only one pass through the array, you count the number of times each integer occurs in the array. For example, the figure below shows an array of integers that lie between 0 and 4 and the five counters after a counting sort has made its pass through the...
The following flow chart is a bubble sort. Write the code for this sort. Make sure...
The following flow chart is a bubble sort. Write the code for this sort. Make sure you display each pass and comparison. Include comments.
what is cradle to grave, grave to the gate boundary and define each steps with an...
what is cradle to grave, grave to the gate boundary and define each steps with an example
Please let me know how to make code sort. If you put sort (sort 1000 6...
Please let me know how to make code sort. If you put sort (sort 1000 6 5 4 3 2 1, not separate), you will get 1 2 3 4 5 6 1000. sort 50 300 27 5 5 27 50 300 public static void main (String[] args) {        Scanner scnr = new Scanner(System.in);        String a = "";            a = scnr.nextLine();            String[] b = imput.split(" ") if (b[0].equalsI("sort")) { }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT