Respuesta :
Answer:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class InsertionSort {
public static void insertionSort(String[] array) {
int n = array.length;
for (int i = 1; i < n; i++)
{
String temp = array[i];
int j = i - 1;
while (j >= 0 && temp.compareTo(array[j]) < 0)
{
array[j + 1] = array[j];
j--;
}
array[j+1] = temp;
}
}
public static void main(String[] args) {
if (args.length == 2) {
int n = Integer.parseInt(args[0]);
File file = new File(args[1]);
try {
Scanner fin = new Scanner(file);
String[] strings = new String[n];
for (int i = 0; i < n; i++) {
strings[i] = fin.nextLine();
}
insertionSort(strings);
for (int i = 0; i < n; i++) {
System.out.println(strings[i]);
}
fin.close();
} catch (FileNotFoundException e) {
System.out.println(file.getAbsolutePath() + " is not found!");
}
} else {
System.out.println("Please provide n and filename as command line arguments");
}
}
}
Explanation: