MIS 215 Module 7 - PowerPoint PPT Presentation

About This Presentation
Title:

MIS 215 Module 7

Description:

The process of dividing an array into two parts based on the value of a 'pivot' element ... When faced with programming problem, see if its solution can be obtained by ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 24
Provided by: WrightStat2
Category:
Tags: mis | module | twofaced

less

Transcript and Presenter's Notes

Title: MIS 215 Module 7


1
MIS 215 Module 7 Sorting
2
Where are we?
MIS215
Basic Algorithms
Introduction
List Structures
Advanced structures
Search Techniques
Intro to Java, Course
Sorting Techniques
Java lang. basics
Hashtables
Linked Lists
Binary Search
Graphs, Trees
Stacks, Queues
Arrays
Bubblesort
Fast Sorting algos (quicksort, mergesort)
Newbie
Programmers
Developers
Professionals
Designers
3
Todays buzzwords
  • Bubblesort
  • The sorting method in which the big elements
    bubble up to the top
  • Selection sort
  • The sorting method where you select the
    smallest item to be placed in the appropriate
    place
  • Insertion sort
  • The sorting method where an item from the
    unsorted part of the array is inserted in the
    sorted part
  • Divide-and-conquer techniques
  • Techniques where the problem space is divided,
    solved separately, and merged
  • Typically results in more efficient solutions
  • Shellsort
  • An advanced version of insertion sort where
    elements are inserted into their appropriate
    intervals
  • Mergesort
  • The divide-and-conquer sorting technique that
    divides the array into two, sorts them separately
    and merges them
  • Partitioning
  • The process of dividing an array into two parts
    based on the value of a pivot element
  • Quicksort
  • The sorting technique where the array is
    partitioned and sorted separately

4
Basic Sorting Techniques
  • Bubble Sort
  • Selection Sort
  • Insertion Sort
  • Sorting Objects
  • Comparing the simple sorts

5
Bubble sort what is it?
  • The simplest sorting strategy at most 5-6 lines
    of code
  • The large elements bubble up to the end of the
    list

6
Bubble sort basic strategy
  • Start at left end
  • Compare two adjacent items, put bigger one in the
    right position
  • any reason why not the smaller one?
  • what to do if theyre equal?
  • Move to the right, repeat the comparison till you
    get to the (right) end
  • What have you got?
  • biggest one at the right end
  • Now, start at the left end again, but move how
    far? Only to the right end minus 1. Repeating..
  • Why is this called a bubble sort?

7
Bubble sort - analysis
  • How many comparisons?
  • How many swaps?
  • Whats the best case?
  • Whats the worst case?

8
Selection sort
  • Start at left end
  • Keep track of smallest when you find a new
    smaller, swap it to left end
  • any reason why not the biggest?
  • what to do if equal?
  • Move to the right, repeating...
  • What have you got?
  • smallest at the left end
  • Now start at left end plus 1, repeating

9
Selection sort - analysis
  • How many comparisons?
  • How many copies?
  • Best case?
  • Worst case?

10
Insertion sort
  • Start at left end plus one
  • put this item in its proper place among the ones
    on its left
  • now move one position to the right, put this item
    in its proper place among the ones the left,
    repeat....

11
Insertion sort - analysis
  • How many comparisons?
  • How many swaps?
  • Best case?
  • Worst case?

12
Efficiencies
  • For random data (which means what?), all 3 run in
    O(N2)
  • so if it took 20 sec to sort 10,000 items, how
    long will it take to sort 20,000?
  • Remember this ignores the constants...
  • so...under the right conditions, one runs faster
    than another
  • for smaller N, selection sort may be faster than
    bubble sort
  • for data already nearly in order, insertion runs
    in (nearly) O(N)

13
Divide-and-Conquer techniques
  • Take a problem space, divide into two, solve
    each, merge solution
  • Why is this better?
  • Often you can disregard one of the partitions!
  • You can only divide into 2 log n times
  • So, if merging takes O(1), we get total O(lg n)!
  • And if merging takes O(n), we get total O(n lg
    n)!
  • O(lg n) is way better than O(n)
  • O(n lg n) is way better than O(n2)

14
Mergesort a divide-conquer sorting strategy
  • Divide the array into two equal (almost) halves
  • Sort each half separately
  • Can call mergesort recursively!
  • Merge the two halves
  • O(n) time complexity if we use O(n) extra space
  • O(n) complexity even if we do not use extra space
    how?
  • Total runtime is O(n lg n) average and optimal
    case.

15
Recursion Tree for MergesortAn Example of 7
numbers
16
Divide and Conquer SortingQuicksort
  • In Quicksort
  • We first choose some key from the list for
    which, we hope, about half the keys will come
    before and half after. Call this key the pivot.
    Then we partition the items so that all those
    with keys less than the pivot come in one
    sublist, and all those with greater keys come in
    another. Then we sort the two reduced lists
    separately, put the sublists together, and the
    whole list will be in order.

17
Trace of QuicksortExample of the Same 7 Numbers

Sort (26,33,35,29,19,12,22)
18
Recursion Tree for QuicksortExample of the Same
7 Numbers

19
QuicksortContiguous Implementation
20
Quicksort Implementation(Contd.)
21
Analysis of Quicksort
  • Worst-case analysis
  • If the pivot is chosen poorly, one of the
    partitioned sublists may be empty and others
    reduced by only one entry. In this case,
    quicksort is slower than either insertion sort or
    selection sort.
  • Average-case analysis
  • In its average case, quicksort performs
  • O(n lg n)
  • comparisons of keys in sorting a list of n
    entries in random order.

22
Quicksort Choice of Pivot
  • First or last entry Worst case appears for a
    list already sorted or in reverse order.
  • Median of three entry Poor cases are rare.
  • Random entry Poor cases are very unlikely to
    occur. Ensure that the random number generation
    algorithm is fast.

23
Pointers and Pitfalls
  • Many computer systems have a general-purpose
    sorting utility. If you can access this utility
    and it proves adequate for your application, then
    use it rather than writing a sorting program from
    scratch.
  • In choosing a sorting method, take into account
    the ways in which the keys will usually be
    arranged before sorting, the size of the
    application, the amount of time available for
    programming, the need to save computer time and
    space, the way in which the data structures are
    implemented, the cost of moving data and cost of
    comparing keys.
  • Divide-and-conquer is one of the most widely
    applicable and most powerful methods for
    designing algorithms. When faced with programming
    problem, see if its solution can be obtained by
    first solving the problem for two (or more)
    problems of the same general form but of a
    smaller size. If so, you may be able to formulate
    an algorithm that uses the divide-and-conquer
    method and program it using recursion.
Write a Comment
User Comments (0)
About PowerShow.com