Download Presentation
## Recursive Sorting

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**Why is recursive sorting faster ?**Merge Sort Quick Sort Description Quick Sort Pseudocode Choice of Quick Sort pivot record The Quick Sort library function. Recursive Sorting**If all iterative algorithms require On2 operations then**halving the number of records to be sorted would require one quarter of the time. But we are not restricted to halving the set of records to be sorted once. We can halve n items log2n times (using integer division). Then if we have to perform n operations on each of log2n layers this algorithm requires nlog2n operations. Why is recursive sorting faster ?**Try this algorithm with a pack of cards. First decide the**sort order, e.g bridge rules (i.e. aces high with suit precendence: clubs, diamonds, hearts, spades). Then sort the pack by splitting it in 2, sorting each half and merging the 2 half packs into a sorted pack. When sorting half, quarter or smaller packs the same algorithm (approach) can be used. Of course a pack containing only 1 card is already sorted. Merge Sort Illustration**Parameters: address of array, starting and ending indexes.**IF start_index == end_index : return // Only 1 record so block is already sorted mid_index = (start_index + end_index)/2 // integer division, no fraction // sort lower half sort(array_address, start_index, mid_index) // sort upper half sort(array_address, mid_index+1, end_index) // merge lower and upper half blocks into sorted block merge(array_address,low_index,mid_index,high_index) Merge Sort Pseudocode**Parameters: address of array, starting, mid and ending**indexes. Declare static local array for merging into WHILE items exist to be merged from both halves: IF next item in lowerhalf lower than next item in upper half: copy item from lower half into local array ELSE: copy item from upper half into local array FOR all items not yet copied in either half: copy item into local array FOR all items in local array: copy item back into original block Merge Pseudocode**This algorithm is the fastest general-purpose sort known.**Faster sorts are possible for data whose key values make it is feasible to use a hash-table sort. The same approach (i.e. immediate return) is taken as by merge sort for record blocks sizes of 1 or 0 which are already sorted. Quick sort otherwise works by selecting an arbitrary pivot record (e.g. the first) and counting how many records in the block come below this value (let's call this number of records X). The pivot record is then swapped with the (X+1)th record. Quick Sort 1**This partitions the original block into 3:**a. The bottom part from the first to the Xth record inclusive. b. The middle part being the (X+1)th pivot record (now in its final sorted position). The size of this part is 1. c. The top part being from the (X+2)th record to the last record inclusive. Quick Sort 2**There will now be the same number of records in the bottom**part which will need to be moved into the top part as vice-versa. The next part of the algorithm involves swapping these records between bottom and top parts. This is achieved using 2 indices used to search through the top part (starting at the top record, going down by 1 each search) and the bottom part (starting at the bottom record going up by 1 each search), finding the next pair of records to swap into the correct parts, until either index gets to the middle record, the (X+1)th position. Quick Sort 3**All the records in the bottom part are now lower in the sort**order than the record in the middle part, which is lower than all the records in the top part of the original block. The sort algorithm is next applied recursively to the bottom part and to the top part. The middle record is correctly positioned so it doesn't need to be moved. Quick Sort 4**There is no reason why the bottom and top parts should be**equal in size. It is also as likely that one part will have zero size as any other possible size . Due to the arbitrary selection of the record to be swapped into the (X+1)th position, there will be a near enough to 50/50 split often enough on average to make this algorithm efficient. Also, even if occasionally the top or bottom part might have 0 records, this doesn't prevent this algorithm from resolving because the block is still split between the other part and the middle record. Quick Sort 5**parameters: address of array, indexes of bottom and top**records in part of array to be sorted. // handle trivial case IF 1 or fewer records: return // find correct position for start record: X=0 FOR all records from start +1 to end: IF record higher than start: X=X+1 Swap start and (X+1)th records Quicksort Algorithm 1**// swap records in wrong part for each other**bot_index=start top_index=end DO : WHILE record at top_index in correct part and top_index > X+1: top_index = top_index-1 WHILE record at bot_index in correct part and bot_index < X+1: bot_index = bot_index +1 IF top_index > X+1 and bot_index < X+1: swap records at top_index and bot_index WHILE bot_index != X+1 AND top_index != X+1; // sort bottom and top parts sort(array,start,X) sort(array,X+2,end) Quicksort Algorithm 2**In the simple form of quicksort described above, where an**arbitrary choice is made concerning the pivot record, performance can degrade if the data is already sorted or nearly sorted. In this situation the size of the start or end partition will always be zero, and quicksort will behave like selection sort, degrading to On2 comparisons. An optimisation is to perform a 3 record bubble sort between the first, middle and last records and then to use the record ending in the middle position as the pivot. This guarantees that neither partition size will be of zero size, and shortens the parts of the algorithm which positions the pivot and which swaps records between partitions.. Choice of quicksort pivot record**The stdlib.h library header contains the prototype for a**generalised quicksort function qsort(). #include <stdlib.h> void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); The parameters allow any kind of record and sort key: a. The array base address b. The number of records c. The record block size d. A pointer to a user supplied comparison function. Library quicksort function