Question

Demo Your instructor will now debug the below faulty QuickSort example int size 8,W makes code simpler void swapint xp, int
Problem2 In our lecture notes,e avehown that the total number of comparisons needed for selection sort should be roughly 2*n
Problem 3 Below you will see a partial implementation of Merge Sort Please complete the implementation. You are only allowed
A successful output should look like this: Original: 17 22 10 22 9 30 252 Merpe Reault 17 22 Mcrge Result: 10 2 Merge Rewalt:
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Modified merge program

#include<stdio.h>

int b[100];

void DoMerging(int a[] ,int startIndex , int midIndex , int endIndex){
       int first1= startIndex;
   int last1= midIndex;
   int first2 = midIndex+1;
   int last2 = endIndex;
   int index = first1;
  
   while(first1<=last1 && first2<=last2) {
       if(a[first1]<a[first2]) {
           b[index] = a[first1];
           first1++;
       }
       else {
           b[index] = a[first2];
           first2++;
          
       }
       index++;
      
   }
   while(first1<=last1) {
       b[index++] = a[first1++];
  
      
   }
   while(first2<=last2) {
       b[index++] = a[first2++];
  
   }
  
  
   int k;
   printf("Merge Result : ");
   for(k=startIndex;k<=endIndex ; k++){
       a[k] = b[k];
       printf("%d ",b[k]);
      
   }
   printf(" ");
}

void MergeSort(int a[] ,int startIndex ,int endIndex){
   int midIndex;
   if(startIndex<endIndex){
       midIndex = (startIndex+endIndex)/2;
       MergeSort(a,startIndex,midIndex);
       MergeSort(a,midIndex+1,endIndex);;
       DoMerging(a,startIndex,midIndex,endIndex);
   }
   else return;
}

int main(){
   int i;
   int a[]={17,22,10,22,49,30,25,2};
  
   printf("Original : ");
   for(i=0;i<8;i++)printf("%d ",a[i]);
   printf(" ");
   MergeSort(a,0,7);
  
   printf("Final Result : ");
   for(i=0;i<8;i++)printf("%d ",a[i]);
   printf(" ");
   return 0;
}

//sample output

C:\Users IshuManish\Documents\Merse.exe erge Result Merge Result 10 17 22 22 17 22 Merge Result 30 49 erge Result 2 25 rge Re

Add a comment
Know the answer?
Add Answer to:
Demo Your instructor will now debug the below faulty QuickSort example int size 8,W makes code...
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
  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble...

    C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble with is in bold. -------------------------------------------------------------------------------------------------driverProgram.cpp #include #include #include #include #include "quickSort.cpp" using namespace std; int main() { const int MIN_SIZE = 4; //Array size const int SIZE = 25; int theArray[SIZE] = {11, 22, 33, 44, 55, 66, 77, 88, 99, 12, 13, 14, 15, 16, 17, 18, 19, 18, 19, 20, 21, 22, 23, 24, 25}; cout << "List of 25 items: ";...

  • HW60.1. Array Quicksort You've done partition so now it's time to finish Quicksort. Create a publ...

    HW60.1. Array Quicksort You've done partition so now it's time to finish Quicksort. Create a public non-final class named Quicksort that extends Partitioner. Implement a public static method void quicksort (int] values) that sorts the input array of ints in ascending order. You will want this method to be recursive, with the base case being an array with zero or one value. You should sort the array in place, which is why your function is declared to return void. If...

  • c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each...

    c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...

  • Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Jav...

    Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Java program provided: // Student Name Today's Date import java.util.Arrays; import java.util.Random; public class SortTimer {    // Please expand method main() to meet the lab requirements.       // You have the following sorting methods available:    // insertionSort(int[] a);    // selectionSort(int[] a);    // mergeSort(int[] a);    // quickSort(int[] a);    // The array will be in sorted order after the routines are called!   ...

  • Implement quicksort and bucket sort. Use the code in your book to help; the partition in...

    Implement quicksort and bucket sort. Use the code in your book to help; the partition in quicksort is tricky. Make sure your implementations are correct — it is easy to gain some confidence in the correctness of your code by writing a program which creates arrays filled with random numbers, sorts them, and then checks that they are sorted. Then time your code (using clock() or similar methods) on both methods for arrays filled with random integers of the following...

  • Code in C language ADT: typedef struct{ int ID; float salary; int age; }Employee; ...

    code in C language ADT: typedef struct{ int ID; float salary; int age; }Employee; Specification: In this lab, five functions need to be implemented using the given ADT. 1. Employee* readRecord(FILE*) This function receives a FILE pointer created before. It reads a line from the provided csv file, and creates an Employee struct pointer with the information from that line then returns the pointer back to the calling function. Each line in the provided csv file contains the id, salary,...

  • The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also...

    The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also please explain why the seg fault is happening. Thank you #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name loadMusicFile // @Brief Load the music database // 'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size;...

  • I want to compare the runtimes and swap operations times among quick Sort, selection Sort and...

    I want to compare the runtimes and swap operations times among quick Sort, selection Sort and shell Sort here is my code: But when I create a 1000,000 size array, I can't get the result of the operations times and runtime. what's wrong with my code? and I also want to copy the array. Because I want to use same array for three sort. And for the shell Sort, I haven't learn it in my class. Can anyone help me...

  • must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int...

    must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...

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
ADVERTISEMENT