CS 106 Computing Fundamentals II Chapter 77 - PowerPoint PPT Presentation

PPT – CS 106 Computing Fundamentals II Chapter 77 PowerPoint presentation | free to download - id: 771db1-Mjc1M

The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
Title:

CS 106 Computing Fundamentals II Chapter 77

Description:

CS 106 Computing Fundamentals II Chapter 77 Algorithm Herbert G. Mayer, PSU CS Status 7/24/2013 Initial content copied verbatim from CS 106 material developed by – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 28
Provided by: pdx69
Category:
Tags:
Transcript and Presenter's Notes

Title: CS 106 Computing Fundamentals II Chapter 77

1
CS 106 Computing Fundamentals II Chapter
77 Algorithm
• Herbert G. Mayer, PSU CS
• Status 7/24/2013
• Initial content copied verbatim from
• CS 106 material developed by
• CS professors Cynthia Brown Robert Martin

2
Syllabus
• Algorithm
• Finding Smallest Element
• Find Smallest and Make it First
• Switching the Values of Variables
• Using an Array
• Sorting
• Time for Selection Sort
• Importance of Algorithm
• Software Engineering

3
Algorithm
• An algorithm is a finite set of instructions
which if followed will lead to a correct problem
solution in a finite amount of time
• Sorting is a very important problem and has been
studied extensively
• We build up a sort algorithm by starting with
finding the smallest element in an array

4
Finding Smallest Element
• min data(1) 'the smallest so far
• For k 2 To lastIndex
• 'test each element for min so far
• If data(k) lt min Then 'new min found
• min data(k)
• End If
• Next

5
Find Smallest and Make it First
• What if we want to find the smallest element and
put it first in the array?
• Well do this by switching it with the first
element
• We need to know the index of the smallest element
to do this
• A slight modification of our code for finding the
smallest element will let us do this

6
Find Smallest Element and Index
• min data(1) 'the smallest so far
• minIndex 1
• For k 2 To lastNdx
• 'test each element to see if it is
the min so far
• If data(k) lt min Then 'new min
found
• min data(k)
• minIndex k
• End If
• Next k

7
Switching the Values of Variables
• Consider the following code
• varA 1
• varB 4
• varA varB
• varB varA
• What are the values of varA and varB after I do
this?

8
Switching the Values of Variables
• Doing it right
• varA 1
• varB 4
• temp varB
• varB varA
• varA temp
• What are the values of varA and varB after I do
this?

9
Now Using the Array
• find the smallest element, value and index
• min data(1) 'the smallest so far
• minIndex 1
• For k 2 To lastNdx
• 'test each element to see if it
is the min so far
• If data(k) lt min Then 'new min
found
• min data(k)
• minIndex k
• End If
• Next
• exchange the smallest element with the
first
• temp data(1)
• data(1) data(minIndex)
• data(minIndex) temp

10
Sorting
• Find the smallest element in the array, and
switch it with the first one
• Find the smallest element in the rest of the
array, and switch it with the second one
• Etc.
• This is called selection sort

11
Algorithm picture (1)
• Heres an initial array
• The smallest element is in index 3. If we switch
it with the element in index 1, we get
• We now know the first element is the smallest.

10 5 2 11 3
1 2 3 4 5
2 5 10 11 3
1 2 3 4 5
12
Algorithm picture (2)
• Looking at elements 2-5, the smallest is in index
5
• Lets switch with the element in index 2
• Now we know the first two are smallest, and are
in the right order.

2 5 10 11 3
1 2 3 4 5
2 3 10 11 5
1 2 3 4 5
13
Algorithm picture (3)
• Consider elements 3-5. The smallest is in
position 5.
• Lets switch with the element in index 3

2 3 10 11 5
1 2 3 4 5
2 3 5 11 10
1 2 3 4 5
14
Algorithm picture (4)
• Consider elements 4-5. The smallest is in
position 5.
• Lets switch with the element in index 4
• This finishes sorting the array (why?)

2 3 5 11 10
1 2 3 4 5
2 3 5 10 11
1 2 3 4 5
15
Algorithm Structure
• Starting with the whole array, then progressing
at the rest without the first element, then the
array without the second element, etc.
• If we work on a whole array of n elements, thats
a loop from 1 to n, i.e. with n iterative steps
• If we work on a whole array minus the first
element, that loop is from 2 to n
• Next we do 3 to n, etc.

16
Loop Setup
• A loop from 1 to n looks like
• For k 1 To n
• ltcodegt
• Next k
• Heres a loop from 2 to n
• For k 2 To n
• ltcodegt
• Next k

17
In general
• We need a loop that looks like this
• For k j To n
• ltcodegt
• Next k
• for each j going from 1 to n-1. This we can do
by using another loop!

18
The Nested Loop
• Heres what the structure looks like
• For j 1 To n - 1
• For k j 1 To n
• ltcodegt
• Next k
• Next j

19
Heres the Code
• min data(j) 'the largest so far
• minIndex j
• For k j 1 To lastNdx look at
• If data(k) lt min Then
• min data(k)
• minIndex k
• End If
• Next k
• temp data(j) exchange the smallest
element with element j
• data(j) data(minIndex)
• data(minIndex) temp
• Next j

20
Tricky Bits
• Note the -1 and 1 in the loop limits. Getting
those right takes some thought
• Does the code work on arrays with just one
element? With two elements? With no elements?
(Nothing to sort, but we want to avoid a runtime
error.) What if the data is already sorted?

21
Demo Simple Sort
22
Other Ways of Sorting
• There are actually many ways of sorting items
• Sorting is very important so people have put a
lot of thought into it
• Some well-known methods
• Bubble sort
• Quicksort
• Heapsort
• Mergesort
• Bucket sort

23
Which Method is Best?
• With small data sets, the best method is usually
the easiest one to program
• With large data sets, speed becomes an issue
• We could measure the time with a stopwatch, but
the essential factor is the functional form of
the time if n is the length of the list of data,
is the time proportional to n? n log n? n2?

24
Comparison of n, n log n, n2
25
Time for Selection Sort
• The number of comparisons of data elements in a
sorting algorithm is usually proportional to the
time
• On the first loop in Selection Sort, we do n-1
comparisons. The second loop does n-2, etc the
last loop does 1
• So the time is roughly proportional to
• (n-1) (n-2) 1 (n2 n)/2
• The largest power of n is n2 which dominates the
time for this algorithm
• This means Selection Sort is actually too slow to
use on large amounts of data

26
Importance of Algorithms
• You now know the basics for programming
assignment statements, conditionals, procedures
and functions, loops, and arrays
• This is like knowing the rules for chess or go
• What you have only started to learn are the
tactics and strategies to use these tools
effectively
• Algorithms are the tactics for how to accomplish