CS114-009 Class 25 - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

CS114-009 Class 25

Description:

Up to 2 hours, but it will be only 50% longer than other exams ... Merge rest of unexhausted array. for (int t = low; t = high; t ) tempList[k ] = array[t] ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 21
Provided by: davidc73
Learn more at: http://cs.ua.edu
Category:

less

Transcript and Presenter's Notes

Title: CS114-009 Class 25


1
CS114-009 Class 25
  • Today
  • Another Class example
  • Merge Sort
  • Final exam review
  • Announcements
  • Final Exam, Thurs. Dec. 11 at 8-1030 am
  • Location Our classroom
  • Up to 2½ hours, but it will be only 50 longer
    than other exams
  • Same format as other exams short answer,
    tracing, coding
  • Open note, open book

2
Yet another class example
  • void main ( )
  • Account a1, a2(5000)
  • cout ltlt a1.get_balance( ) ltlt endl
  • cout ltlt a2.get_balance( ) ltlt endl
  • a1.deposit (1000)
  • a2.deposit (2000)
  • cout ltlt a1.get_balance( ) ltlt endl
  • cout ltlt a2.get_balance( ) ltlt endl
  • a1.withdraw (700)
  • a2.withdraw (4000)
  • cout ltlt a1.get_balance( ) ltlt endl
  • cout ltlt a2.get_balance( ) ltlt endl
  • a2.deposit (2000)
  • cout ltlt a2.get_balance( ) ltlt endl
  • a2.withdraw (4000)
  • cout ltlt a2.get_balance( ) ltlt endl
  • a2.withdraw (4000)
  • cout ltlt a2.get_balance( ) ltlt endl
  • class Account
  • float balance
  • public
  • Account ( )
  • balance0
  • Account (float b)
  • balanceb
  • float get_balance ( )
  • return balance
  • void deposit (float amount)
  • balanceamount
  • void withdraw (float amount)
  • balanceamount

3
Class Exercises
  • What is output of the Account program?
  • Modify the second Account constructor so that the
    balance cannot be initialized to a negative value
  • Modify the deposit( ) method so that the amount
    deposited must be positive
  • Modify the withdraw( ) method so that the amount
    withdrawn must be positive
  • Also modify the withdraw( ) method so that the
    balance can never become negative
  • These methods should print error messages when
    the user attempts to violate any of these
    conditions
  • Compile and run your modified program
  • What is output now?

4
A better algorithm Merge Sort
  • Basic idea
  • A list of size one is sorted!
  • It is easy to merge sorted lists into a larger
    sorted list
  • Algorithm
  • Break list up into lots of small (1 item) lists
  • Merge these lists
  • Example
  • 90 5 35 25 20 50 10
  • 5 10 20 25 35 50 90

5
Efficiency of Merge Sort
  • Two parts to algorithm
  • Partition the array until you get to lists of
    size one
  • Merge these pieces back together
  • Efficiency of the algorithm
  • Merging items is O(N)
  • Why? To merge a list, must process each item once
  • How many times do we merge these lists? O(log2
    N)
  • Why? Start with partitions of size 1 (merge into
    list of size 2), then merge lists of size 2 into
    lists of size 4, etc. until we hit N items in the
    list (similar idea to binary search)
  • Total is O(N log2 N)
  • merge lists log2 N times, must do N work each time

6
Review merge sort algorithm
  • Start
  • Unsorted list
  • Break it down into smaller lists (size 1)
  • Those lists are sorted
  • Merge the sorted lists back together

7
Class Exercises
  • Sort the following arrays using merge sort,
    showing the steps in partitioning and merging
  • 24 12 63 89 7 50 95 14
  • 92 91 52 42 89 23 18 12
  • 28 14 11 19 15 52 80 17 60 59 72 24
    11 34 37 72

8
Making smaller lists
  • Basic idea
  • Dont create new arrays
  • Just keep track of where each sub-list starts
    and stops
  • Once you get down to size 1, the small list is
    sorted

Subscripts on first list are 0 (first item in
list) and 3 (last item in list)
27 65 18 43
The first smaller list has subscripts 0 and 1
The second smaller list has subscripts 2 and 3
list (0..0)
list (1..1)
list (2..2)
list (3..3)
9
Merging lists together
  • Basic idea
  • Start with smallest in each array
  • Loop while you have items left in both lists
  • Compare, put smallest of two in new array
  • Get another item from that list and repeat the
    loop
  • When one list is empty, copy over all remaining
    items from other list

5
13
27
52
5








8
13
15
22
8
15
22
24
24
27
52
10
Merge Sort algorithm part 1
  • Parameters
  • Array
  • Start of this sublist
  • End of this sublist
  • If array is of size one, this part is sorted
  • Otherwise, break into smaller pieces and sort
    them
  • Merge the small sorted pieces together
  • Use a different function
  • void MergeSort ( int array , int a, int z )
  • // 1 element array is sorted
  • if (az) return
  • // Find the middle of the array
  • int mid (az)/2
  • // Make smaller sorted lists
  • MergeSort( array, a, mid )
  • MergeSort( array, mid1, z )
  • // Merge the two sorted halves
  • Merge( array, a, mid, z )

