Recursive Sorting - PowerPoint PPT Presentation

About This Presentation
Title:

Recursive Sorting

Description:

If all iterative algorithms require On2 operations then halving the number of ... rules (i.e. aces high with suit precendence: clubs, diamonds, hearts, spades) ... – PowerPoint PPT presentation

Number of Views:10
Avg rating:3.0/5.0
Slides: 15
Provided by: richar219
Category:

less

Transcript and Presenter's Notes

Title: Recursive Sorting


1
Recursive Sorting
  • Why is recursive sorting faster ?
  • Merge Sort
  • Quick Sort Description
  • Quick Sort Pseudocode
  • Choice of Quick Sort pivot record
  • The Quick Sort library function.

2
Why is recursive sorting faster ?
  • 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.

3
Merge Sort Illustration
  • 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.

4
Merge Sort Pseudocode
  • 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_index1, end_index)?
  • // merge lower and upper half blocks into sorted
    block
  • merge(array_address,low_index,mid_index,high_index
    )?

5
Merge 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

6
Quick Sort 1
  • 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 (X1)th record.

7
Quick Sort 2
  • 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 (X1)th pivot record
    (now in its final sorted position). The size of
    this part is 1.
  • c. The top part being from the (X2)th record to
    the last record inclusive.

8
Quick Sort 3
  • 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 (X1)th position.

9
Quick Sort 4
  • 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.

10
Quick Sort 5
  • 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 (X1)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.

11
Quicksort Algorithm 1
  • 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
  • X0
  • FOR all records from start 1 to end
  • IF record higher than start
  • XX1
  • Swap start and (X1)th records

12
Quicksort Algorithm 2
  • // swap records in wrong part for each other
  • bot_indexstart
  • top_indexend
  • DO
  • WHILE record at top_index in correct part
  • and top_index gt X1
  • top_index top_index-1
  • WHILE record at bot_index in correct part
  • and bot_index lt X1
  • bot_index bot_index 1
  • IF top_index gt X1 and bot_index lt X1
  • swap records at top_index and bot_index
  • WHILE bot_index ! X1 AND top_index ! X1
  • // sort bottom and top parts
  • sort(array,start,X)?
  • sort(array,X2,end)?

13
Choice of quicksort pivot record
  • 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..

14
Library quicksort function
  • The stdlib.h library header contains the
    prototype for a generalised quicksort function
    qsort().
  • include ltstdlib.hgt
  • 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.
Write a Comment
User Comments (0)
About PowerShow.com