Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA code No package Do not modify main method, except writing your name in line 83 and OPTIONAL part). Implement external sort: -for sort phase

JAVA code

No package

Do not modify main method, except writing your name in line 83 and OPTIONAL part).

Implement external sort:

-for sort phase use normal sort,

-for merge phase use two way merge to merge n sorted files (merge2way(n)),

-for array sort use heapsort.

Also write merge(f1, f2, f3) to merge two sorted files f1 and f2 into f3

  1. Write mergenway(n) method and print execution time of both merges for initial input file over 10MB data.
  2. A sample input is as follow:Note:Fist input is max array size for sort

10 84 82 52 80 96 85 75 75 82 87 92 89 57 94 93 92 63 99 87

72 73 56 74 50 84 62 72 55 86 75 74 100 83 60 53 68 89 67 66

65 72 94 73 54 98 96 85 75 75 82 87 92 89

-------------------------------------------------------------------------------------------------------------------------------

1

2 import java.util.*;

3 import java.io.*;

4 // CSUN COMP 282 Sp 21, Homework-2

5 // Implementing external sort:

6 // For sort phase using normal sort and

7 // for merge phase using 2way merge.

8 // Author:

9 public class xxxxxH2{

10 // class used for search

11 int heap[], M;//M is largest array size

12

13 //use a short word for System.out to save typing

14 PrintStream prt = System.out;

15

16 // print file formatted k integers per line

17 private void prtfile(String fn, int k){

18 //declare variables

19 int i=0, x;

20 prt.printf(" \t%s:", fn);

21 try{

22 Scanner inf = new Scanner(new File(fn));

23 while (inf.hasNext()) {

24 //read an input from fname

25 x = inf.nextInt();

26 prt.printf("%3d ", x);

27 i++;

28 if(i % k == 0) prt.printf(" \t");

29 } // endwhile

30 // close file

31 inf.close();

32 } catch (Exception e){

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

34 } // end prtfile

35

36 // print n files

37 private void prtfiles(int n, int k){

38 int i;

39 String fname ;

40 for (i = 1; i <= n; i++){

41 fname = "F" +i+".txt";

42 prtfile(fname, k);

43 }

44 } // end prtfiles

45

46 // normalsort, creating arrays of size n

47 private int normalsort(){

48 // complete this method

49 } // end normalsort

50

51 //2way merge n sorted files

52 private int merge2way(int n){

53 // complete this method

54 } // end merge2way

55

56 //OPTIONAL nway merge n sorted files

57 //private int mergenway(int n){

58 // complete this method

59 //} // end mergenway

60

61 //merge 2 sorted files f1, f2 into f3

62 private int merge(String f1, String f2, String f3){

63 // complete this method

64 } // end merge

65

66 //Heapsort heap[] with n integers

67 private void heapsort(int n){

68 //complete this method

69 } // end sort

70

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

72 int n, k = 15; // print 15 integers per line

73 xxxxxH2 srt = new xxxxxH2();

74

75 n = srt.normalsort(); // n is no. of sorted files created

76 srt.prtfiles(n, k);

77

78 System.out.printf(" Overall %4d sorted files are created", n);

79 //srt.merge2way(n); // Merging n sorted files using 2-way merge

80 //OPTIONAL srt.mergenway(n); Merging n sorted files using n-way merge

81 //OPTIONAL Print execution time of both merges

82 //MAKE SURE TO WRITE YOUR NAME IN NEXT LINE

83 System.out.printf(" \tAuthor: xxxx Date: " +

84 java.time.LocalDate.now());

85 } // end main

86 } // end xxxxxH2

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_2

Step: 3

blur-text-image_3

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

More Books

Students also viewed these Databases questions

Question

Question What is the advantage of a voluntary DBO plan?

Answered: 1 week ago