SEQUENCES - PowerPoint PPT Presentation

About This Presentation
Title:

SEQUENCES

Description:

SEQUENCES. SORTING. Sorting. Input: (unsorted) sequence of n elements a1, a2, ..., an ... What if you had 10,000 unsorted records and each record had a value between ... – PowerPoint PPT presentation

Number of Views:13
Avg rating:3.0/5.0
Slides: 39
Provided by: duanemar
Category:

less

Transcript and Presenter's Notes

Title: SEQUENCES


1
SEQUENCES
  • SORTING

2
Sorting
  • Input (unsorted) sequence of n elements a1, a2,
    , an
  • Output the same sequence in increasing order (or
    in nondecreasing order if there are equal
    elements)

3
Sorting
  • What do we measure ?- Comparisons and swaps
    whichever dominates - However, comparisons
    dominate most of the time

4
Sorting A Different View
  • sorting can be seen as a permutation problem
  • determine a permutation of indices i1, i2, , in
    such that ai1ltai2ltltain
  • n! possible permutations

5
A Few Assumptions
  • we extend dictionary ADT to include a sort
    method
  • sort the keys of the key-element pairs (k, e)
  • implemented by an array-based sequence, unless
    specifically stated

6
DICTIONARY METHODS
  • size()
  • isEmpty()
  • findElement(k)
  • findAllElements(k)
  • return no. of items in D
  • test if D is empty
  • return element of item with key equal to k, else
    NO_SUCH_KEY
  • return an enumeration of all elements with key k

7
Sorting
  • Focus on the process
  • Methodology might be useful in generating
    solutions to other problems (i.e. brainstorming)

8
Sorting Algorithms
  • O(n2) Bubble, Selection, Insertion Sort
  • O(n log n) Merge, Quick, Heap Sort
  • O(n)input is qualified Bucket Sort

9
Bubble Sort
  • view the sequence to be in a vertical instead of
    a horizontal position (easier to visualize)
  • the items are like bubbles in a water tank with
    weights according to their keys
  • then, each pass results in the bubble rising to
    its proper level of weight

10
Bubble Sort - Pseudocode
  • Algorithm BubbleSort
  • for i ? 0 to n-1
  • for j ? 0 to n-i
  • if (sj.key gt sj1.key) then
  • swap (sj,sj1)

11
Insertion Sort
  • Repeatedly insert element in its proper place
  • Algorithm InsertionSort
  • for i ? 1 to n-1 dox ? Siinsert x in the
    proper place in S0Si

12
Insertion Sort Example
13
Insertion Sort TC
  • array implementation at most O(n) data moves and
    O(n) comparisons per pass O(log n) if binary
    search is used
  • linked-list implementation O(1) data moves but
    still O(n) comparisons per pass (binary search is
    not possible)
  • insertion sort is on the average n(n-1)/4 or
    O(n2)

14
Simple Sorting Algorithms
  • Bubble Sort
  • Selection Sort
  • Insertion Sort
  • All have running times of O(n2)

15
  • Advanced Sorting Algorithms

16
Merge - Query
  • Given two sorted arrays A and B, arrange the
    elements to a third array C (in sorted order)
  • A23,47,81,95
  • B7,14,39,55,62,74

17
Merge - Operations
18
Merged List
  • A23,47,81,95
  • B7,14,39,55,62,74
  • C7,14,23,39,47,55,62,74,81,95

19
Merge Sort - General
  • Divide into two parts
  • Sort left side, Sort right side
  • Merge left and right side

20
Merge Sort - Mathematical
  • Divide
  • divide S into 2 equal sequences S1 and S2
  • Recurse
  • recursively sort sequences S1 and S2
  • Conquer
  • merge sorted sequences S1 and S2 back into S

21
Merge Sort Example
22
Closer Look At Merging
23
Merge Sort TC
  • t(n) time to merge sort n elements
  • t(n/2) time to merge sort n/2 elements
  • t(n) 2t(n/2) O(n), a recurrence relation
  • gt merge sort is O(n log n) look at diagram
  • Note merge sort requires extra space

24
Divide-and-Conquer
  • Divide
  • if input size lt threshold, solve directly
  • else, divide data into 2 or more subsets
  • Recurse
  • recursively solve subproblems associated with
    subsets
  • Conquer
  • take solutions to subproblems
  • combine into solution to original problem

25
Advanced Sorting
  • O(n log n) vs O(n2) significant
  • For 10,000 records, n2 100,000,000 n log n
    40,000
  • if sorting using merge sort takes 40 seconds,
    insertion sort will take 28 hrs
  • Theres another sorting algorithm (same level)
    which does not require additional space

26
Partitioning - Query
  • Consider this problem, Make an algorithm, that
    will arrange List L so that all items less than
    50 will be to the left of 50 while all items
    greater than 50 will be at the right of 50 in one
    pass.List L 85,24,63,45,17,31,96,50.

27
Partitioning
  • Create pointer at both ends of the list
  • If a0lt50, then it is in the right
    position,else (wrong position) stop and wait
    for the right side to find a wrong entry If
    angt50 then it is in the right position else
    (wrong position) stop and wait for the right
    side to find a wrong entry If both wrong
    entries, swap

28
Partitioning Example
  • partitioning around pivot 50

29
Quick Sort
  • Divide if S has at least 2 elements, select x
    from S called the pivot. Put elements of S into
    3 subsequences
  • L, all elements in S less than x
  • E, all elements in S equal to x
  • G, all elements in S greater than x
  • Recurse recursively sort L and G
  • Conquer Put back elements into S in order by
    concatenating L, E, and G

30
Quick Sort Example
  • new pivot is selected each line

31
Quick Sort
  • Pivot can be selected at random
  • Usually, its the rightmost or the leftmost
    entry
  • On the average, it will be in the middle portion
    of the list

32
Quick Sort TC
  • time complexity depends on pivot choice
  • average O(n log n)
  • worst O(n2) - data is skewed, reverse order

33
O(n log n) Algorithms
34
Bucket Sort
  • What if you had 10,000 unsorted records and each
    record had a value between 1 to 10 which you had
    to arrange in increasing order (1,1,1,1,2,2,2,etc.
    )?
  • Can we beat O(n log n) ?

35
Bucket Sort
  • input restriction sequence S of n items with
    keys in the range 0,N-1
  • can sort S in O(nN) time
  • if N is O(n), then sorting time is O(n)
  • note NOT based on comparison

36
Pseudocode
  • Algorithm BucketSort
  • Input Sequence S with integer keys in range
    0,N-1
  • Output S sorted
  • let Barray of N initially empty sequences
  • for each item x in S do
  • kkey of x
  • remove x from S and insert at end of sequence
    Bk
  • for i ? 0 to N-1 do
  • for each item x in sequence Bi do
  • remove x from Bi and insert at end of S

37
Other Sorting Algorithms
  • Shell SortImprovement of Insertion Sort-Sorts
    widely spaced elements (i.e. every
    4th)-Diminishing gaps (364,121,40,1)- Insertion
    sort the last pass- Running time (estimated)
    between O(n7/6) to O(n3/2)more realistic)

38
Sorting - Summary
  • Looked at various sorting algorithms
  • Analyzed their running time
  • Comparisons and swaps/copies
  • Implementation described in the book
  • Review recursion
Write a Comment
User Comments (0)
About PowerShow.com