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.

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];
//read data from file
       int n = 0;
       while(in.hasNext())
       {
           a[n] = in.nextInt();
           n++;
       }
                 
bubbleSort(a, n);
//print the sorted list
System.out.println("Sorted List: ");
for(int i=0; i<n; i++)
{
   System.out.println(a[i]);
}

}
}

input.txt

25 37 52 26 34
92 43 99 37 59
40 79 22 81 25
29 37 52 28 35

Output:

java BubbleSort input.txt

Sorted List:
22
25
25
26
28
29
34
35
37
37
37
40
43
52
52
59
79
81
92
99


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.
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.
The programming language is C. In its simplest algorithm, the bubble sort technique compares each two...
The programming language is C. In its simplest algorithm, the bubble sort technique compares each two adjacent elements of an array of size “N” and exchanges their values if they are out of order. The process of comparing and exchanging is repeated for N array passes. For sorting array elements in ascending order, the smaller values “bubble” to the top of the array (toward element 0), while the larger values sink to the bottom of the array. Assuming that: double...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT