HELP PLEASE!! Implement a method: public static void selectionSort(String [] wor
ID: 3546445 • Letter: H
Question
HELP PLEASE!!
Explanation / Answer
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.io.*;
public class HelloWorld{
public static String[] selectionSort(String[] s, int f){
for (int i = f - 1; i >= 1; i--){
String currentMax = s[0];
int currentMaxIndex = 0;
for (int j = 1; j <= i; j++) {
if (currentMax.compareTo(s[j]) < 0) {
currentMax = s[j];
currentMaxIndex = j;
}
}
if (currentMaxIndex != i) {
s[currentMaxIndex] = s[i];
s[i] = currentMax;
}
}
return s;
}
public static String[] insertionSort(String array[], int f){
String temp="";
for(int i=0;i<f;i++){
for(int j=i+1;j<f;j++){
if(array[i].compareToIgnoreCase(array[j])>0){
temp = array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
return array;
}
public static void main(String []args){
Scanner in = new Scanner(System.in);
String s;
System.out.println("Enter a string");
s = in.nextLine();
BufferedReader br = null;
List<String> lines = new ArrayList<String>();
try {
String sCurrentLine;
int i=0;
br = new BufferedReader(new FileReader(s));
while ((sCurrentLine = br.readLine()) != null) {
lines.add(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
String[] unSorttedArray = lines.toArray(new String[0]);
String[] sorted=new String[100];
System.out.println("Output of selection sort");
sorted=selectionSort(unSorttedArray,unSorttedArray.length);
for(int i=0;i<sorted.length;i++)
{
System.out.println(sorted[i]);
}
System.out.println("Output of insertioin sort");
sorted=insertionSort(unSorttedArray,unSorttedArray.length);
for(int i=0;i<sorted.length;i++)
{
System.out.println(sorted[i]);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.