Sorting I - PowerPoint PPT Presentation

About This Presentation
Title:

Sorting I

Description:

Words of dictionary in alphabetical order. Students names listed alphabetically ... These keys are used to define an ordering of the items in the list. ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 76
Provided by: stansc1
Learn more at: https://www.cs.bu.edu
Category:

less

Transcript and Presenter's Notes

Title: Sorting I


1
Sorting I
  • Chapter 8
  • Kruse and Ryba

2
Introduction
  • Common problem sort a list of values, starting
    from lowest to highest.
  • List of exam scores
  • Words of dictionary in alphabetical order
  • Students names listed alphabetically
  • Student records sorted by ID
  • Generally, we are given a list of records that
    have keys. These keys are used to define an
    ordering of the items in the list.

3
C Implementation of Sorting
  • Use C templates to implement a generic sorting
    function.
  • This would allow use of the same function to sort
    items of any class.
  • However, class to be sorted must provide the
    following overloaded operators
  • Assignment
  • Ordering gt, lt,
  • Example class C STL string class
  • In this lecture, well talk about sorting
    integers however, the algorithms are general and
    can be applied to any class as described above.

4
Quadratic Sorting Algorithms
  • We are given n records to sort.
  • There are a number of simple sorting algorithms
    whose worst and average case performance is
    quadratic O(n2)
  • Selection sort
  • Insertion sort
  • Bubble sort

5
Sorting an Array of Integers
  • Example we are given an array of six integers
    that we want to sort from smallest to largest

0 1 2 3 4
5
6
The Selection Sort Algorithm
  • Start by finding the smallest entry.

0 1 2 3 4
5
7
The Selection Sort Algorithm
  • Swap the smallest entry with the first entry.

0 1 2 3 4
5
8
The Selection Sort Algorithm
  • Swap the smallest entry with the first entry.

0 1 2 3 4
5
9
The Selection Sort Algorithm
Sorted side
Unsorted side
  • Part of the array is now sorted.

0 1 2 3 4
5
10
The Selection Sort Algorithm
Sorted side
Unsorted side
  • Find the smallest element in the unsorted side.

0 1 2 3 4
5
11
The Selection Sort Algorithm
Sorted side
Unsorted side
  • Swap with the front of the unsorted side.

0 1 2 3 4
5
12
The Selection Sort Algorithm
Sorted side
Unsorted side
  • We have increased the size of the sorted side by
    one element.

0 1 2 3 4
5
13
The Selection Sort Algorithm
Sorted side
Unsorted side
Smallest from unsorted
  • The process continues...

0 1 2 3 4
5
14
The Selection Sort Algorithm
Sorted side
Unsorted side
Swap with front
  • The process continues...

0 1 2 3 4
5
15
The Selection Sort Algorithm
Sorted side is bigger
Sorted side
Unsorted side
  • The process continues...

0 1 2 3 4
5
16
The Selection Sort Algorithm
Sorted side
Unsorted side
  • The process keeps adding one more number to the
    sorted side.
  • The sorted side has the smallest numbers,
    arranged from small to large.

0 1 2 3 4
5
17
The Selection Sort Algorithm
  • We can stop when the unsorted side has just one
    number, since that number must be the largest
    number.

0 1 2 3 4
5
18
The Selection Sort Algorithm
  • The array is now sorted.
  • We repeatedly selected the smallest element, and
    moved this element to the front of the unsorted
    side.

0 1 2 3 4
5
19
template ltclass Itemgt void selection_sort(Item
data , size_t n) size_t i, j, smallest
Item temp if(n lt 2) return //
nothing to sort!! for(i 0 i lt n-1
i) // find smallest in unsorted part
of array smallest i for(j i1 j lt n
j) if(datasmallest gt dataj) smallest
j // put it at front of unsorted part of
array (swap) temp datai datai
datasmallest datasmallest temp
20
Selection Time Sort Analysis
  • In O-notation, what is
  • Worst case running time for n items?
  • Average case running time for n items?
  • Steps of algorithm
  • for i 1 to n-1
  • find smallest key in unsorted part of array
  • swap smallest item to front of unsorted array
  • decrease size of unsorted array by 1

