viewed in the previous section. After this phase, we getthe external file simultaneously in the position of Block-N-1 in the external file until the block is full. So, half ofthe records in the memory array will be sorted by merg-ing and written in the position of Block-N-1 in the exter-nal file. The remaining records in the lower half (if any)are copied into the upper half of memory array. Now theupper half of memory array contains the highest recordsof Block-N and Block-N-1. Then, again Merge Sort is ap-plied to sort the records in the upper half of memory ar-ray. The additional space required for Merge Sort is thelower half of memory array. After this, Block-N-2 Thenext iteration starts with Block-N-2 and Block-N-1 to beread into the lower half and upper half of memory arrayrespectively. At the end of this iteration, upper half isread into lower half of memory array. The Merging andMerge Sort terminates when Block-B is read into the low-er half of memory array and processed accordingly. Afterthis iteration the upper half of the memory array containsthe highest sorted records and they are written in the po-sition of Block-N in the external file.
2.3 In Place Merging Algorithm
The algorithm works in two phases. In the firstphase, the algorithm works as the algorithm proposed byDufrene and Lin that is, Block_1 and Block_ S are readinto lower half and upper half of the memory array, re-spectively,and they are sorted using Quick sort. Thisphase terminates when Block_ 2 is read into the upperhalf of the memory array and sorted with the remainingrecords in the lower half of the memory array. Thus weget sorted runs. After this phase, the lower half of thememory array contains the lowest sorted records of theentire file.Then, the algorithm switches to its secondphase, whereby the sorting process continues consideringthe following two cases:Case 1: Here the required blocks are read and ifthe last record of the lower half of the memory array issmaller than the first record of the upper half of thememory array, then it is not required to sort the recordsof the memory array and then the next block will be readfor further approach.Case 2: This is the general case. The in-placemerging technique is used here. In the second phase,Block_ S
−1 and Block_ S are read into the lower and u
p-per halves of the memory array respectively. For Case 1the blocks are not required to write back in the externalfile. In Case 2, after applying the in-place merging, theupper half of the memory array contains the highest or-dered records of Block_ S and
Block_ S −1 and the lower half is sent back to its
corresponding position in the external file (Fig. 6). After
this, Block_ S − 2 is read into the lower half of the mem
o-ry array and checked for the conditions specified in Case1 or Case 2. In this way, when Block_ 2 has beenprocessed, the upper half of the memory array containsthe highest sorted records of the entire file and they arewritten in the position of Block_ S in the external file forCase 2. The next iteration starts with Block_ S
− 2 andBlock_ S −1 to be read into the lower and upper halves of
the memory array respectively. At the end of this itera-tion, the upper half of the memory array contains thehighest sorted records among the blocks i.e. Block_ 2 ,Block_ 3 , . . . , Block_
S −1 and they are written in the p
sition of Block_ S −1 in the external file for Case 2. After
each pass, the size of the external file is decreased by oneblock. The last two blocks to be processed are Block_ 2and Block_ 3 , which upon completion, the entire file issorted.
2.3.1 Algorithm: An external sorting algorithmusing in-place merging no additional disk space
Declare the blocks in external file to behalf of memory array. Let the blocks be
Block_1, Block_2, …,Block_ S −1 , Block_
If there is only one block in the externalfile then quicksort the entire memory ar-ray3.
Read Block_1 into the lower half ofmemory array. Set T = S //Begins firstphase4.
Read Block_T into upper half of memoryarray5.
Sort the entire memory array usingquicksort6.
Write upper half of memory array toBlock_T area of external file7.
Decrement Block_ T by one block8.
Repeat from step 4 if Block_T is notequal to Block_19.
Write lower half of memory array toBlock_1 area of external file10.
Set P = S //Begins second phase11.
Read Block_ P into the upper half of
memory array and set Q = P −1
Read Block_Q into the lower half ofmemory array13.
If last element of the lower half is greaterthan first element of the upper half thensort (merge) the memory array using in-place merging and write lower half ofmemory array to Block_Q area of exter-nal file14.
Decrement Block_Q by one block15.
Repeat from step 12 if Block_Q ≠
JOURNAL OF COMPUTING, VOLUME 3, ISSUE 8, AUGUST 2011, ISSN 2151-9617HTTPS://SITES.GOOGLE.COM/SITE/JOURNALOFCOMPUTING/WW.JOURNALOFCOMPUTING.ORG85© 2011 Journal of Computing Press, NY, USA, ISSN 2151-9617