Question

Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max...

Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max = 3,000,000 numbers), sorts those numbers and indicates time consumption. This programming question will address the advantage of using iteration loops over recursive calls as well as using INSERTION-SORT() as a procedure in MERGESORT(). Your program must perform the following actions:
1. Opens the given file name and reads all double numbers. For simplicity, we assume this file only contains numbers and nothing else.

2. Implements the function INSERTION-SORT() that only sort an array of maximum 25 numbers. The idea is that INSERTION-SORT() will be used as a sub-procedure to sort any sub-array when its size is small enough.

3. Four versions of MERGE-SORT() namely a. MERGE-SORT-A(): Using recursive calls and NO INSERTION-SORT() as a sub-procedure b. MERGE-SORT-B(): Using ITERATIVE loops (i.e, NO recursion) and NO INSERTION-SORT() as a subprocedure. c. MERGE-SORT-C(): Using recursive calls and INSERTION-SORT() as a sub-procedure. d. MERGE-SORT-D(): Using ITERATIVE loops (i.e, NO recursion) and INSERTION-SORT() as a subprocedure.

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

solution:
1.program to read the numbers from file:-

FILE *f=fopen("inputHW02.txt");   

int intergersarray[10000]; //array to store values from text file

int i=0,num;

while(fscanf(f,%d,&num) && !feof(f)) //reads until it reaches the end of file

{

intergersarray[i]=num;

i++;

}

   fclose(f);

2.implementation of insertion sort as a sub procedure:

INSERTION_SORT(int array[], int n)

{

int i,val,j;

for(i=1;i

val=array[i];

j=i-1;

while(j>=0&&array[j]>val){

array[j+1]=array[j];

j--;

}

array[j+1]=key;

}

}

3.MERGE-SORT-A:

void mergesort(int array[],int l,int r){
if(l

int m=l+(r-1)/2

mergesort(array,l,m);

mergesort(array,m+1,r);

merge(array,l,m,r);

}

}

void merge(int array[],int l,int m,int r){

int i,j,k;

int p=m-l+1;

int q=r-m;

int L[p],R[q];

for(i=0;i

L[i]=array[l+i];

}

for(j=0;j

R[i]=array[m+1+j];

}

i=0;

j=0;

k=l;

while(i

if(L[i]<=R[i]){

arr[k]=L[i];

i++;

}

else{

array[k]=R[j];

j++;

}

k++;

   }

while(i

arr[k]=L[i];

i++;

k++;

}

while(j

arr[k]=R[j];

j++;

k++;

}

}

MERGE_SORT_B:-

  void mergeSort(int array[], int n)

