Question
Complete insertsort and searchsort methods. Based on process method, create input and save it in input.txt file. import java.util.*; import java.io.*; public class Sortingpractice{ //
Complete insertsort and searchsort methods. Based on process method, create input and save it in input.txt file.
import java.util.*;
import java.io.*;
public class Sortingpractice{
// class Variables
private int n, count = 0;
String arr[];
//use a short word for prt to save typing
PrintStream prt = System.out;
//insert x in a sorted list if list is not full
public int insertsort(String x){
int p, j; prt.printf(" \t\tInsert %s in a sorted list:", x);
if (count == n) return 0;//list is full
// complete the rest return 1;
}
//sequential search for x in a sorted list
// if successful return position of x in the
// sorted list otherwise return -1;
public int searchsort(String x){
prt.printf(" \t\tSearch for %s in a sorted list:", x);
if (count == 0) return -1; //list is empty
// complete the rest return -1;
} // end searchsort
//delete x from a sorted list
public int deletesort(String x){
prt.printf(" \t\tDelete %s from a sorted list:", x); if (count == 0) return 0;//list is empty int i, p = searchsort(x);
if (p == -1) return 0; //x is not found
//Shift to left array from position p
for (i = p ; i < count-1 ; i++)
arr[i] = arr[i+1];
// end for count--; return 1; //successful deletion
} // end delete
// print list elements formatted
public void printlist(){
int i; prt.print(" \tList contents: ");
for (i = 0; i < count; i++) prt.printf("%s, ", arr[i]);
// enf for } // end printlist
// insert, delete and search in the list
private void process(){
int j, m, k, p, s; String x; prt.print("\tArray implementation of sorted list. This program first reads:n array size," +
" \tNo. of elements to insert, followed by elements to insert," + " \tNo. of elements to search, followed by elements to search," + " \tNo. of elements to delete, followed by elements to delete" + " \t\tTo compile: javac Sortingpractice.java" +
" \t\tTo execute: java Sortingpractice < any data file");
// open input file Scanner inf = new Scanner(System.in); try{
//read array size n = inf.nextInt(); //read no. of elements to insert m = inf.nextInt();
// Allocate Space for array arr = new String[n];// prt.printf(" \tCreating array of size %4d:",n);
prt.printf(" \tInsert %2d elements in the sorted list.", m); for(j = 1; j <= m; j++){
x = inf.next(); //read x to insert
s = insertsort(x); //insert x if (s == 1)
prt.printf(" Successful insertion.");
else
prt.printf(" Failed insertion.");
}// end for printlist(); //print linked list elements
//read no. of elements to search in list m = inf.nextInt(); prt.printf(" \tSearch for %d elements in sorted list.", m);
for(j = 1; j <= m; j++){
x = inf.next(); // read x to serach
s = searchsort(x); //delete x
if (s >= 0)
prt.printf(" found at position %d", s);
else
prt.printf(" not found.");
}// end for
//read no. of positions to delete from list m = inf.nextInt(); prt.printf(" \tDelete %d elements from sorted list.", m);
for(j = 1; j <= m; j++){
x = inf.next(); // read x to delete
s = deletesort(x); //delete x if (s == 1)
prt.printf(" Successful deletion");
else
prt.printf(" Failed deletion.");
} // end for printlist(); //print list elements
// close input file
inf.close();
} catch (Exception e){ prt.print(" Exception " + e + " ");}
} // end process
// main method public static void main(String args[]) throws Exception{
Sortingpractice lst = new Sortingpractice();
lst.process();
System.out.printf(" \tmade: " + java.time.LocalDate.now());
} // end main } // end class Sortingpractice
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started