11
Merge Sort algorithm part 2
  • Merge two adjacent lists
  • That is why you only need 3 subscripts
  • Lists adjacent in array
  • int tempList10 // needed temp array
  • // better way in CS 124
  • void Merge ( int array , int f, int m, int l )
  • int i f, j m1, k f
  • // Merge arrays until one is exhausted
  • while (i ltm j lt l)
  • if (arrayi lt arrayj)
  • tempListk arrayi
  • else
  • tempListk arrayj
  • int low, high
  • // Determine which array is left
  • if (i gt m) low j high l
  • else low i high m
  • // Merge rest of unexhausted array
  • for (int t low t lt high t)
  • tempListk arrayt
  • // Copy temp array back to arrayf..l
  • for (k f k ltl k)

27 65 18 43
18 27 43 65
12
Example executing merge sort
  • Array 70 29 87 67
  • Main callsMergeSort with
  • Array 70, 29, 87, 67
  • First 0
  • Last 3
  • Trace of the function calls
  • Calling MergeSort with first0 last3
  • Calling MergeSort with first0 last1
  • Calling MergeSort with first0 last0
  • Calling MergeSort with first1 last1
  • Calling Merge with first0 middle0 last1
  • Calling MergeSort with first2 last3
  • Calling MergeSort with first2 last2
  • Calling MergeSort with first3 last3
  • Calling Merge with first2 middle2 last3
  • Calling Merge with first0 middle1 last3

70
29
87
67
70
29
87
67
87
29
70
67
29
70
67
87
39
67
70
87
13
Class Exercise
  • The file sort.cpp contains a C program that
    implements merge sort and selection sort.
  • Copy this program into Visual Studio .NET and
    compile it
  • Run it a few times with small data (8 or 16 or 32
    or 128 items)
  • Print the data before (youll have to add cout
    statements)
  • Print the data afterwards
  • Print the intermediate calls in merge sort
  • Watch how long selection and merge sort take
  • Try it with bigger data sets
  • Dont do all the printing (before, after, calls)
  • 1000 and 10,000 and 100,000 and 500,000 and
    1,000,000
  • How do the speeds compare once you get to bigger
    data sets?

14
Topics in CS 114 course (outline)
  • Data types
  • int, char, double, float, string
  • Basic operators
  • Arithmetic , , , /,
  • Relational lt, lt, , !, gt, gt
  • Boolean , , !
  • Levels of precedence
  • Sequence
  • Assignment statements
  • , , -, , etc
  • Input and output
  • cin, cout, file input output
  • Selection
  • If statement
  • Switch statement (and break)
  • Iteration
  • While loop
  • For loop
  • Functions
  • Declaration vs. definition
  • Parameters
  • Pass-by-value pass-by-reference
  • Local variables
  • Return type
  • int, double, char, void, etc.
  • Recursion
  • Arrays
  • One- multi-dimensional
  • Passing arrays to functions
  • Efficient algorithms
  • Sequential search vs. Binary search
  • Selection sort vs. Merge sort
  • Character and string variables
  • Classes
  • What is a data type?
  • Using new user-defined types

15
Review Questions
  • Questions
  • What is the difference in
  • a a 1
  • a
  • a
  • a 1
  • What is the difference in
  • include ltiostreamgt
  • include ltfstreamgt
  • What function allows you to test for end-of-file?
  • Why is the file close() function used?
  • Practice, evaluate
  • int a5, b10, c15, d20
  • cout ltlt c/b ltlt endl
  • coutltlt(double) c/b ltltendl
  • cout ltlt cd ltlt endl
  • cout ltlt cb ltlt endl
  • cout ltlt ab/cd ltlt endl
  • cout ltlt b ltlt endl
  • cout ltlt d ltlt endl

16
Review Questions (page 2)
  • Write using for
  • int a10
  • while (alt25)
  • cout ltlt a ltlt endl
  • Write using while
  • for (int a100 agt0 a-5)
  • cout ltlt a ltlt endl
  • What does this do?
  • for (int a0 alt10 a)
  • cout ltlt a ltlt endl
  • Evaluate
  • for (int a0 alt5 a)
  • switch (a)
  • case 0
  • if (a) coutltltYltltendl
  • else cout ltlt Nltltendl
  • case 1
  • case 2
  • cout ltlt 9(a1) ltlt endl
  • break
  • default
  • cout ltlt a ltlt endl

17
Review Questions (page 3)
  • Questions
  • How can you pass back three values from a
    function?
  • Can you have a function with no parameters that
    returns void?
  • Can you mix pass-by-value and pass-by-reference
    parameters in the same function?
  • Can a function have a local variable and a
    parameter with the same name?
  • Questions
  • Which subscript refers to the first element in an
    array?
  • Which subscript refers to the last element in an
    array of 500 items?
  • When passing an array as a parameter to a
    function, do you have to declare its size?

18
Review Questions (page 4)
  • The size of the data (input) plays a role in how
    long it takes to execute a process
  • The bigger the data set, the longer the time
  • Some algorithms are not efficient
  • Makes a difference with large data sets
  • Classes
  • What is a data type?
  • Representation of the info
  • Operations that can be performed on this info

19
Course Evaluations
  • This is a serious instrument.
  • Department heads Deans take note of these
    numbers.
  • Written comments are encouraged, if you have
    something to say beyond the numbers.
  • The instructor is not supposed to be in the room
    while you fill it out. (so Im leaving!)
  • I need a volunteer to take the completed surveys
    to the CS office (101 Houser).

20
End of Class 25
Write a Comment
User Comments (0)
About PowerShow.com