CSC141- Introduction to Computer programming - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

CSC141- Introduction to Computer programming

Description:

CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture 19 Thanks for Lecture Slides: www.nanamic.org.uk/T%20.../bubblesortexample.ppt – PowerPoint PPT presentation

Number of Views:173
Avg rating:3.0/5.0
Slides: 30
Provided by: woodh150
Category:

less

Transcript and Presenter's Notes

Title: CSC141- Introduction to Computer programming


1
CSC141- Introduction to Computer programming
  • Teacher
  • AHMED MUMTAZ MUSTEHSAN
  • Lecture 19
  • Thanks for Lecture Slides
  • www.nanamic.org.uk/T20.../bubblesortexample.ppt
  • http//freedownload.is/ppt/c-programming-skills-pa
    rt-4-arrays-119712.html
  • 2003 Prentice Hall, Inc.

2
Applications of Arrays
3
Searching Arrays Linear Search
  • Search array for a key value
  • Linear search
  • Compare each element of array with key value
  • Start at one end, go to other
  • Useful for small and unsorted arrays
  • Inefficient, if search key not present, examines
    every element

4
Linear Search Function
  • // The function compares key to every element of
    array until location
  • // is found or until end of array is reached
    return subscript of
  • // element key or -1 if key not found
  • int linearSearch( const int array, int key,
    int sizeOfArray )

  • for ( int j 0 j lt sizeOfArray j )

  • if ( array j key ) // if found,
  • return j // return
    location of key

  • return -1 // key not found

  • // end function linearSearch

