Sorting - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Sorting

Description:

Like looking up an entry in a phone directory. S.Preuitt CS160 Sorting & Searching ... So far we've seen repetition as a loop structure executed in a circular fashion ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 19
Provided by: Preu
Category:
Tags: sorting

less

Transcript and Presenter's Notes

Title: Sorting


1
Sorting Searching
  • "A computer can do tedious, and it can do
    time-consuming, but we probably don't want to let
    it."
  • -- rab
  • (found on the web)

2
Sorting and Searching
  • Sorting, as well as Searching, are fundamental
    operations in computers many programs will use
    it as an intermediate step
  • An algorithm is said to be correct if, for every
    input instance, it halts with the correct output
  • There is a direct correlation between the
    complexity of an algorithm and its relative
    efficiency.

3
Sorting and Searching
  • Algorithmic complexity is generally written in a
    form known as Big-O notation, where the O
    represents the complexity of the algorithm and a
    value n represents the size of the set the
    algorithm is run against.
  • For example, O(n) means that an algorithm has a
    linear complexity. In other words, it takes ten
    times longer to operate on a set of 100 items
    than it does on a set of 10 items (10 10
    100).
  • If the complexity was O(n2) (quadratic
    complexity), then it would take 100 times longer
    to operate on a set of 100 items than it does on
    a set of 10 items.

4
Sorting and Searching
  • The common sorting algorithms can be divided into
    two classes by the complexity of their
    algorithms
  • O(n2), which includes the bubble, insertion,
    selection, and shell sorts and
  • O(nlog2n) which includes the heap, merge, and
    quick sorts.
  • Clearly, O(nlog2n) is faster than O(n2)
    algorithms.

5
Advanced Topics
  • If you are interested in
  • Recursive structures
  • Search Algorithms
  • Sort Algorithms
  • read chapter 4 sections 4.5 and 4.6 for a simple
    introduction to the topics, and then
  • Refer Introduction to Algorithms by Cormen,
    Leiserson and Rivest for further study.

6
Search pre-sorted lists
  • Sequential Search
  • Useful for short lists
  • Search through a list of items to find a
    particular item
  • Binary Search
  • Uses recursion i.e., references the procedure
    in the procedure itself
  • Like looking up an entry in a phone directory

7
Sequential Search
  • procedure Search (List, TargetValue)
  • if (List empty)
  • print Search failed.
  • else
  • select first entry in List as TestEntry
  • while (TargetValue gt TestEntry AND
  • there are more entries in the List to
    checkup)
  • select the next entry in the
    List
  • if (TargetValue TestEntry)
  • print Success. Item found in List.
  • else
  • print Item not found in
    List.

8
Linear Search Algorithm (continued)
  • Works best for short lists
  • Best case is when the first entry in the list set
    as TestValue equals the TargetValue
  • Worst case is when it is the last entry

9
Recursion
  • So far weve seen repetition as a loop structure
    executed in a circular fashion
  • When each stage of repetition is executed as a
    sub-task of the previous stage, it is known as a
    recursive repetition structure, or simply,
    recursion.
  • The illusion is that there are multiple copies of
    the procedure itself, each of which is called an
    activation of the procedure
  • These activations are created in a telescopic
    manner and disappear as their role is done and
    the algorithm advances.
  • Of those activations existing at any instant of
    time, only one is actively progressing
  • while the others are in a limbo, each waiting
    for another activation to terminate before it
    gets its turn to continue.

10
Binary Search as an example of Recursion
  • Given a list of sorted items and a target item to
    search if present in that list, we can
  • Find the midpoint in the list and check it
    against the target and
  • Eliminate half the list from further
    consideration
  • Binary search is an algorithm that repeats this
    procedure, halving the remaining portion of the
    list each time till target is located in the
    list, by recursive calls rather than iterative
    looping.
  • Typical example search for a particular entry in
    the telephone directory to get the phone number,
    knowing the name of the entry.

11
Binary Search
  • procedure Search (List, TargetValue)
  • if (List empty)
  • print Search failed.
  • else
  • select middle entry in List as TestEntry
  • execute the appropriate block as the case
    may be
  • case 1 TargetValue TestEntry
  • print Success. Item found in List.
  • case 2 TargetValue lt TestEntry
  • apply procedure Search to
  • the first half of the pre-sorted List
  • case 3 TargetValue gt TestEntry
  • apply procedure Search to
  • the second half of the pre-sorted List

12
Sort strategies
  • Given a list of entries in no particular order,
    we want to arrange them in a specified order
    viz., ascending or descending.
  • Examples
  • Insertion Sort, Bubble Sort O(n2),
  • Quick Sort O(nlog2n)
  • In-class activity Given the set of cards with
    entries on each, arrange them in ascending order.

13
Insertion Sort Algorithm
  • Sorting in place, incrementally
  • Like when arranging the hand you are dealt with
    from a deck of cards
  • Start with empty left-hand and your set of cards
    facing down on the table
  • Remove one card from the table at a time and
    insert it into the correct position in your left
    hand
  • To find the correct position of the new card, we
    compare the new card with the cards already
    ordered in the left hand, scanning it left to
    right (or, right to left)
  • Efficient for small number of elements

14
Simulator Applet for Insertion Sort
  • Visit
  • http//spot.pcc.edu/spreuitt/CS160/
  • Simulators page to run Sort Applets from U
    Michigan.

15
Insertion Sort Algorithm
  • procedure Insertion Sort (List)
  • N 2
  • while (N not greater than length of List)
  • select Nth entry as pivot
  • move pivot to a temporary location
    leaving a hole
  • while (there is an entry above the hole
    and
  • it is greater than the pivot)
  • move the entry above the hole down into
  • the hole leaving a hole above the entry
  • move pivot into hole in List
  • N N 1

16
Insertion Sort Algorithm
  • procedure Insertion Sort (List)
  • for (i 1 i lt N i)
  • temp Listi ith element in the list
  • for (j i j gt 0 AND temp lt Listj j)
  • Listj Listj-1
  • Listj temp

17
Bubble Sort
  • Begin by comparing the entries at positions n and
    (n-1) then, the entries at position (n-1) and
    (n-2) and so on
  • Till the first and second entries are compared.
  • A total on (n-1) passes take place as above, at
    the end of which, the list is sorted.
  • The small (or large, as the case may be) entry
    bubbles up to the top by repeated comparison
    between adjacent values, and hence the name.

18
Quick Sort
  • is left as an exercise
  • It is not covered in your textbook
  • It uses what is known as a divide-and-conquer
    approach.
  • It uses recursive structure that we will only
    introduce in this class but not use extensively.
Write a Comment
User Comments (0)
About PowerShow.com