Professional Documents
Culture Documents
Co-Sequential Processing
Co-Sequential Processing
Co-Sequential Processing
Example : Sort the following data given that memory can’t hold except 4 numbers only:
• 0,19,6,4,2,16,8,3
0,19,6,4,2,16,8,3
• Run/file 1 : 0,9,6,4
• Run/file 2: 2, 16,8,3
Internal Sorting:
• Run 1 : 0,4,6,9
• Run 2: 2, 3,8,16
• Run 1: 0,4,6,9
• Run 2: 2, 3,8,16
Merging:
0,20
4,2 0,2
4,3 0,2,3
4,8 0,2,3,4
6,8 0,2,3,4,6
9,8 0,2,3,4,6,8
9,16 0,2,3,4,6,8,9
-, 16 0,2,3,4,6,8,9,16
-,- 0,2,3,4,6,8,9,16
3,4,6,2,9,4,8,7,5,6,3,1,2 Input file
2,3
4,4 1,2 PASS 2
6,7 3,5
8,9 6
1,2
PASS 3
2,3
3,4
4,5
6,6
7,8
9
Selecting The Minimum value From a set of
Run Values using Selection Tree
• Advantages:
• It can be applied to files of any size.
• Reading of the input during the run-creation step is sequential Not much
seeking.
Heap Sort
Sort On Memory
Heap Sort
Heap Sort
• The Second stage occur virtually as well at the same time we write
sorted numbers to output file.
• So, All the sort process happens while we are reading and writing the
data to file which will reduce time needed to sort the data on file.
• Sorting time and reading/writing times has been merged into one since the
sorting and reading/writing will happen in parallel fashion rather than
sequential fashion
Heap Sort Example
How To Improve K-Way Merge Sort
• Increasing the amount of memory: helps make the buffers larger and
thus reduce the numbers of seeks.
• Increasing the Number of Dedicated Disk Drives: If we had one separate
read/write head for every run, then no time would be wasted seeking.
• Increasing the Number of I/O Channels: With a single I/O Channel, no
two transmission can occur at the same time. But if there is a separate
I/O Channel for each disk drive, then I/O can overlap completely.
But what if hardware based improvements are not possible Increase Run
Size By Replacement Selection
How To Improve K-Way Merge Sort
• Replacement Selection Procedure:
1. Read a collection of records and sort them using heapsort. The resulting heap is called
the primary heap.
2. Instead of writing the entire primary heap in sorted order, write only the record whose
key has the lowest value.
3. Bring in a new record and compare the values of its key with that of the key that has just
been output.
4. If the new key value is higher, insert the new record into its proper place in the primary
heap along with the other records that are being selected for output.
5. If the new record’s key value is lower, place the record in a secondary heap of records
with key values smaller than those already written.
6. Repeat Step 3 as long as there are records left in the primary heap and there are records
to be read. When the primary heap is empty, make the secondary heap into the primary
heap and repeat steps 2 and 3.
How To Improve
K-Way Merge Sort