5
Sorting Arrays Bubble sort revisit
  • Sorting data (arranging data elements in
    ascending or descending order
  • Important computing application
  • Every organization sometimes requires to sort
    some data
  • Bubble sort
  • Several passes through the array
  • Successive pairs of elements are compared
  • If increasing order (or identical), no change
  • If decreasing order, elements exchanged
  • Repeat these steps for every element

6
Bubble Sort Example
9, 6, 2, 12, 11, 9, 3, 7
6, 9, 2, 12, 11, 9, 3, 7
Bubblesort compares the numbers in pairs from
left to right exchanging when necessary. Here
the first number is compared to the second and as
it is larger they are exchanged.
6, 2, 9, 12, 11, 9, 3, 7
Now the next pair of numbers are compared. Again
the 9 is the larger and so this pair is also
exchanged.
6, 2, 9, 12, 11, 9, 3, 7
In the third comparison, the 9 is not larger than
the 12 so no exchange is made. We move on to
compare the next pair without any change to the
list.
6, 2, 9, 11, 12, 9, 3, 7
The 12 is larger than the 11 so they are
exchanged.
6, 2, 9, 11, 9, 12, 3, 7
The twelve is greater than the 9 so they are
exchanged
The end of the list has been reached so this is
the end of the first pass. The twelve at the end
of the list must be largest number in the list
and so is now in the correct position. We now
start a new pass from left to right.
6, 2, 9, 11, 9, 3, 12, 7
The 12 is greater than the 3 so they are
exchanged.
6, 2, 9, 11, 9, 3, 7, 12
The 12 is greater than the 7 so they are
exchanged.
7
Bubble Sort Example
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass
6, 2, 9, 11, 9, 3, 7, 12
2, 6, 9, 11, 9, 3, 7, 12
2, 6, 9, 9, 11, 3, 7, 12
2, 6, 9, 9, 3, 11, 7, 12
2, 6, 9, 9, 3, 7, 11, 12
Notice that this time we do not have to compare
the last two numbers as we know the 12 is in
position. This pass therefore only requires 6
comparisons.
8
Bubble Sort Example
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass
2, 6, 9, 9, 3, 7, 11, 12
Third Pass
2, 6, 9, 9, 3, 7, 11, 12
2, 6, 9, 3, 9, 7, 11, 12
2, 6, 9, 3, 7, 9, 11, 12
This time the 11 and 12 are in position. This
pass therefore only requires 5 comparisons.
9
Bubble Sort Example
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass
2, 6, 9, 9, 3, 7, 11, 12
Third Pass
2, 6, 9, 3, 7, 9, 11, 12
Fourth Pass
2, 6, 9, 3, 7, 9, 11, 12
2, 6, 3, 9, 7, 9, 11, 12
2, 6, 3, 7, 9, 9, 11, 12
Each pass requires fewer comparisons. This time
only 4 are needed.
10
Bubble Sort Example
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass
2, 6, 9, 9, 3, 7, 11, 12
Third Pass
2, 6, 9, 3, 7, 9, 11, 12
Fourth Pass
2, 6, 3, 7, 9, 9, 11, 12
Fifth Pass
2, 6, 3, 7, 9, 9, 11, 12
2, 3, 6, 7, 9, 9, 11, 12
The list is now sorted but the algorithm does not
know this until it completes a pass with no
exchanges.
11
Bubble Sort Example
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass
2, 6, 9, 9, 3, 7, 11, 12
Third Pass
2, 6, 9, 3, 7, 9, 11, 12
Fourth Pass
2, 6, 3, 7, 9, 9, 11, 12
Fifth Pass
This pass no exchanges are made so the algorithm
knows the list is sorted. It can therefore save
time by not doing the final pass. With other
lists this check could save much more work.
2, 3, 6, 7, 9, 9, 11, 12
Sixth Pass
2, 3, 6, 7, 9, 9, 11, 12
12
Bubble Sort Example Quiz
  1. Which number is definitely in its correct
    position at the end of the first pass?

Answer The last number must be the largest.
  1. How does the number of comparisons required
    change as the pass number increases?

Answer Each pass requires one fewer comparison
than the last.
  1. How does the algorithm know when the list is
    sorted?

Answer When a pass with no exchanges occurs.
  1. What is the maximum number of comparisons
    required for a list of 10 numbers?

Answer 9 comparisons, then 8, 7, 6, 5, 4, 3, 2,
1 so total 45
13
Bubble Sort Function code
  • // This function sorts an array with bubble
    sort algorithm
  • void bubbleSort( int a, int size )
  • int temp // temporary variable used to
    swap elements
  • // loop to control number of passes
  • for ( int pass 1 pass lt size pass )
  • // loop to control number of comparisons
    per pass
  • for ( int j 0 j lt size 1 j )
  • // swap elements if out of order
  • if ( a j gt a j 1 )
  • temp a j
  • a j a j 1
  • a j 1 temp
  • // end if
  • // end function bubbleSort

14
Binary search
  • Only used with sorted arrays
  • Compare middle element with key
  • If equal, match found
  • If key lt middle
  • Repeat search on first half of array
  • If key gt middle
  • Repeat search on last half
  • Very fast
  • At most N steps, where 2 gt Number of elements
  • 30 element array takes at most 5 steps (25 gt 30 )

15
Binary Search
This algorithm is for finding an item in a sorted
list or determining that it is not in the
list. Is Mr Naeem in the list opposite?
First find the middle of the list (101)/2 5.5
so take 6th item
Compare Mr Naeem and Mr Sarfraz, Naeem comes
before Sarfraz so reduce the list to the first
half.
16
Binary Search
Now find the middle (15)/2 3rd item
Compare Mr Naeem and Mr Hmid , Naeem comes after
Hamid so reduce the list to the second half.
17
Binary Search
Find the middle (45)/2 4.5 so take item 5
Compare Mr Naeem and Mr Rashid, Naeem comes
before Rashid so take first half of list
18
Binary Search
Compare Mr Naeem and Mr Naeem, We have found him!
19
Multiple-Subscripted Arrays
  • Multiple subscripts
  • a i j
  • Tables with rows and columns
  • Specify row, then column
  • Array of arrays
  • a0 is an array of 4 elements
  • a00 is the first element of that array

Array name
Column subscript
Row subscript
20
Two-dimensional Array Referencing
  • Referenced like normal
  • cout ltlt b 0 1
  • Outputs 0
  • Cannot reference using commas, Syntax error
  • cout ltlt b 0, 1
  • Function prototypes
  • Must specify sizes of subscripts
  • First subscript not necessary, as with
    single-scripted arrays
  • void printArray( int 3 )

21
  • // Initializing and printing
    multidimensional arrays.
  • include ltiostream.h gt
  • void printArray( int 3 )
  • int main()
  • int array1 2 3 1, 2, 3 , 4,
    5, 6
  • int array2 2 3 1, 2, 3, 4, 5
  • int array3 2 3 1, 2 , 4
  • cout ltlt "Values in array1 by row are" ltlt
    endl
  • printArray( array1 )
  • cout ltlt "Values in array2 by row are" ltlt
    endl
  • printArray( array2 )
  • cout ltlt "Values in array3 by row are" ltlt
    endl
  • printArray( array3 )

22
  • // Function to output array with two rows
    and three columns
  • void printArray( int a 3 )

  • for ( int r 0 r lt 2 r ) // for
    each row

  • for ( int c 0 c lt 3 c ) //
    output column values
  • cout ltlt a r c ltlt ' '

  • cout ltlt endl // start new line of
    output

  • // end outer for structure

  • // end function printArray

Values in array1 by row are 1 2 3 4 5 6 Values
in array2 by row are 1 2 3 4 5 0 Values in
array3 by row are 1 2 0 4 0 0
23
Multidimensional Arrays and Parallel Arrays
  • Example Tracking Students Grades
  • Use initialized data
  • Keep track of students grades
  • Uses two-dimensional array (table)
  • Rows are students
  • Columns are grades
  • Use functions
  • To find out minimum maximum grades
  • To Calculate average grades
  • To print the result

24
  • // Tracking Students grades example.
  • include ltiostream.hgt
  • include ltiomanipgt
  • const int students 3 // number of
    students
  • const int exams 4 // number of exams
  • // function prototypes
  • int minimum( int exams , int, int )
  • int maximum( int exams , int, int )
  • double average( int , int )
  • void printArray( int exams , int, int )
  • int main()
  • // initialize student grades for three
    students (rows)
  • int studentGrades students exams
    77, 68, 86, 73 ,
  • 96, 87, 89, 78 ,
  • 70, 90, 86, 81
  • // output array studentGrades

25
  • // calculate average grade for each
    student
  • for ( int person 0 person lt students
    person )
  • cout ltlt "The average grade for student
    " ltlt person
  • ltlt " is "
  • ltlt average( studentGrades person
    , exams )
  • ltlt endl
  • return 0 // indicates successful
    termination
  • // end main
  • // The following function finds minimum grade
  • int minimum( int grades exams , int
    pupils, int tests )
  • int lowGrade 100 // initialize to
    highest possible grade
  • for ( int i 0 i lt pupils i )
  • for ( int j 0 j lt tests j )

26
  • // The following function finds maximum grade
    of all grades
  • int maximum( int grades exams , int
    pupils, int tests )
  • int highGrade 0 // initialize to
    lowest possible grade
  • for ( int i 0 i lt pupils i )
  • for ( int j 0 j lt tests j )
  • if ( grades i j gt highGrade )
  • highGrade grades i j
  • return highGrade
  • // end function maximum

27
  • // The following function determines average
    grade for particular student
  • double average( int setOfGrades, int tests
    )

  • int sum 0

  • // total all grades for one student
  • for ( int i 0 i lt tests i )
  • sum setOfGrades i

  • return static_castlt double gt( sum ) /
    tests // average

  • // end function maximum

28
  • // The following function prints the
    two-dimensional array of students grades
  • void printArray( int grades exams , int
    pupils, int tests )
  • // set left justification and output column
    heads
  • cout ltlt left ltlt " 0 1
    2 3"
  • // output grades in tabular format
  • for ( int i 0 i lt pupils i )
  • // output label for row
  • cout ltlt "\nstudentGrades" ltlt i ltlt " "
  • // output one grades for one student
  • for ( int j 0 j lt tests j )
  • cout ltlt setw( 5 ) ltlt grades i j
  • // end outer for

29
  • The array is
  • 0 1 2 3
  • studentGrades0 77 68 86 73
  • studentGrades1 96 87 89 78
  • studentGrades2 70 90 86 81
  •  
  • Lowest grade 68
  • Highest grade 96
  • The average grade for student 0 is 76.00
  • The average grade for student 1 is 87.50
  • The average grade for student 2 is 81.75
Write a Comment
User Comments (0)
About PowerShow.com