## student student Interview Question

Students Students**Country:**India

**Interview Type:**In-Person

External sorting algorithm, which sorts chunks that each fit in RAM, then merges the sorted chunks together. For example, for sorting 900 megabytes of data using only 100 megabytes of RAM:

Read 100 MB of the data in main memory and sort by some conventional method, like quicksort.

Write the sorted data to disk.

Repeat steps 1 and 2 until all of the data is in sorted 100 MB chunks (there are 900MB / 100MB = 9 chunks), which now need to be merged into one single output file.

Read the first 10 MB (= 100MB / (9 chunks + 1)) of each sorted chunk into input buffers in main memory and allocate the remaining 10 MB for an output buffer. (In practice, it might provide better performance to make the output buffer larger and the input buffers slightly smaller.)

Perform a 9-way merge and store the result in the output buffer. If the output buffer is full, write it to the final sorted file, and empty it. If any of the 9 input buffers gets empty, fill it with the next 10 MB of its associated 100 MB sorted chunk until no more data from the chunk is available. This is the key step that makes external merge sort work externally -- because the merge algorithm only makes one pass sequentially through each of the chunks, each chunk does not have to be loaded completely; rather, sequential parts of the chunk can be loaded as needed.

courtesy :- wikipedia (en.wikipedia.org/wiki/External_sorting )

```
public static int getKthSmallest(int[] input,int k)
{
// post condition: This function returns kth smallest element
// Algorithm: selection algorithm:
// complexity: O(N)
// Assumption: All values in input are distinct.However, you can change this function a
// little bit to accommodate with non-distinct values
// Notes: I ignore the memory limitation. You can also optimize that in this function.
int pivotValue=input[(input.length-1)/2];
int [] rearrangement=new int[input.length];
int smaller=0,bigger=input.length-1;
for(int i=0;i<input.length;i++)
{
if(i!=((input.length-1)/2))
{
if(input[i]<pivotValue)
{
rearrangement[smaller]=input[i];
smaller++;
}
else if(input[i]>pivotValue)
{
rearrangement[bigger]=input[i];
bigger--;
}
}
}
if(smaller==k-1)
{
return pivotValue;
}
else if(smaller<k-1)
{
int [] nextInput=new int[input.length-smaller-1];
for(int i=0;i<input.length-smaller-1;i++)
{
nextInput[i]=rearrangement[i+smaller+1];
}
return getKthSmallest(nextInput,k-smaller-1);
}
else
{
int [] nextInput=new int[smaller];
for(int i=0;i<smaller;i++)
{
nextInput[i]=rearrangement[i];
}
return getKthSmallest(nextInput,k);
}
}
public static void printFirstKthSmallestElements(int [] input,int k)
{
int KthValue=getKthSmallest(input,k);
for(int i=0;i<input.length;i++)
{
if(input[i]<KthValue)
System.out.print(input[i]+",");
}
System.out.print(KthValue);
}
```

Heap will work perfectly if the K is a constant. If k is equal to half size of the array.The time complexity will be NlogN

If k is small, you can use a heap (which I suppose fits in the memory).

- Anonymous August 05, 2012