21
Selection Time Sort Analysis
  • In O-notation, what is
  • Worst case running time for n items?
  • Average case running time for n items?
  • Steps of algorithm
  • for i 1 to n-1 O(n)
  • find smallest key in unsorted part of array
    O(n)
  • swap smallest item to front of unsorted array
  • decrease size of unsorted array by 1
  • Selection sort analysis O(n2)

22
template ltclass Itemgt void selection_sort(Item
data , size_t n) size_t i, j, smallest
Item temp if(n lt 2) return //
nothing to sort!! for(i 0 i lt n-1
i) // find smallest in unsorted
part of array smallest i for(j i1 j lt n
j) if(datasmallest gt dataj) smallest
j // put it at front of unsorted part of
array (swap) temp datai datai
datasmallest datasmallest temp
Outer loop O(n)
23
template ltclass Itemgt void selection_sort(Item
data , size_t n) size_t i, j, smallest
Item temp if(n lt 2) return //
nothing to sort!! for(i 0 i lt n-1
i) // find smallest in unsorted
part of array smallest i for(j i1 j lt n
j) if(datasmallest gt dataj) smallest
j // put it at front of unsorted part of
array (swap) temp datai datai
datasmallest datasmallest temp
Outer loop O(n)
Inner loop O(n)
24
The Insertion Sort Algorithm
  • The Insertion Sort algorithm also views the array
    as having a sorted side and an unsorted side.

0 1 2 3 4
5
25
The Insertion Sort Algorithm
  • The sorted side starts with just the first
    element, which is not necessarily the smallest
    element.

0 1 2 3 4
5
26
The Insertion Sort Algorithm
  • The sorted side grows by taking the front element
    from the unsorted side...

0 1 2 3 4
5
27
The Insertion Sort Algorithm
  • ...and inserting it in the place that keeps the
    sorted side arranged from small to large.

0 1 2 3 4
5
28
The Insertion Sort Algorithm
0 1 2 3 4
5
29
The Insertion Sort Algorithm
  • Sometimes we are lucky and the new inserted item
    doesn't need to move at all.

0 1 2 3 4
5
30
The Insertionsort Algorithm
  • Sometimes we are lucky twice in a row.

0 1 2 3 4
5
31
How to Insert One Element
  • Copy the new element to a separate location.

0 1 2 3 4
5
32
How to Insert One Element
  • Shift elements in the sorted side, creating an
    open space for the new element.

0 1 2 3 4
5
33
How to Insert One Element
  • Shift elements in the sorted side, creating an
    open space for the new element.

0 1 2 3 4
5
34
How to Insert One Element
  • Continue shifting elements...

0 1 2 3 4
5
35
How to Insert One Element
  • Continue shifting elements...

0 1 2 3 4
5
36
How to Insert One Element
  • ...until you reach the location for the new
    element.

0 1 2 3 4
5
37
How to Insert One Element
  • Copy the new element back into the array, at the
    correct location.

0 1 2 3 4
5
38
How to Insert One Element
  • The last element must also be inserted. Start by
    copying it...

0 1 2 3 4
5
39
Sorted Result
0 1 2 3 4
5
40
template ltclass Itemgt void insertion_sort(Item
data , size_t n) size_t i, j Item
temp if(n lt 2) return // nothing to
sort!! for(i 1 i lt n i) //
take next item at front of unsorted part of array
// and insert it in appropriate location in
sorted part of array temp datai for(j i
dataj-1 gt temp and j gt 0 --j)
dataj dataj-1 // shift element
forward dataj temp
41
Insertion Sort Time Analysis
  • In O-notation, what is
  • Worst case running time for n items?
  • Average case running time for n items?
  • Steps of algorithm
  • for i 1 to n-1
  • take next key from unsorted part of array
  • insert in appropriate location in sorted part of
    array
  • for j i down to 0,
  • shift sorted elements to the right if key gt
    keyi
  • increase size of sorted array by 1

