Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// Program to implementing following internal sorting algorithms: // bubble sort, insertion sort, selection sort, // merge sort and quick sort // Assume the input

// Program to implementing following internal sorting algorithms:

// bubble sort, insertion sort, selection sort,

// merge sort and quick sort

// Assume the input is in file sort.txt.

// Prepared by: Name:

// Date:

import java.io.*;

import java.util.*;

public class pro1{

private int n, arr[];

PrintStream prt = System.out;

// read input method

private void readarr(){

int j;

try{

Scanner inf = new Scanner(new File("sort.txt");

// read No. of integer data to sort

n = inf.nextInt();

// Allocate space for storing n integers

arr = new int[n];

// Read k integer numbers from input

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

arr[j] = inf.nextInt();

// end for

inf.close();

} catch (Exception e) {

prt.printf("Exception " + e + " ");}

} // end readarr method

// formatted printarr method that prints 10 integers per line

private void printarr(String s){

int j;

prt.print(s);

// print n input, 10 integer per line

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

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

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

prt.print(" ");

} // end for

} // end printarr method

// partition merge (i, j) partition arr[i] thru arr[j] , assume pivot = arr[i]

private int partition (int i, int j){

} // end partition

// merge (i, j, k) arr[i] thru j and arr[j+1] thru arr[k] is sorted,

// merge the 2 sorted sub arrays

private void merge(int i, int j, int k){

} // end merge

// swap arr[i] & arr[j]

private void swap(int i, int j){

} // end swap

// bubble sort method

private void bubblesort(){

} // end bubble sort method

// insertion sort method

private void insertionsort(){

} // end insertion sort method

// selection sort method

private void selection(){

} // end selection sort method

// recursive mergesort method

private void mergesort(int i, int j){

} // end recursive mergesort method

// recursive quicksort method

private void quicksort(int i, int j){

} // end recursive quicksort method

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

System.out.print(" Program to implem internal sorting algorithms "

+ " Prepared by Last: First: "

+ " Student ID: "

+ " Date: 02/16/2020");

pro1 srt = new pro1();

// read input

srt.readarr();

// print array before sorting

srt.printarr(" Input befor sorting is as follow: ");

// perform bubble sort

srt.bubblesort();

// print sorted array after bubble sort

srt.printarr(" Input after bubble sort is as follow: ");

// read input

srt.readarr();

// perform bubble sort

srt.insertionsort();

// print sorted array after insertion sort

srt.printarr(" Input after insertion sort is as follow: ");

// read input

srt.readarr();

// perform selection sort

srt.selectionsort();

// print sorted array after selection sort

srt.printarr(" Input after selection sort is as follow: ");

// read input

srt.readarr();

// perform recursive mergesort

srt.mergesort(0, srt.n - 1);

// print sorted array after recursive merge sort

srt.printarr(" Input after recursive merge sort is as follow: ");

// read input

srt.readarr();

// perform recursive quicksort

srt.quicksort(0, srt.n - 1);

// print sorted array after recursive quick sort

srt.printarr(" Input after recursive quick sort is as follow: ");

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

Students also viewed these Databases questions