Hello, I need to implement a sorting algorithm using NetBeans 8.2 and Java langu
ID: 3810383 • Letter: H
Question
Hello, I need to implement a sorting algorithm using NetBeans 8.2 and Java language.
The sorting algorightm is Selection Sort. This algorithm will have to sort numbers in ascending order. Has to read the input file that will be a file with 10 randoms numbers. The file name is 10.txt.
For example. 10.txt:
-1,2,3,-2,10,24,-68,-90,98,54
You must also have a function confirmSorted(). This function should take your array of numbers, verify that it is sorted in ascending order, and return a Boolean. If the numbers are confirmed to be sorted, print out “Confirmed sorted” and if they aren’t sorted, print out “Confirmed NOT sorted”. Do this before and after the sort.
Also, all programming languages have ways to get elapsed time in milliseconds. The basic idea is to get the time (in milliseconds) before sorting, after sorting, and then find the difference between them.
This image is just an example of how it should look after running the program.
Sample output Below is an idea of what kind of output your program should have Reading data from 100000.txt Confirmed NOT sorted. Sorting using Quicksort. It took 150 ms. Confirmed SortedExplanation / Answer
HI, PLease find my implementation.
Please let me know in case of any issue.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class FileIntegerSorting {
public static boolean confirmSorted(int[] arr){
for(int i=0; i<arr.length-1; i++)
if(arr[i] > arr[i+1])
return false;
return true;
}
public static void doSelectionSort(int[] arr){
for (int i = 0; i < arr.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < arr.length; j++)
if (arr[j] < arr[index])
index = j;
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
}
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(System.in);
System.out.print("Enter input file name: ");
String fileName = sc.next();
// opening input file name
Scanner fileScanner = new Scanner(new File(fileName));
// creating an array of size 10
int size = 10;
int arr[] = new int[size];
int i =0 ;
while(fileScanner.hasNextInt())
arr[i++] = fileScanner.nextInt();
fileScanner.close();
sc.close();
if(confirmSorted(arr)){
System.out.println("Confirmed Sorted");
}else{
System.out.println("Confirmed NOT Sorted");
System.out.println("Sorting using SelectionSort");
long start = System.nanoTime();
doSelectionSort(arr);
System.out.println("It took "+(System.nanoTime()-start)+" ns");
System.out.println("Confirmed Sorted");
}
}
}
/*
######### 10.txt #############
-1 2 3 -2 10 24 -68 -90 98 54
Sample run:
Enter input file name: 10.txt
Confirmed NOT Sorted
Sorting using SelectionSort
It took 85188 ns
Confirmed Sorted
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.