Question: Add bubble sort, insertion sort and selection sort methods to the program without modifying the constructor and any methods and Modify each sort method that

Add bubble sort, insertion sort and selection sort methods to the program without modifying the constructor and any methods and Modify each sort method that computes and prints no. of comparison and swaps after sorting. add this statement at the end of each sorting method:

prt.printf(" \tComparisons = %d, Swaps = %d", ncomps, nswaps);

CODE:

import java.io.*;

import java.util.*;

public class swap2{

int arr[], barr[], n, k;

int nswap, ncomp;

PrintStream prt = System.out;

// class constructor

swap2 (){

int i;

prt.print("\tThis Java program implements bubble, insertion and selection sort \t" +

"algorithms and prints no. of comparisons and swaps. \t" +

"The program first reads n, k and next reads n integers and stores \t" +

"them in array barr[]. \tApplies bubble, insertion and selection sort to the same \t" +

"data and print no. of comparisons and swaps after sorting." +

" \t\tTo compile: javac swap2.java" +

" \t\tTo execute: java swap2 < any data file");

try{

// open input file inf

Scanner inf = new Scanner(System.in);

// read from input file

n = inf.nextInt();//read n, no. of data

k = inf.nextInt();//read k, how many per line

prt.printf(" \tn = %3d, k = %4d ", n, k);

//space for arr

arr = new int[n];

barr = new int[n];

//loop read n integers

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

barr[i] = inf.nextInt();

// close input file inf

inf.close();

} catch (Exception e){ prt.printf(" Ooops! Read Exception: %s", e);}

} // end constructor

//Method to print formatted array,

//k elements per line

private void printarr(){

int j;

for (j = 0; j < n ; j ++){

prt.printf("%4d ", arr[j]);

if ((j+1) % k == 0)

prt.printf(" \t");

} // end for

} // end printarr

//swap arr[j] and arr[k]

private void swap(int j, int k){

int tmp = arr[j];

arr[j] = arr[k];

arr[k]= tmp;

} // end swap

// main program

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

// Create an instance of swap2 class

swap2 h = new swap2();

//copy barr to arr for bubble sort,

h.arr = h.barr.clone();

System.out.printf(" \tInput before bubble sort \t");

h.printarr();

h.bubble();

System.out.printf(" \tInput after bubble sort \t");

h.printarr();

//copy barr to arr for insertion sort,

h.arr = h.barr.clone();

System.out.printf(" \tInput before insertion sort \t");

h.printarr();

h.insertion();

System.out.printf(" \tInput after insertion sort \t");

h.printarr();

//copy barr to arr for selection sort

h.arr = h.barr.clone();

System.out.printf(" \tInput before selection sort \t");

h.printarr();

h.selection();

System.out.printf(" \tInput after selection sort \t");

h.printarr();

System.out.printf(" \tDate: " +

java.time.LocalDate.now());

} // end main

} // end class swap2

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!