Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beyond Big Data Using Social MDM To Drive Deep Customer Insight

Authors: Martin Oberhofer, Eberhard Hechler

1st Edition

0133509796, 9780133509793

More Books

Students also viewed these Databases questions

Question

2 What backs the money supply.

Answered: 1 week ago

Question

In an Excel Pivot Table, how is a Fact/Measure Column repeated?

Answered: 1 week ago