42
Insertion Sort Time Analysis
  • In O-notation, what is
  • Worst case running time for n items?
  • Average case running time for n items?
  • Steps of algorithm
  • for i 1 to n-1
  • take next key from unsorted part of array
  • insert in appropriate location in sorted part of
    array
  • for j i down to 0,
  • shift sorted elements to the right if key gt
    keyi
  • increase size of sorted array by 1

Outer loop O(n)
43
Insertion Sort Time Analysis
  • In O-notation, what is
  • Worst case running time for n items?
  • Average case running time for n items?
  • Steps of algorithm
  • for i 1 to n-1
  • take next key from unsorted part of array
  • insert in appropriate location in sorted part of
    array
  • for j i down to 0,
  • shift sorted elements to the right if key gt
    keyi
  • increase size of sorted array by 1

Outer loop O(n)
Inner loop O(n)
44
template ltclass Itemgt void insertion_sort(Item
data , size_t n) size_t i, j Item
temp if(n lt 2) return // nothing to
sort!! for(i 1 i lt n i) //
take next item at front of unsorted part of array
// and insert it in appropriate location in
sorted part of array temp datai for(j i
dataj-1 gt temp and j gt 0 --j)
dataj dataj-1 // shift element
forward dataj temp
O(n)
O(n)
45
The Bubble Sort Algorithm
  • The Bubble Sort algorithm looks at pairs of
    entries in the array, and swaps their order if
    needed.

0 1 2 3 4
5
46
The Bubble Sort Algorithm
  • The Bubble Sort algorithm looks at pairs of
    entries in the array, and swaps their order if
    needed.

Swap?
0 1 2 3 4
5
47
The Bubble Sort Algorithm
  • The Bubble Sort algorithm looks at pairs of
    entries in the array, and swaps their order if
    needed.

Yes!
0 1 2 3 4
5
48
The Bubble Sort Algorithm
  • The Bubble Sort algorithm looks at pairs of
    entries in the array, and swaps their order if
    needed.

Swap?
0 1 2 3 4
5
49
The Bubble Sort Algorithm
  • The Bubble Sort algorithm looks at pairs of
    entries in the array, and swaps their order if
    needed.

No.
0 1 2 3 4
5
50
The Bubble Sort Algorithm
  • The Bubble Sort algorithm looks at pairs of
    entries in the array, and swaps their order if
    needed.

Swap?
0 1 2 3 4
5
51
The Bubble Sort Algorithm
  • The Bubble Sort algorithm looks at pairs of
    entries in the array, and swaps their order if
    needed.

No.
0 1 2 3 4
5
52
The Bubble Sort Algorithm
  • The Bubble Sort algorithm looks at pairs of
    entries in the array, and swaps their order if
    needed.

Swap?
0 1 2 3 4
5
53
The Bubble Sort Algorithm
  • The Bubble Sort algorithm looks at pairs of
    entries in the array, and swaps their order if
    needed.

Yes!
0 1 2 3 4
5
54
The Bubble Sort Algorithm
  • The Bubble Sort algorithm looks at pairs of
    entries in the array, and swaps their order if
    needed.

Swap?
0 1 2 3 4
5
55
The Bubble Sort Algorithm
  • The Bubble Sort algorithm looks at pairs of
    entries in the array, and swaps their order if
    needed.

Yes!
0 1 2 3 4
5
56
The Bubble Sort Algorithm
  • Repeat.

Swap? No.
0 1 2 3 4
5
57
The Bubble Sort Algorithm
  • Repeat.

