Question

How to read an existing file content and then sort them using merge sort into (in...

How to read an existing file content and then sort them using merge sort into (in java)

1. alphabetical

2. shortest to the longest word

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

// Java code for above problem

import java.util.*;
import java.io.*;
class Main
{
   public static void main(String args[]) throws Exception
   {
       File file = new File("words.txt");
       BufferedReader br = new BufferedReader(new FileReader(file));
       ArrayList<String> arr=new ArrayList<String>();
       String str="";
       while ((str=br.readLine())!=null)       // read words from file and store them in a list
           arr.add(str);
       System.out.println("Before sorting: "+arr);
       mergeSort(arr,0,arr.size()-1);           // sort the list using merge sort method
       System.out.println("After sorting: "+arr);
   }
   public static void mergeSort(ArrayList<String> arr, int start, int end)
   {
       if(start>=end)
           return;
       int middle=(start+end)/2;
       mergeSort(arr,start,middle);
       mergeSort(arr,middle+1,end);
       merge(arr,start,middle,end);
   }
   public static void merge(ArrayList<String> arr,int start,int middle,int end)
   {
       int n1=middle-start+1;
       int n2=end-middle;
       ArrayList<String> arr1=new ArrayList<String>();
       for(int i=0;i<n1;i++)
           arr1.add(arr.get(start+i));
       ArrayList<String> arr2=new ArrayList<String>();
       for(int i=0;i<n2;i++)
           arr2.add(arr.get(middle+1+i));
       int i=0,j=0,k=start;
       while(i<n1 && j<n2)
       {
           int value=arr1.get(i).compareTo(arr2.get(j));
           if(value<0)
           {  
               arr.set(k,arr1.get(i));
               i++;
           }
           else
           {
               arr.set(k,arr2.get(j));
               j++;
           }
           k++;
       }
       while(i<n1)
       {
           arr.set(k,arr1.get(i));
           i++;
           k++;
       }
       while(j<n2)
       {
           arr.set(k,arr2.get(j));
           j++;
           k++;
       }
   }
}

Sample output:

let "words.txt" has following data:

aaa
abcd
abc
aa
a
zebra
home
my
word
sort

then following will be the output:

Before sorting: [aaa, abcd, abc, aa, a, zebra, home, my, word, sort] After sorting: [a, aa, aaa, abc, abcd, home, my, sort, w

// Mention in comments if any mistakes or errors are found. Thank you.

Add a comment
Know the answer?
Add Answer to:
How to read an existing file content and then sort them using merge sort into (in...
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
  • 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....

  • 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....

  • 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....

  • Program with generic merge sort and binary search method help. The programming language I'm using is...

    Program with generic merge sort and binary search method help. The programming language I'm using is Java. This program should show understanding generic merge sort methods and generic binary search methods in java. The execution should include at least 5 found items including one from the first three items in the sorted array and one from the last three items in the sorted array as well as at least two items not found Create a generic merge sort method that...

  • 6 6. Merge Bubble Sort: a) How does the merge bubble sort break the array into...

    6 6. Merge Bubble Sort: a) How does the merge bubble sort break the array into sublists? b) What does it need to use for each sublist and then what does it do with all of the sublists? c) What is the Big-O notation for this sort? 7. Merge Sort: a) How does the merge sort use recursion to break the array into sublists? b) What happens to each of these sublists to get the final sorted list? c) What...

  • Consider a variation of Merge sort called 4-way Merge sort. Instead of splitting the array into...

    Consider a variation of Merge sort called 4-way Merge sort. Instead of splitting the array into two parts like Merge sort, 4-way Merge sort splits the array into four parts. 4-way Merge divides the input array into fourths, calls itself for each fourth and then merges the four sorted fourths. a)Implement 4-way Merge sort from Problem 4 to sort an array/vector of integers and name it merge4. Implement the algorithm in the same language you used for the sorting algorithms...

  • Show step by step how the merge procedure of merge sort will merge the arrays 1,...

    Show step by step how the merge procedure of merge sort will merge the arrays 1, 3, 4, 7, 9, 11, 13, 14 and 2, 5, 6, 8, 10, 12

  • Using Java programming Using System.nanoTime(), compare the performance of bucketsort with the sorting algorithms insertion sort, quicksort, merge sort. Does it really perform faster in practice?...

    Using Java programming Using System.nanoTime(), compare the performance of bucketsort with the sorting algorithms insertion sort, quicksort, merge sort. Does it really perform faster in practice?

  • Show how to sort the sequence by using 3-merge-sort respectively,each time you divide the sequence into...

    Show how to sort the sequence by using 3-merge-sort respectively,each time you divide the sequence into 3 sub arrays instead of 2 subarrays 2 90 3 25 46 7 8 9 0 74 100

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
Active Questions
ADVERTISEMENT