0% found this document useful (0 votes)
37 views

Java Practice - Algorithms

The document describes a program that implements various sorting algorithms to sort an array of float numbers. The program includes 6 functions: 1) Enter array elements and save to a file, 2) Read from file and display array, 3) Implement bubble sort and save each step to a file, 4) Implement selection sort and save steps to a file, 5) Implement insertion sort and save steps to a file, 6) Calculate running times of the sorting algorithms. The program uses a menu driven interface for user input.

Uploaded by

Dominique Bacani
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Java Practice - Algorithms

The document describes a program that implements various sorting algorithms to sort an array of float numbers. The program includes 6 functions: 1) Enter array elements and save to a file, 2) Read from file and display array, 3) Implement bubble sort and save each step to a file, 4) Implement selection sort and save steps to a file, 5) Implement insertion sort and save steps to a file, 6) Calculate running times of the sorting algorithms. The program uses a menu driven interface for user input.

Uploaded by

Dominique Bacani
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

****Build program about array of float number.

The program incluedes 6 functions:

1. Enter a array of float numbers consiting of n numbers (n<=20) and save its to
the file INPUT.TXT.

2. Read data from the INPUT.TXT file, save it to a 1-demensional array and display
it on the screen.

3. Display the results of sorting the elements in the array a in item 2 in


ascending order, according to each step of the Bubble Sort algorithm (each step
displays up to 1 line) and save it to the file OUTPUT1.TXT

4. Display the results of sorting the elements in the array a in item 2 in


ascending order, according to each step of the Selection Sort algorithm (each step
displays up to 1 line) and save it to the file OUTPUT2.TXT

5. Display the results of sorting the elements in the array a in item 2 in


ascending order, according to each step of the Insert Sort algorithm (each step
displays up to 1 line) and save it to the file OUTPUT3.TXT.

6. Calculate the running time of the 3 algorithms: BubbleSort, SelectionSort and


InsertionSort.

***You must use this template:

public class algorithm {

public static void writeFlie(float a[]) {

public static int[] readFile(float a[], int size) {

public static void bubbleSort(float a[]) {

public static void selectionSort(float a[]) {

}
public static void InsertionSort(float a[]) {

public static void timeToRun(float a[]) {

public class main_sort {

public static void main(String[] args) throws IOException {

algorithm al = new algorithm();

Scanner sc = new Scanner(System.in);

int choice;

while (true) {

System.out.println("\n -----------Menu-------------");

System.out.println("| 1. Input |");

System.out.println("| 2. Output |");

System.out.println("| 3. Bublle Sort |");

System.out.println("| 4. Selection Sort |");

System.out.println("| 5. Insertion Sort |");

System.out.println("| 6. Time run three sort algorihtm |");

System.out.println("| 0. Exit |");

System.out.println(" ----------------------------- \n");

System.out.print(" Your selection (0 -> 6): ");

choice = sc.nextInt();
if (choice == 0) {

System.out.println(" Good bye, have a nice day!");

break;

switch (choice) {

case 1:

break;

case 2:

break;

case 3:

break;

case 4:

break;

case 5:

break;

case 6:

break;

default:

System.out.println("**Invalid choice. Try again.**");

sc.close();

}
*** My code:

public class algorithm {

public static void writeFlie(float a[]) throws IOException {

try {

OutputStream os = new FileOutputStream("D:\\Study\\FUNiX\\CC3\\input.txt");

DataOutputStream dos = new DataOutputStream(os);

for (int x = 0; x < a.length; x++) {

dos.writeFloat(a[x]); // writes the bytes

os.close();

dos.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

public static int[] readFile(float a[], int size) {

public static void bubbleSort(float a[]) {

public static void selectionSort(float a[]) {


}

public static void InsertionSort(float a[]) {

public static void timeToRun(float a[]) {

public class main_sort {

public static void main(String[] args) throws IOException {

algorithm al = new algorithm();

Scanner sc = new Scanner(System.in);

int choice;

while (true) {

System.out.println("\n -----------Menu-------------");

System.out.println("| 1. Input |");

System.out.println("| 2. Output |");

System.out.println("| 3. Bublle Sort |");

System.out.println("| 4. Selection Sort |");

System.out.println("| 5. Insertion Sort |");

System.out.println("| 6. Time run three sort algorihtm |");

System.out.println("| 0. Exit |");

System.out.println(" ----------------------------- \n");


System.out.print(" Your selection (0 -> 6): ");

choice = sc.nextInt();

if (choice == 0) {

System.out.println(" Good bye, have a nice day!");

break;

switch (choice) {

case 1:

System.out.print(" Enter number of elements:");

int n = sc.nextInt();

float arr[] = new float[n];

if (n > 20) {

System.out.println(" Number of elements is less than or equal to 20. Please try


again.");

} else {

System.out.println(" Enter elements: ");

for(int i = 0; i < n; i++) {

arr[i] = sc.nextFloat();

al.writeFlie(arr);

break;

case 2:

break;

case 3:

break;
case 4:

break;

case 5:

break;

case 6:

break;

default:

System.out.println("**Invalid choice. Try again.**");

sc.close();

***Please help me complete this. Thank u!!!!

You might also like