DESCRIPTION
Requirements
Write a program that performs a Bubble sort
You will read input from a file (uses starter code from Mr M. below)
Your program should run on O(n^2) time with an early exit for sorted data
Your program must output each major step it takes in sorting the data
Clarification: Print once line for each execution of your outer loop
If you implement the challenges below, the program should use the original list each time
CHALLENGE (+10%)
Option #1 - Basic
Implement two extra O(n²) sorts
Examples: Selection Sort, Insertion Sort
Option #2 - Jokes
Implement two joke sorts
Examples: Bogo Sort, Stalin Sort
Option #3
Implement one O(n log n) sort
Examples: Merge Sort, Quick Sort
EXAMPLE: RUNNING PROGRAM
Choose a Sort (0) Quit (1) Bubble
> 1
5 3 2 1 9 82 7
3 2 1 5 9 7 82
2 1 3 5 7 9 82
1 2 3 5 7 9 82
Choose a Sort (0) Quit (1) Bubble
> 0
Using Files
Create a file called data.txt
Fill it with a set of numbers on a single line
Update LIST_SIZE in your code to match the numbers in the list
Ex: data.txt
5 3 2 1 9 82 7
CODE FRAMEWORK
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main
{
final static int LIST_SIZE = 7;
static int[] numbers = new int[LIST_SIZE];
// Write this method
public static void printList()
{
}
// Write this method.
public static void bubbleSort()
{
}
// Modify this method as needed (challenge only)
public static void menu()
{
Scanner sc = new Scanner(System.in);
System.out.println("Choose a Sort: (0) Quit (1) Bubble");
System.out.print(">");
int choice = sc.nextInt();
long startTime = System.nanoTime();
if(choice == 1)
{
bubbleSort();
}
else
{
System.exit(0);
}
long runTime = (System.nanoTime() - startTime) / 1000;
System.out.println("Runtime " + runTime + " ms");
System.out.println();
}
// This method is completed - do not modify
public static void main(String[] args) throws FileNotFoundException
{
File file = new File("data.txt");
while(true)
{
Scanner sc = new Scanner(file);
for(int i = 0; i < LIST_SIZE; i++)
{
numbers[i] = sc.nextInt();
}
sc.close();
menu();
}
}
}