Question

In: Computer Science

Create a program called BubleSort.java that implements the Bubble Sort algorithm (The Art of Computer Programming...

Create a program called BubleSort.java that implements the Bubble Sort algorithm (The Art of Computer Programming - Donald Knuth). The algorithm is as follows: The program should be able to do the following: accepts one command line parameter. The parameter specifies the path to a text file containing the integers to be sorted. The structure of the file is as follows: There will be multiple lines in the file (number of lines unknown). Each line will contain multiple integers, separated by a single whitespace. reads the integers from the text file in part a into an array of integers. sort the integers in ascending order, and then prints out a sorted version of these integers, one per line. The implementation should follow the given the pseudo code/algorithm description. JAVA CODE

Solutions

Expert Solution

Program:

import java.util.*;
import java.io.*;

//class BubbleSort
public class BubbleSort
{  
   //method to perform bubble Sort
   public static void bubbleSort(int arr[], int n)
{
   while(true)
   {
       int t = 0;
        for(int j=0; j<n-1; j++)
        {
            if(arr[j]>arr[j+1])
            {
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
                t = j+1;
            }
        }
        if(t==0) break;
   }
}
  
//main method
public static void main(String args[]) throws IOException
{
   File file = new File(args[0]);
  
   //create object of Scanner class
       Scanner in = new Scanner(file);
      
       //declare the array
       int a[] = new int[1000];
      
       int n = 0;
       while(in.hasNext())
       {
           a[n] = in.nextInt();
           n++;
       }
                 
bubbleSort(a, n);
     
System.out.println("Sorted List: ");
for(int i=0; i<n; i++)
{
   System.out.println(a[i]);
}

}
}

num.txt

15 36 62 16 43
29 43 9 33 5
44 78 21 8 29

Output:

java BubbleSort num.txt

Sorted List:
5
8
9
15
16
21
29
29
33
36
43
43
44
62
78

N.B. Whether you face problem then share with me in the comment section, I'll help you. I expect positive feedback from your end.


Related Solutions

Create a program called BubleSort.java that implements the Bubble Sort algorithm (The Art of Computer Programming...
Create a program called BubleSort.java that implements the Bubble Sort algorithm (The Art of Computer Programming - Donald Knuth). The algorithm is as follows: The program should be able to do the following: accepts one command line parameter. The parameter specifies the path to a text file containing the integers to be sorted. The structure of the file is as follows: There will be multiple lines in the file (number of lines unknown). Each line will contain multiple integers, separated...
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that...
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that sorts the array below in ascending order.  LISP is a recursive language so the program will use recursion to sort. Since there will be no loops, you will not need the variables i, j, and temp, but still use the variable name array for the array to be sorted.             Array to be sorted is 34, 56, 4, 10, 77, 51, 93, 30, 5, 52 The...
Create a program called DualPivotQuickSort.java that implements the QuickSort algorithm using Yaroslavskiy’s algorithm. The program should...
Create a program called DualPivotQuickSort.java that implements the QuickSort algorithm using Yaroslavskiy’s algorithm. The program should be able to do the following: accepts one command line parameter. The parameter specifies the path to a text file containing the integers to be sorted. The structure of the file is as follows: There will be multiple lines in the file (number of lines unknown). Each line will contain multiple integers, separated by a single whitespace. reads the integers from the text file...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm. Create a second Java Application that implements an Insertion sort algorithm to sort the same array. Again, output the array in its original order, then output the array after it has been sorted...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm.
Write a program and test a program that translates the following Bubble Sort algorithm to a...
Write a program and test a program that translates the following Bubble Sort algorithm to a bubblesort function. The function's prototype is, void bubblesort(int a[], int size); Bubble Sort The inner loop moves the largest element in the unsorted part of the array to the last position of the unsorted part of the array; the outer loop moves the last position of the unsorted part of the array. The Bubble sort exchanges elements with adjacent elements as it moves the...
please write a C program that implements Quick Sort algorithm.
please write a C program that implements Quick Sort algorithm.
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
compare the time efficiency of the insertion sort algorithm with the bubble sort algorithm. Give the...
compare the time efficiency of the insertion sort algorithm with the bubble sort algorithm. Give the big theta notation of each of the algorithms as a part of your answer.
Write an MPI program that implements the hypercube bitonic sort algorithm. The program should be tested...
Write an MPI program that implements the hypercube bitonic sort algorithm. The program should be tested using a sequence of 64 integers (from 1 to 64, initially not sorted) and 8 processes. The number of elements assigned to each process is 8.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT