In: Computer Science
2. Write a Java program that reads a series of input lines from given file “name.txt”, and sorts them into alphabetical order, ignoring the case of words. The program should use the merge sort algorithm so that it efficiently sorts a large file.
Contents of names.text
Slater, KendallLavery, RyanChandler, Arabella "Babe"Chandler, StuartKane, EricaChandler, Adam JrSlater, ZachMontgomery, JacksonChandler, KrystalMartin, JamesMontgomery, BiancaCortlandt, PalmerDevane, AidanMadden, JoshHayward, DavidLavery,k JonathanSmythe, GreenleeCortlandt, OpalMcDermott, AnnieHenry, DiGrey, MariaEnglish, BrookeKeefer, JuliaMartin, JosephMontgomery, LilyDillon, AmandaColby, LizaStone, Mary FrancesChandler, ColbyFrye, DerekMontgomery, ReggieMontgomery, SeanSantos, HayleySantos, MateoDillon, JanetJefferson, KelseyChandler, MarianFargate, MyrtleHenry, DelCodahy, LiviaWarner, AnitaLavery, SpikeMartin, RuthMontgomery, Barbara
ANSWER:
I have provided the properly commented
and indented code so you can easily copy the code as well as check
for correct indentation.
I have provided the output image of the code so you can easily
cross-check for the correct output of the code.
Have a nice and healthy day!!
CODE
import java.util.*;
import java.io.*;
public class Main
{
public static String [] mergeSort(String [] array){
String [] list = array;
if(list.length > 1)
{
// mid of list
int mid = list.length / 2;
// Split left part
String[] left = new String[mid];
for(int i = 0; i < mid; i++)
{
left[i] = list[i];
}
// Split right part
String[] right = new String[list.length - mid];
for(int i = mid; i < list.length; i++)
{
right[i - mid] = list[i];
}
// calling mergeSort
mergeSort(left);
mergeSort(right);
int i = 0;
int j = 0;
int k = 0;
// Merge left and right arrays
while(i < left.length && j < right.length)
{
// compare left right string
// using
if(left[i].compareTo(right[j]) < 0)
{
list[k] = left[i];
i++;
}
else
{
list[k] = right[j];
j++;
}
k++;
}
// Collect remaining elements
while(i < left.length)
{
list[k] = left[i];
i++;
k++;
}
while(j < right.length)
{
list[k] = right[j];
j++;
k++;
}
}
return list;
}
public static void main(String[] args) {
// reading names.txt
File namesFile = new File("names.txt");
try
{
// creating scanning object on file object
Scanner reader = new Scanner(namesFile);
// reading file line by line,
// creating complete file string object
String data = "";
while (reader.hasNextLine()) {
// reading line
String line = reader.nextLine();
// storing line in data, using , seprator
data = ","+line;
}
// display content
// System.out.println(data);
// spliting string on , using string split method
String[] names = data.split(",");
// using strip to remove trailing spaces from names
for(int i=0;i<names.length;i++){
names[i] = names[i].trim();
System.out.println(names[i]);
}
// merges rot
String[] sorted = mergeSort(names);
System.out.println("");
// using strip to remove trailing spaces from names
for(int i=0;i<sorted.length;i++){
System.out.println(sorted[i]);
}
}
catch (FileNotFoundException ex)
{
// insert code to run when exception occurs
System.out.println("FIle not found");
}
}
}
OUTPUT IMAGE
SORTED