27
Introduction to Introduction to Computing Computing Dr. Nadeem A Khan Dr. Nadeem A Khan

Introduction to Computing Dr. Nadeem A Khan. Lecture 28

  • View
    219

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Introduction to Computing Dr. Nadeem A Khan. Lecture 28

Introduction to Introduction to ComputingComputing

Dr. Nadeem A KhanDr. Nadeem A Khan

Page 2: Introduction to Computing Dr. Nadeem A Khan. Lecture 28

Lecture 28Lecture 28

Page 3: Introduction to Computing Dr. Nadeem A Khan. Lecture 28

Last Assignment has been postedLast Assignment has been posted

Page 4: Introduction to Computing Dr. Nadeem A Khan. Lecture 28

Two Dimensional ArraysTwo Dimensional Arrays

Page 5: Introduction to Computing Dr. Nadeem A Khan. Lecture 28

►Declaration formats:Declaration formats:

Dim Dim arrayname(m1 arrayname(m1 ToTo n1, m2 n1, m2 ToTo n2 n2) As ) As varTypevarType

ReDim ReDim arrayname(m1 arrayname(m1 ToTo n1, m2 n1, m2 ToTo n2 n2) As ) As varTypevarType

=> Scope (Global/ Form-level/ Local) => Scope (Global/ Form-level/ Local) established in the same way as for 1-D arrays established in the same way as for 1-D arrays

2-D Arrays2-D Arrays

Page 6: Introduction to Computing Dr. Nadeem A Khan. Lecture 28

►Declaration format examples:Declaration format examples:

Private Dim rm(1 To 4, 1 To 4) As String Private Dim rm(1 To 4, 1 To 4) As String

Public Dim rm( ) As StringPublic Dim rm( ) As String

Dim rm( ) As StringDim rm( ) As String

ReDim rm(1 To 4, 1 To 4) ReDim rm(1 To 4, 1 To 4)

ReDim rm(1 To 4, 3 To 4) As StringReDim rm(1 To 4, 3 To 4) As String

2-D Arrays2-D Arrays

Page 7: Introduction to Computing Dr. Nadeem A Khan. Lecture 28

►Value assignment: Value assignment:

Dim student(1 To 2, 1 To 3) as StringDim student(1 To 2, 1 To 3) as String

Let student(1, 3) = “Aslam”Let student(1, 3) = “Aslam”Let student(2, 1) = “Bushra”Let student(2, 1) = “Bushra”

2-D Arrays2-D Arrays

Page 8: Introduction to Computing Dr. Nadeem A Khan. Lecture 28

► Example 1: Example 1:

Program to store and access data from Program to store and access data from Table 7.9Table 7.9

Data is in the data file DISTANCE.TXT Data is in the data file DISTANCE.TXT

2-D Arrays2-D Arrays

Page 9: Introduction to Computing Dr. Nadeem A Khan. Lecture 28

Example 1 (Contd.)Example 1 (Contd.)

Dim rm(1 To 4, 1 To 4) As Single ‘General DeclarationDim rm(1 To 4, 1 To 4) As Single ‘General Declaration

Sub Form_LoadSub Form_LoadDim row As Integer, col As IntegerDim row As Integer, col As IntegerOpen “DISTANCE.TXT” For Input As #1Open “DISTANCE.TXT” For Input As #1For row=1 To 4For row=1 To 4

For col=1 To 4For col=1 To 4Input #1, rm(row, col)Input #1, rm(row, col)

Next colNext colNext rowNext rowClose #1Close #1End SubEnd Sub

2-D Arrays2-D Arrays

Page 10: Introduction to Computing Dr. Nadeem A Khan. Lecture 28

Example 1 (Contd.)Example 1 (Contd.)

Sub Command1_Click( )Sub Command1_Click( )Dim row As Integer, col As IntegerDim row As Integer, col As IntegerLet row=Val(Text1.Text)Let row=Val(Text1.Text)Let col = Val(Text2.Text)Let col = Val(Text2.Text)If (row>=1 And row<=4) And (col>=1 And col<=4) If (row>=1 And row<=4) And (col>=1 And col<=4)

