Internal Sorting - PowerPoint PPT Presentation

About This Presentation
Title:

Internal Sorting

Description:

Internal Sorting Each record contains a field called the key. The keys can be places in a linear order. The Sorting Problem Given a sequence of record R1, R2, , Rn ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 20
Provided by: WillT157
Category:
Tags: internal | shell | sort | sorting

less

Transcript and Presenter's Notes

Title: Internal Sorting


1
Internal Sorting
  • Each record contains a field called the key.
  • The keys can be places in a linear order.
  • The Sorting Problem
  • Given a sequence of record R1, R2, , Rn with key
    values k1, k2, , kn, respectively, arrange the
    records into any order s such that records Rs1,
    Rs2, , Rsn have keys obeying the property ks1
    ks2 ksn.
  • Measures of Cost
  • Comparisons
  • Swaps

2
Insertion Sort
  • void inssort (ELEM array, int n)
  • for (int I1 Iltn I)
  • for (int jI (jgt0) (key(arrayj)ltkey(arrayj-
    1)) j--)
  • swap (arrayj, arrayj-1)
  • init I1 I2 I3 I4 I5
    I6 I7
  • 42 20 17 13 13 13
    13 13
  • 20 42 20 17 17 14
    14 14
  • 17 17 42 20 20 17
    17 15
  • 13 13 13 42 28 20
    20 17
  • 28 28 28 28 42 28
    23 20
  • 14 14 14 14 14 42
    28 23
  • 23 23 23 23 23 23
    42 28
  • 15 15 15 15 15 15
    15 42

3
Speed
  • CASE Comparisons Swapping
  • BEST n-1
    0
  • WORST O(n2)
    O(n2)
  • AVERAGE O(n2) O(n2)

4
Bubble Sort
  • void bubsort(ELEM array, int n)
  • for (int I0 Iltn-1 I)
  • for (int jn-1 jgtI j--)
  • if (key(arrayj) lt key(arrayj-1))
  • swap (arrayj, arrayj-1)
  • Example smallest goes to top with each pass.
  • Speed Analysis
  • CASE Comparisons Swaps
  • Best O(n2)
    0
  • Worst O(n2)
    O(n2)
  • Average O(n2)
    O(n2)

5
Selection Sort
  • void selsort (ELEM array, int n)
  • for (int I0 Iltn-1 I)
  • int lowindexI
  • for (int jn-1 jgtI j--)
  • if (key(arrayj)ltkey(arraylowindex))
  • lowindexj
  • swap(arrayI, arraylowindex)
  • EXAMPLE find smallest and put at top of rest of
    list.
  • Speed Analysis
  • CASE Comparisons Swaps
  • Best O(n2)
    0
  • Worst O(n2)
    O(n)
  • Average O(n2)
    O(n)

6
Shell Sort
  • SKIP

7
Pointer Swapping
  • Instead of swapping records, swap pointers in an
    array.
  • Start array with 0, 1, 2, ., n-1.
  • Just needs extra level of subscripting in
    algorithms.
  • void selsort (ELEM array, int n)
  • for (int I0 Iltn-1 I)
  • int lowindexI
  • for (int jn-1 jgtI j--)
  • if (key(arraypointj)
  • ltkey(arraypointlowindex))
  • lowindexj
  • swap(pointI, pointlowindex)

8
Heap Sort
  • Build Heap O(N)
  • Remove smallest
  • O(log N) since need to reheapify
  • Done N times, so O(N log N)
  • Must use a second array for heap
  • Cant sort within the heap array.
  • Unless we use a max heap and put the value
    removed (largest) at the end of the array (which
    is now empty since removed a value).

9
Quicksort
  • Divide and Conquer technique
  • Split the problem into 2 subproblems so that when
    each is solved independently, their solutions
    will be the solution to the whole problem.
  • Need to divide the problem such that all of one
    subproblem has all values less that all the
    values of the other subproblem. This way when
    each subproblem is sorted, the entire array will
    be sorted.
  • Now to sort each subset, we divide and conquer
    again.
  • Repeat until we have subproblems of size 0 or 1.

10
Quicksort Algorithm
  • void qsort (ELEM array, int I, int j)
  • int pivotindex findpivot(array,I,j)
  • swap (arraypivotindex, arrayj)
  • int kpartition(array, I-1,j, key(arrayj))
  • swap (arrayk, arrayj)
  • if ((k-I)gt1) qsort(array,I,k-1)
  • if ((j-k)gt1) qsort(array,k1,j)
  • int findpivot (ELEM array, int I, int j)
  • return (Ij)/2

