Please do the program in JAVA Sorting file: lines in a new file must be in alpha
ID: 3788745 • Letter: P
Question
Please do the program in JAVA
Sorting file: lines in a new file must be in alphabetical order after sorting Small and capital letters are treated the same: A and a are the same letter; not case sensitive Describe algorithm in README.txt file, it can be drawing Searching: find word in the file Describe algorithm in README.txt file, it can be drawing. Estimate theoretically number of operations, depends of input n, in terms big-o notation Insert variable counter and calculate exact number of operations. Test your program with data, that gives you minimum, maximum, average number of operations. Provide input and output files for all cases. You can use any sorting method. What generalization of the problem can you offer to continue development of this problem? How can you apply solution of this problem in your life?Explanation / Answer
File Sorting
-------------------------------------------------------------------------------------------------------------
package com.game;
import java.io.*;
import java.util.*;
public class Sort {
private static int operation_Count=0;
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new FileReader("D:\abc.txt"));
operation_Count += 3;
Map<String, String> map=new TreeMap<String, String>();
operation_Count += 2;
String line="";
operation_Count += 1;
while((line=reader.readLine())!=null){
operation_Count+= 3;
map.put(getField(line),line);
operation_Count += 2;
}
reader.close();
operation_Count += 1;
FileWriter writer = new FileWriter("D:\abc.txt");
operation_Count += 1;
for(String val : map.values()){
operation_Count+=2;
writer.write(val);
operation_Count += 1;
writer.write(" ");
operation_Count += 1;
}
writer.close();
operation_Count += 1;
System.out.println("Total Operations are : "+operation_Count);
}
private static String getField(String line) {
operation_Count += 1;
return line.split(" ")[0];//extract value you want to sort on
}
}
---------------------------------------------------------------------------------------------------------------------------------
Theorital no of operation in terms of big-o notation - O(n)
----------------------------------------------------------------------------------------------------------------------------------
Input Test Use Case 1
Input File Records
hello
apple
------------------------------------------------------------------------------------------------------------------------------------
Minimum operations : 29
------------------------------------------------------------------------------------------------------------------------------------
Input Test Use Case 2
Input File Records
hello
apple
I
am
not
a
baby
------------------------------------------------------------------------------------------------------------------------------------
Minimum operations : 79
------------------------------------------------------------------------------------------------------------------------------------
Part 2 : Searching
-----------------------------------------------------------------------------------------------------------------------------------
package com.game;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Searcher {
private static int operation_Count=0;
public static void main(String[] args){
Scanner txtscan;
try {
txtscan = new Scanner(new File("D:\abc.txt"));
operation_Count += 3;
boolean isExists = false;
operation_Count += 1;
while(txtscan.hasNextLine()){
operation_Count += 1;
String str = txtscan.nextLine();
operation_Count += 2;
if(str.indexOf("word") != -1){
operation_Count += 2;
System.out.println("EXISTS");
operation_Count += 1;
isExists = true;
operation_Count += 2;
break;
}
operation_Count += 1;
}
if(!isExists){
System.out.println("Not Exist");
operation_Count += 2;
}
} catch (FileNotFoundException e) {
operation_Count += 1;
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("No of Operations : "+operation_Count);
}
}
--------------------------------------------------------------------------------------------------------------------------------
Theorital no of operation in terms of big-o notation - O(n)
----------------------------------------------------------------------------------------------------------------------------------
Input Test Use Case 1
Input File Records
word
I
a
am
apple
baby
hello
not
------------------------------------------------------------------------------------------------------------------------------------
Minimum operations : 12
------------------------------------------------------------------------------------------------------------------------------------
Input Test Use Case 2
Input File Records
I
a
am
apple
baby
hello
not
word
------------------------------------------------------------------------------------------------------------------------------------
Minimum operations : 40
------------------------------------------------------------------------------------------------------------------------------------