ThenThenCall ShowMileage(rm(), row, col)Call ShowMileage(rm(), row, col)

ElseElseMsgBox ”Range 1 to 4 only”, , “Error”MsgBox ”Range 1 to 4 only”, , “Error”

End IfEnd IfText1.SetFocusText1.SetFocusEnd SubEnd Sub

2-D Arrays2-D Arrays

Page 11: Introduction to Computing Dr. Nadeem A Khan. Lecture 28

Example 1 (Contd.)Example 1 (Contd.)

Sub ShowMileage (rm( ) As Single, row As Integer, Sub ShowMileage (rm( ) As Single, row As Integer, __

col col As Integer)As Integer)

Picture1.ClsPicture1.ClsPicture1.Print “The road mileage is:” rm(row, col)Picture1.Print “The road mileage is:” rm(row, col)End SubEnd Sub

2-D Arrays2-D Arrays

Page 12: Introduction to Computing Dr. Nadeem A Khan. Lecture 28

SortingSorting

Page 13: Introduction to Computing Dr. Nadeem A Khan. Lecture 28

► Sorting: Ordering an unordered Sorting: Ordering an unordered array like the following:array like the following:

PebblesPebblesBarneyBarneyWilmaWilmaFredFredDinoDino

SortingSorting

Page 14: Introduction to Computing Dr. Nadeem A Khan. Lecture 28

►Compare adjacent items and swap Compare adjacent items and swap if out of orderif out of order

► In n-1 passes the array of n In n-1 passes the array of n elements will get sortedelements will get sorted

=>How will you swap values stored in a =>How will you swap values stored in a pair of variables? pair of variables?

Bubble Sorting AlgorithmBubble Sorting Algorithm

Page 15: Introduction to Computing Dr. Nadeem A Khan. Lecture 28

► First Pass First Pass

Pebbles Pebbles Barney Barney Barney Barney Braney Braney BarneyBarney

Barney Barney PebblesPebbles PebblesPebbles Pebbles Pebbles PebblesPebbles

Wilma Wilma Wilma Wilma WilmaWilma FredFred FredFred

Fred Fred Fred Fred Fred Fred WilmaWilma DinoDino

Dino Dino Dino Dino Dino Dino DinoDinoWilmaWilma

Bubble SortingBubble Sorting

Page 16: Introduction to Computing Dr. Nadeem A Khan. Lecture 28

► Second Pass Second Pass

Barney Barney Barney Barney Barney Barney Braney Braney

Pebbles Pebbles PebblesPebbles FredFred Fred Fred

FredFred Fred Fred PebblesPebbles DinoDino

Dino Dino Dino Dino Dino Dino PebblesPebbles

Wilma Wilma WilmaWilma WilmaWilma WilmaWilma

Bubble SortingBubble Sorting

Page 17: Introduction to Computing Dr. Nadeem A Khan. Lecture 28

► Third Pass Third Pass

Barney Barney Barney Barney Barney Barney

FredFred FredFred DinoDino

DinoDino Dino Dino FredFred

PebblesPebbles PebblesPebbles PebblePebbles s

Wilma Wilma WilmaWilma WilmaWilma

Bubble SortingBubble Sorting

Page 18: Introduction to Computing Dr. Nadeem A Khan. Lecture 28

► Fourth Pass Fourth Pass

Barney Barney BarneyBarney

DinoDino DinoDino

FredFred FredFred

PebblesPebbles PebblesPebbles

Wilma Wilma WilmaWilma

Bubble SortingBubble Sorting

Page 19: Introduction to Computing Dr. Nadeem A Khan. Lecture 28

► In each pass compare items at a gap In each pass compare items at a gap andand

swap if out of orderswap if out of order

►Halve the gap in the next passes if no Halve the gap in the next passes if no swap in the current passswap in the current pass

