Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

private void two-way-merge () {// pseudo code // merge two sorted files File01.txt // and File02.txt into File03.txt // assume integer input Scanner F1 =

private void two-way-merge () {// pseudo code // merge two sorted files File01.txt // and File02.txt into File03.txt // assume integer input Scanner F1 = new Scanner(new File(File01.txt)); Scanner F2 = new Scanner(new File(File02.txt)); PrintWriter F3 = new PrintWriter(new File(File03.txt)); // read an input from file F1 x = F1.nextInt; // read an input from file F2 y = F2. nextInt; while (F1.hasNext() && F2.hasNext()) { if (x < y ) { F3.print(x + " "); //read an input from file F1 x = F1.nextInt; } else {

F3.print(y + " "); //read an input from file F2 y = F2.nextInt; } } // endwhile while (F1.hasNext()) { F3.print(x + " "); //read an input from file F1 x = F1.nextInt; } // endwhile while (F2.hasNext()) { F3.print(y + " "); //read an input from file F2 y= F2.nextInt; } // endwhile // close all files F1.close(); F2.close(); F2.close(); } // end two way-merge

private int replacementselectionsort () {// // open input file inf Scanner inf = new Scanner(System.in); // read array size M for min heap array M = inf.nextInt(); // allocate array space for min heap // Note min heap starts from position 1 minheap = new int[M+1];// // fill intial minheap from infile for (j = 1; j M && inf.hasNext()); j++) // read next input from infile minheap [j] = inf.nextInt ()); // endfor // no. of elements in heap heapsize = --j; // No. of data in minheap // output file no. i = 1; // outer while while (heapsize > 0) { // while more data exist // create output file i fname = "File0" + i +".txt"; // open output file[i] PrintWriter opf = new PrintWriter(new File(fname)); //convert to minheap for (j = heapsize/2; j > 0; j--) minheapdn (j, heapsize); // Initialize last written lastwritten = -; //Integer.MIN_VALUE in java inheap = 0; // unprocessed elements // write minheap[1] to output file[i] and read next data // if not end of file read next input // else shift minheap element to left and reduce heap size // inner while while ( heapsize > 0) { // root can be written to current output file[i] if (minheap[1] < lastwritten) { swap (minheap[1], minheap[heapsize]); // heapsize --; // reduce heap size //increase unprocessed elements in heap inheap ++; else { lastwritten = minheap[1]; // write min to output file[i] opf.print(arr[j] + " "); // if end of file shift elements to left in minheap if (!inf.hasNext()){ // for ( j = 1; j < heapsize+inheap; j ++) minheap[j] = minheap[j+1]; // end for heapsize --; // reduce heap size }// end if else { // read next input from infile minheap [1] = inf.nextInt()); }// end inner else

}//end outer else // heap down from root; minheapdn(1, heapsize); } // end inner while opf.close(); // close this file since it no more can be added if (inheap = = 0) break; // all data are processed heapsize = inheap; // i ++; // start another output file } // end outer while inf.close (); // close input file return i; // No. of files } // end replacementselectionsort

implement external sort using the 2 way merge sort and replacement selection code above. java

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

Understanding Oracle APEX 5 Application Development

Authors: Edward Sciore

2nd Edition

1484209893, 9781484209899

Students also viewed these Databases questions