11
Quicksort Partition
  • int partition (ELEM array, int l, int r, KEY
    pivot)
  • do
  • while (key(arrayl) lt pivot)
  • while (r (key(array--r) gtpivot))
  • swap (arrayl, arrayr)
  • while (lltr)
  • swap (arrayl, arrayr)
  • return l

12
Partition Example
  • initial 72 6 57 88 85 42 83 73 48
    60
  • l
    r
  • Pass 1 72 6 57 88 85 42 83 73 48 60
  • l
    r
  • Swap 1 48 6 57 88 85 42 83 73 72 60
  • l
    r
  • Pass 2 48 6 57 88 85 42 83 73 72 60
  • l r
  • Swap 2 48 6 57 42 85 88 83 73 72 60
  • l r
  • Pass 3 48 6 57 42 85 88 83 73 72 60
  • r l
  • Swap 3 48 6 57 85 42 88 83 73 72 60
  • r l
  • Rev. 48 6 57 42 85 88 83 73 72 60
  • Swap
  • Cost O(n)

13
Quicksort Example
72 6 57 88 60 42 83 73 48
85
Pivot 60
48 6 57 42 60 88 83 73 72
85
Pivot 6
Pivot 73
6 42 57 48
72 73 83 88 85
Pivot 88
Pivot 57
83 85 88
42 48 57
Pivot 83
Pivot 42
42 48
83 85
14
Cost for Quicksort
  • Best Case Always partition in half O(n log n)
  • Worst case Bad partition - have a subproblem of
    size 1 each time. O(n2).
  • Average case
  • T(n) n1 1/(n-1) Sk1 (T(k)T(n-k))
  • O(n log n)
  • Optimizations for Quicksort
  • Better pivot
  • Use better algorithm for small sublists
  • Eliminate recursion.

n-1
15
Mergesort
  • Good for both internal and especially external
    sorting.
  • The function merge merges 2 sorted lists into one
    sorted list. More easily done when sorted.
  • List mergesort(list inlist)
  • if (length(inlist) 1) return inlist
  • list l1half of items from inlist
  • list l2other half of items from inlist
  • return merge(mergesort(l1), mergesort(l2))

16
Mergesort Example
36 20 17 13 28 14 23
15

36 20 17 13 28 14 23
15
36 20 17 13 28 14 23
15
20 36 13 17 14 28 15
23
13 17 20 36 14 15 23
28
13 14 15 17 20 23 28
36
17
Mergesort Implementation
  • void mergesort (ELEM array, ELEM temp, int
    left, int right)
  • int mid (leftright)/2
  • if (leftright) return
  • mergesort(array, temp, left, mid)
  • mergesort(array, temp, mid1, right)
  • for (int Ileft Iltright I)
  • tempIarrayI
  • int i1left
  • int i2mid1
  • for (int curleft currltright curr)
  • if (i1mid1)
  • arraycurrtempi2
  • else if (i2gtright)
  • arraycurrtempi1
  • else if (tempi1lttempi2)
  • arraycurrtempi1
  • else arraycurrtempi2

18
Optimized Mergesort
  • void mergesort(ELEM array, ELEM temp, int
    left, int right)
  • int I, j, k, mid(leftright)/2
  • if (left right) return
  • mergesort(array, temp, left, mid)
  • mergesort(array, temp, mid1, right)
  • for (Ileft Iltmid I) tempI arrayI
  • for (j1 jltright-mid j) tempright-j1
    arrayjmid
  • Ileft
  • jright
  • for (kleft kltright k)
  • if (tempIlttempj) arrayktempI
  • else arrayktempj--

19
Sorting Lower Bounds
  • Want to prove a lower bound for all possible
    sorting algorithms.
  • Sorting is O(n log n).
  • Sorting I/O takes O(n) time.
  • Will prove sorting bound is O(n log n).
  • Model comparison based sorting with a binary
    tree.
  • The tree has n! leaves.
  • n! sqrt(2pn) (n/e)n
  • log(n!) c log(sqrt(n)) n log (n/e) O(n log
    n).
  • The tree is O(n log n) levels deep.
  • log n! O(n log n)
Write a Comment
User Comments (0)
About PowerShow.com