Initial gap: Initial gap: half the array lengthhalf the array lengthInt (array length/2)Int (array length/2)

Final gap: Final gap: adjacent neighboursadjacent neighbours

Sorting: Shell SortSorting: Shell Sort

Page 20: Introduction to Computing Dr. Nadeem A Khan. Lecture 28

► First Pass: gap=2First Pass: gap=2

Pebbles Pebbles Pebbles Pebbles Pebbles Pebbles Pebbles Pebbles

Barney Barney Barney Barney BarneyBarney Barney Barney

Wilma Wilma WilmaWilma Wilma Wilma DinoDino

Fred Fred Fred Fred FredFred Fred Fred

Dino Dino Dino Dino Dino Dino WilmaWilma

Shell SortingShell Sorting

Page 21: Introduction to Computing Dr. Nadeem A Khan. Lecture 28

► Second Pass: gap=2Second Pass: gap=2

Pebbles Pebbles Dino Dino Dino Dino DinoDino

Barney Barney BarneyBarney BarneyBarney Barney Barney

Dino Dino PebblesPebbles Pebbles Pebbles PebblesPebbles

Fred Fred Fred Fred FredFred Fred Fred

Wilma Wilma WilmaWilma Wilma Wilma WilmaWilma

Shell SortingShell Sorting

Page 22: Introduction to Computing Dr. Nadeem A Khan. Lecture 28

► Third Pass: gap=2Third Pass: gap=2

Dino Dino Dino Dino Dino Dino DinoDino

Barney Barney BarneyBarney BarneyBarney Barney Barney

Pebbles Pebbles PebblesPebbles Pebbles Pebbles PebblesPebbles

Fred Fred Fred Fred FredFred Fred Fred

Wilma Wilma WilmaWilma Wilma Wilma WilmaWilma

Shell SortingShell Sorting

Page 23: Introduction to Computing Dr. Nadeem A Khan. Lecture 28

► Fourth Pass: gap=1Fourth Pass: gap=1

Dino Dino Barney Barney Barney Barney Barney Barney Barney Barney

Barney Barney DinoDino DinoDino Dino Dino Dino Dino

Pebbles Pebbles Pebbles Pebbles PebblesPebbles FredFred Fred Fred

Fred Fred Fred Fred Fred Fred PebblesPebbles PebblesPebbles

Wilma Wilma WilmaWilma Wilma Wilma Wilma Wilma WilmaWilma

Shell SortingShell Sorting

Page 24: Introduction to Computing Dr. Nadeem A Khan. Lecture 28

► Fifth Pass: gap=1Fifth Pass: gap=1

Barney Barney Barney Barney Barney Barney Barney Barney Barney Barney

Dino Dino DinoDino DinoDino Dino Dino Dino Dino

Fred Fred Fred Fred FredFred FredFred Fred Fred

Pebbles Pebbles Pebbles Pebbles Pebbles Pebbles PebblesPebbles PebblesPebbles

Wilma Wilma WilmaWilma Wilma Wilma Wilma Wilma WilmaWilma

Shell Sort is complete (next gap=0)Shell Sort is complete (next gap=0)

Shell SortingShell Sorting

Page 25: Introduction to Computing Dr. Nadeem A Khan. Lecture 28

► Number of comparisons in this exampleNumber of comparisons in this example

Bubble SortBubble Sort vs vs Shell SortShell Sort10 1410 14

=> Bubble sort outperformed=> Bubble sort outperformed

► Shell outperforms on long lists (30 or more Shell outperforms on long lists (30 or more items) items)

SortingSorting

Page 26: Introduction to Computing Dr. Nadeem A Khan. Lecture 28

► Sequential searchSequential search

Search sequentially element by element from Search sequentially element by element from startstart

► Binary searchBinary search Repeated divide into halves retaining the half Repeated divide into halves retaining the half

where the quary may liewhere the quary may lie

Searching in a Sorted ListSearching in a Sorted List

Page 27: Introduction to Computing Dr. Nadeem A Khan. Lecture 28

End End