{

int current;

int l;

for(current=1;current<=n-1;current = 2*current)

  {

  for (l=0;l

  {

int m=min(left+current-1,n-1);

int r=min(l+ 2*current-1,n-1);

  merge(arr,l,m,r);

  }

  }

}

void merge(int array[],int l,int m,int r){

int i,j,k;

int p=m-l+1;

int q=r-m;

int L[p],R[q];

for(i=0;i

L[i]=array[l+i];

}

for(j=0;j

R[i]=array[m+1+j];

}

i=0;

j=0;

k=l;

while(i

if(L[i]<=R[i]){

arr[k]=L[i];

i++;

}

else{

array[k]=R[j];

j++;

}

k++;

   }

while(i

arr[k]=L[i];

i++;

k++;

}

while(j

arr[k]=R[j];

j++;

k++;

}

}

MERGE_SORT_C:

here we are performing insertion sort in the insertion sort if the array size is less than 25.

void mergesort(int array[],int l,int r){

if(r-l <= 25){

insertion_sort(array,l,r);

   else{

int m=l+(r-1)/2

mergesort(array,l,m);

mergesort(array,m+1,r);

merge(array,l,m,r);

}

}

void insertion_sort(int array[],int l,int r){

int val;

for(i=l;i

val=array[i];

j=i-1;

while(j>=l&&array[j]>val){

array[j+1]=array[j];

j--;

}

array[j+1]=key;

}

}

void merge(int array[],int l,int m,int r){

int i,j,k;

int p=m-l+1;

int q=r-m;

int L[p],R[q];

for(i=0;i

L[i]=array[l+i];

}

for(j=0;j

R[i]=array[m+1+j];

}

i=0;

j=0;

k=l;

while(i

if(L[i]<=R[i]){

arr[k]=L[i];

i++;

}

else{

array[k]=R[j];

j++;

}

k++;

   }

while(i

arr[k]=L[i];

i++;

k++;

}

while(j

arr[k]=R[j];

j++;

k++;

}

}

MERGE_SORT_D:

void mergeSort(int array[], int n)

{

int current;

int l;

for(current=1;current<=n-1;current = 2*current)

  {

  for (l=0;l

  {

int m=min(left+current-1,n-1);

int r=min(l+ 2*current-1,n-1);

if(r-l<=25){

insertionsort(array,l,r);

else{

  merge(arr,l,m,r);

}

  }

  }

}

void insertion_sort(int array[],int l,int r){

int val;

for(i=l;i

val=array[i];

j=i-1;

while(j>=l&&array[j]>val){

array[j+1]=array[j];

j--;

}

array[j+1]=key;

}

}

void merge(int array[],int l,int m,int r){

int i,j,k;

int p=m-l+1;

int q=r-m;

int L[p],R[q];

for(i=0;i

L[i]=array[l+i];

}

for(j=0;j

R[i]=array[m+1+j];

}

i=0;

j=0;

k=l;

while(i

if(L[i]<=R[i]){

arr[k]=L[i];

i++;

}

else{

array[k]=R[j];

j++;

}

k++;

   }

while(i

arr[k]=L[i];

i++;

k++;

}

while(j

arr[k]=R[j];

j++;

k++;

}

}

  

  


  
  

Know the answer?
Add Answer to:
Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max...
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
  • Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max...

    Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max = 3,000,000 numbers), sorts those numbers and indicates time consumption. This programming question will address the advantage of using iteration loops over recursive calls as well as using INSERTION-SORT() as a procedure in MERGESORT(). Your program must perform the following actions: 1. Opens the given file name and reads all double numbers. For simplicity, we assume this file only contains numbers and nothing else....

  • Python Merge sort algorithm...

    Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max = 3,000,000 numbers), sorts those numbers and indicates time consumption. This programming question will address the advantage of using iteration loops over recursive calls as well as using INSERTION-SORT() as a procedure in MERGESORT(). Your program must perform the following actions: 1. Opens the given file name and reads all double numbers. For simplicity, we assume this file only contains numbers and nothing else....

  • Java Merge sort algorithm

    Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max = 3,000,000 numbers), sorts those numbers and indicates time consumption. This programming question will address the advantage of using iteration loops over recursive calls as well as using INSERTION-SORT() as a procedure in MERGESORT(). Your program must perform the following actions: 1. Opens the given file name and reads all double numbers. For simplicity, we assume this file only contains numbers and nothing else....

  • Part A Analyze the following recurrences and show their time complexity functions using (I) iteration method...

    Part A Analyze the following recurrences and show their time complexity functions using (I) iteration method and (2) Master Theorem. AI. T(n) = 2T 3 A2. T(n) = 3T 2n АЗ. Т(п) — Т(п — 2) + 3 А4. Т(п) — 2Т (п — 1) + 1 A5. T(n)= 4T +n log n A6. T(n) = 3T +n log n n2 A7. T(n) = 27 Part B Do 2.3-4 (р39) and Problem 2-1 (р39) Part C Implement MERGE-SORT() algorithm that...

  • Part B (BI). Implement a Red-Black tree with only operation Insert(). Your program should read from...

    Part B (BI). Implement a Red-Black tree with only operation Insert(). Your program should read from a file that contain positive integers and should insert those numbers into the RB tree in that order. Note that the input file will only contain distinct integers. Print your tree by level using positive values for Black color and negative values for Red color Do not print out null nodes. Format for a node: <Node_value>, <Parent_value>). For example, the following tree is represented...

  • Write a program that will take input from a file of numbers of type double and...

    Write a program that will take input from a file of numbers of type double and output the average of the numbers in the file to the screen. Output the file name and average. Allow the user to process multiple files in one run. Part A use an array to hold the values read from the file modify your average function so that it receives an array as input parameter, averages values in an array and returns the average Part...

  • Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed...

    Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed as command-line arguments to MergeFiles as follows: java MergeFiles filel file2 mergedFile II. MergeFiles reads names stored in files file and file2 III. Merges the two list of names into a single list IV. Sorts that single list V. Ignores repetitions VI. Writes the sorted, free-of-repetitions list to a new file named mergedFile.txt VII. The names in all three files are stored as one...

  • Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In...

    Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In this project, we combine the concepts of Recursion and Merge Sorting. Please note that the focus of this project is on Merging and don't forget the following constraint: Programming Steps: 1) Create a class called Art that implements Comparable interface. 2) Read part of the file and use Merge Sort to sort the array of Art and then write them to a file. 3)...

  • C++ PROGRAM ONLY! For this lab you need to write a program that will read in...

    C++ PROGRAM ONLY! For this lab you need to write a program that will read in two values from a user and output the greatest common divisor (using a recursive implementation of the Euclidean algorithm) to a file. In Lab #3, you implemented this program using an iterative method. Greatest Common Divisor In mathematics, the greatest common divisor (GCD) of two or more integers (when at least one of of them is zero then the larger value is the GCD....

  • Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that...

    Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that will process monthly sales data for a small company. The data will be used to calculate total sales for each month in a year. The monthly sales totals will be needed for later processing, so it will be stored in an array. Basic Logic for main() Note: all of the functions mentioned in this logic are described below. Declare an array of 12 float/doubles...

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