0% found this document useful (0 votes)
3 views

implement external sorting 12

The document contains a Java program that implements external sorting for large datasets that cannot fit into memory. It reads data from a file in chunks, sorts each chunk, and writes the sorted chunks to temporary files before merging them into a final sorted output file. The program also includes a method to generate random input data for testing the sorting functionality.

Uploaded by

devbratc8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

implement external sorting 12

The document contains a Java program that implements external sorting for large datasets that cannot fit into memory. It reads data from a file in chunks, sorts each chunk, and writes the sorted chunks to temporary files before merging them into a final sorted output file. The program also includes a method to generate random input data for testing the sorting functionality.

Uploaded by

devbratc8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.io.

BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Random;

public class ExternalSorting {

static int N = 10; // Size of the file in disk


static int M = 50; // Max items the memory buffer can hold

public static void externalSort(String fileName) {


String tfile = "temp-file-";
int[] buffer = new int[M < N ? M : N];

try {
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
int slices = (int) Math.ceil((double) N / M);

int i, j;
i = j = 0;

// Iterate through the elements in the file


for (i = 0; i < slices; i++) {
// Read M-element chunk at a time from the file
for (j = 0; j < (M < N ? M : N); j++) {
String t = br.readLine();
if (t != null)
buffer[j] = Integer.parseInt(t);
else
break;
}
// Sort M elements
Arrays.sort(buffer, 0, j);

// Write the sorted numbers to temp file


FileWriter fw = new FileWriter(tfile + Integer.toString(i) +
".txt");
PrintWriter pw = new PrintWriter(fw);
for (int k = 0; k < j; k++)
pw.println(buffer[k]);
pw.close();
fw.close();
}
br.close();
fr.close();

// Now open each file and merge them, then write back to disk
int[] topNums = new int[slices];
BufferedReader[] brs = new BufferedReader[slices];
for (i = 0; i < slices; i++) {
brs[i] = new BufferedReader(new FileReader(tfile +
Integer.toString(i) + ".txt"));

String t = brs[i].readLine();
if (t != null)
topNums[i] = Integer.parseInt(t);
else
topNums[i] = Integer.MAX_VALUE;
}

// Output file path corrected to just a file name


FileWriter fw = new FileWriter("External-Sorted.txt");
PrintWriter pw = new PrintWriter(fw);
for (i = 0; i < N; i++) {
int min = topNums[0];
int minFile = 0;
for (j = 0; j < slices; j++) {
if (min > topNums[j]) {
min = topNums[j];
minFile = j;
}
}
pw.println(min);
String t = brs[minFile].readLine();
if (t != null)
topNums[minFile] = Integer.parseInt(t);
else
topNums[minFile] = Integer.MAX_VALUE;
}

for (i = 0; i < slices; i++)


brs[i].close();

pw.close();
fw.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

static String generateInput(int n) {


String fileName = "external-sort.txt";
Random rand = new Random();

try {
FileWriter fw = new FileWriter(fileName);
PrintWriter pw = new PrintWriter(fw);

for (int i = 0; i < n; i++)


pw.println(rand.nextInt(20)); // generating random integers between
0 and 19
pw.close();

} catch (IOException e) {
e.printStackTrace();
}
return fileName;
}

public static void main(String[] args) {


String fileName = generateInput(N);
externalSort(fileName);
}
}

You might also like