Swap? No.
0 1 2 3 4
5
58
The Bubble Sort Algorithm
  • Repeat.

Swap? Yes.
0 1 2 3 4
5
59
The Bubble Sort Algorithm
  • Repeat.

Swap? Yes.
0 1 2 3 4
5
60
The Bubble Sort Algorithm
  • Repeat.

Swap? Yes.
0 1 2 3 4
5
61
The Bubble Sort Algorithm
  • Repeat.

Swap? Yes.
0 1 2 3 4
5
62
The Bubble Sort Algorithm
  • Repeat.

Swap? No.
0 1 2 3 4
5
63
The Bubble Sort Algorithm
  • Loop over array n-1 times, swapping pairs of
    entries as needed.

Swap? No.
0 1 2 3 4
5
64
The Bubble Sort Algorithm
  • Loop over array n-1 times, swapping pairs of
    entries as needed.

Swap? Yes.
0 1 2 3 4
5
65
The Bubble Sort Algorithm
  • Loop over array n-1 times, swapping pairs of
    entries as needed.

Swap? Yes.
0 1 2 3 4
5
66
The Bubble Sort Algorithm
  • Loop over array n-1 times, swapping pairs of
    entries as needed.

Swap? Yes.
0 1 2 3 4
5
67
The Bubble Sort Algorithm
  • Loop over array n-1 times, swapping pairs of
    entries as needed.

Swap? Yes.
0 1 2 3 4
5
68
The Bubble Sort Algorithm
  • Loop over array n-1 times, swapping pairs of
    entries as needed.

Swap? No.
0 1 2 3 4
5
69
The Bubble Sort Algorithm
  • Loop over array n-1 times, swapping pairs of
    entries as needed.

Swap? No.
0 1 2 3 4
5
70
The Bubble Sort Algorithm
  • Continue looping, until done.

Swap? Yes.
0 1 2 3 4
5
71
template ltclass Itemgt void bubble_sort(Item data
, size_t n) size_t i, j Item
temp if(n lt 2) return // nothing to
sort!! for(i 0 i lt n-1 i)
for(j 0 j lt n-1j) if(dataj gt
dataj1) // if out of order, swap!
temp dataj
dataj dataj1 dataj1
temp
72
template ltclass Itemgt void bubble_sort(Item data
, size_t n) size_t i, j Item temp
bool swapped true if(n lt 2) return
// nothing to sort!! for(i 0 swapped and
i lt n-1 i) // if no elements swapped in
an iteration, // then elements are in order
done! for(swapped false, j 0 j lt n-1j)
if(dataj gt dataj1) // if out of
order, swap! temp
dataj dataj dataj1
dataj1 temp swapped true

73
Bubble Sort Time Analysis
  • In O-notation, what is
  • Worst case running time for n items?
  • Average case running time for n items?
  • Steps of algorithm
  • for i 0 to n-1
  • for j 0 to n-2
  • if keyj gt keyj1 then swap
  • if no elements swapped in this pass through
    array, done.
  • otherwise, continue

74
Bubble Sort Time Analysis
  • In O-notation, what is
  • Worst case running time for n items?
  • Average case running time for n items?
  • Steps of algorithm
  • for i 0 to n-1
  • for j 0 to n-2
  • if keyj gt keyj1 then swap
  • if no elements swapped in this pass through
    array, done.
  • otherwise, continue

O(n)
O(n)
75
Timing and Other Issues
  • Selection Sort, Insertion Sort, and Bubble Sort
    all have a worst-case time of O(n2), making them
    impractical for large arrays.
  • But they are easy to program, easy to debug.
  • Insertion Sort also has good performance when the
    array is nearly sorted to begin with.
  • But more sophisticated sorting algorithms are
    needed when good performance is needed in all
    cases for large arrays.
  • Next time Merge Sort, Quick Sort, and Radix Sort.
Write a Comment
User Comments (0)
About PowerShow.com