Programming Fundamentals Session III www profburnett com Master
Programming Fundamentals Session III www. profburnett. com Master a Skill / Learn for Life
Session Outline � Data Structures � Chapter 1 - Structures and Arrays � Chapter 2 - Sets and Linked List � Chapter 3 - Collections and Dictionaries � Chapter 4 - Stack, Queues, and Deques � Chapter 5 - Graphs and Trees � Algorithms � Chapter 1 - Sorting Algorithm � Chapter 2 - Searching Algorithms � Chapter 3 - String Searching � Chapter 4 - Data Compression Algorithms � Chapter 5 - Encryption Algorithm 1/4/2022 Copyright © Carl M. Burnett 2
Data Structures �Chapter 1 - Structures and Arrays �Chapter 2 - Sets and Linked Lists �Chapter 3 - Collections and Dictionaries �Chapter 4 - Stacks, Queues, Disques �Chapter 5 - Graphs and Trees 1/4/2022 Copyright © Carl M. Burnett 3
Structures and Arrays � Structure – Record = Database record � Array: � Series or list of variables in computer memory � All share the same name � Each has a different subscript � Subscript (or index): � Position number of an item in an array � Subscripts are always a sequence of integers 1/4/2022 Copyright © Carl M. Burnett 4
Defining an Array �Variable Name �Array Size – Number of Items to Store �Type of Data – Integer or String Dim Name. Array(15) as String 1/4/2022 Copyright © Carl M. Burnett 5
How Arrays Occupy Computer Memory � � some. Val[0] some. Val[1] Each item has same name and same data type Element: an item in the array Array elements are contiguous in memory Size of the array: number of elements it will hold some. Val[2] a. Number 25 1/4/2022 36 47 Copyright © Carl M. Burnett 15 6
How Arrays Occupy Computer Memory � Subscript is placed in parentheses or square brackets following the array name (language-dependent) � Zero-based array: � First subscript is 0 � Highest subscript value is 1 less than the array size � Arrays make programs more efficient and professional 1/4/2022 Copyright © Carl M. Burnett 7
Defining Size �Bounds �Lower Bound - First Element �Upper Bound – Last Element �Default Bounds �C and Java = Starts with 0 (Zero-Based Array) �Other Language = Start with 1 (One-based Array) 1/4/2022 Copyright © Carl M. Burnett 8
Defining Size 0 1 2 3 4 Zero-Based Array 1 2 3 4 5 One-Based Array Dim Lottery. Numbers(5) as Integer 1/4/2022 Copyright © Carl M. Burnett 9
Defining Bounds Dim Lottery. Numbers(33. . 37) as Integer 33 1/4/2022 34 35 Copyright © Carl M. Burnett 36 37 10
Array Declaration and Initialization Declaration DIM COUNT(30) Programming Language BASIC, Visual Basic int count(30); C#, C++ int() count = new int(30); Java COUNT OCCURS 30 TIMES PICTURE 9999. COBOL Array count (1… 30) of integer; Pascal � Array elements do not have to be declared individually � Put the array size in square brackets in the declaration 1/4/2022 Copyright © Carl M. Burnett 11
Array Declaration and Initialization �Array elements should be initialized �Initialization loop: loop structure that provides initial values to an array �Use the loop control variable as the array subscript 1/4/2022 Copyright © Carl M. Burnett 12
Array Declaration and Initialization Basic Dim Lottery. Numbers(5) as Integer For I = 1 to 5 Lottery. Numbers(1) = 0 Next I C++ Int lotterynumbers[] = (0, 0, 0); 1/4/2022 Copyright © Carl M. Burnett 13
Storing Data in Array �The Array Name �Array Element Basic Dim myarray(5) as Integer myarray(1) = 357 C++ Int myarray[5]; myarray[0] = 357; Int myarray[5]; myarray[0] = 47; myarray[3] = 91; myarray[1] = 6; 1/4/2022 Copyright © Carl M. Burnett 14
Retrieving Data Basic Dim Names(3) as String Names(1) = “George Washington” Names(2) = “John Adams” Names(3) = “Thomas Jefferson” Print Names (2) This prints the name John Adams on screen. 1/4/2022 Copyright © Carl M. Burnett 15
Multi-Dimensional Arrays �Sometime known as a Data Grid 1/4/2022 Basic Big. Array (1, 1) Big. Array (1, 2) Big. Array (2, 1) Big. Array (2, 2) Dim Big. Array(2, 4, 3, 8) as String Big. Array (3, 1) Big. Array (3, 2) C+ + string Big. Array[4][2]; Big. Array (4, 1) Big. Array (4, 2) Dim Big. Array(4, 2) as String string Big. Array[2][4][3][8]; Copyright © Carl M. Burnett 16
Multi-Dimensional Arrays Storing and Getting Data Dim Big. Array(4, 2) as String Big. Array(4, 1) = “Grover Cleveland” Print Big. Array(4, 1) This prints the name Grover Cleveland on screen. 1/4/2022 Copyright © Carl M. Burnett 17
Array Drawbacks �Large Arrays take up more space �Arrays can hold only one data type �Searching and sorting is difficult �Inserting and removing data is cumbersome 1/4/2022 Copyright © Carl M. Burnett 18
Sets and Linked Lists �Arrays stores integers. �You may need to create resizable (dynamic) arrays. �Arrays stores a special Variant data type - both numbers and text. �Many programming languages allow you to create other types of data structures. �Two popular alternatives to arrays are: �Sets �Linked Lists 1/4/2022 Copyright © Carl M. Burnett 19
Sets An array to hold seven separate variables like this: Dim Day(6) as String Day(0) Day(1) Day(2) Day(3) Day(4) Day(5) Day(6) 1/4/2022 = = = = "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday" "Sunday" From set import sets days = Set ([‘Monday’, ‘Tuesday’, ‘Wednesday’, ‘Thursday’, ‘Friday’, ‘Saturday’, ‘Sunday’]) Advantages • • • No definition of fixed size. No ID of index number for data Can use mixed data types Copyright © Carl M. Burnett 20
Linked Lists � It is a data structure � Group of nodes � Represent a sequence. � A linear collection of data elements � Pointer - Points to the next node by means of a pointer. Variables Pointer 1/4/2022 Variables hold data A pointer points to another node Copyright © Carl M. Burnett 21
Linked Lists Advantages � dynamic data structure - can grow and be pruned, allocating and deallocating memory � Insertion and deletion node operations are easily implemented in a linked list. � Dynamic data structures such as stacks and queues can be implemented using a linked list. � There is no need to define an initial size for a Linked list. � Items can be added or removed from the middle of list. 1/4/2022 Copyright © Carl M. Burnett Disadvantages � They use more memory than arrays � Nodes must be read in order - sequential access. � Nodes are stored incontiguously - greatly increasing the access time required. � Difficulties in reverse traversing. 22
Collections � A grouping of some variable number of data items � Shared significance to the problem being solved � Need to be operated upon together in some controlled fashion. � Like a resizable array. � Can hold different data types. � Collections include: � Lists � Sets Implementations include: � Multisets • C++: known as "container“ � Trees • Java: implemented in the Java collections framework; • Python: some built-in, others implemented in the collections library. � graphs 1/4/2022 Copyright © Carl M. Burnett 23
Dictionaries � An associative array, map, symbol table. � Abstract data type � Composed of a collection of (key, value) pairs. � Operations associated with this data type allow: � the addition of a pair to the collection � the removal of a pair from the collection � the modification of an existing pair � the lookup of a value associated with a particular key 1/4/2022 Copyright © Carl M. Burnett 24
Dictionaries Language support � Dictionaries - Smalltalk, Objective-C, . NET, [13] Python, REALbasic, Swift, and VBA. � Hashes - Perl, Ruby and Seed 7 � Maps - C++, Java, Go, Clojure, Scala, OCaml, Haskell, � Hash tables - Common Lisp and Windows Power. Shell � Associative Arrays - PHP � Objects – Java. Script, JSON (All objects behave as associative arrays with string-valued keys. 1/4/2022 Copyright © Carl M. Burnett 25
Stacks 1/4/2022 Copyright © Carl M. Burnett 26
Queues & Deques 1/4/2022 Copyright © Carl M. Burnett 27
Graphs �Data structure: �Vertices or Nodes or Points �Connections - set of unordered pairs of these vertices 1/4/2022 Copyright © Carl M. Burnett 28
Graphs Abstract Data Type Undirected graph 5 2 3 Directed graph 9 6 Weighted graph 3 1 1/4/2022 Copyright © Carl M. Burnett 8 7 29
Trees �Simulates a hierarchical tree structure �A root value �Subtrees of children with a parent node �Represented as a set of linked nodes. 1/4/2022 Copyright © Carl M. Burnett 30
Trees � Root - The top node in a tree. � Child - A node directly connected to another node when moving away from the Root. � Parent - The converse notion of a child. � Siblings - A group of nodes with the same parent. � Descendant - A node reachable by repeated proceeding from parent to child. � Ancestor - A node reachable by repeated proceeding from child to parent. � Leaf (External node) - A node with no children. � Branch (Internal node) - A node with at least one child. � Degree - The number of sub trees of a node. 1/4/2022 Copyright © Carl M. Burnett � Edge - The connection between one node and another. � Path - A sequence of nodes and edges connecting a node with a descendant. � Level - The level of a node is defined by 1 + (the number of connections between the node and the root). � Height of node - The height of a node is the number of edges on the longest path between that node and a leaf. � Height of tree - The height of a tree is the height of its root node. � Depth - The depth of a node is the number of edges from the tree's root node to the node. � Forest - A forest is a set of n ≥ 0 disjoint trees. 31
Algorithms �Sorting Algorithms �Searching Algorithms �String Searching Algorithms �Data Compression Algorithms �Encryption Algorithms 1/4/2022 Copyright © Carl M. Burnett 32
Sorting Algorithms Comparison of Different Sorting Algorithms Algorithm Average Best Worst Bubble Sort O(n^2) Selection Sort O(n^2) Insertion Sort O(n^2) Heap Sort O(n*log(n)) Merge Sort O(n*log(n)) Quick Sort O(n*log(n)) O(n^2) 1/4/2022 Copyright © Carl M. Burnett 33
Searching Algorithms �Category of Searching Algorithms �Uninformed (Brute-Force) �Informed (Heuristic) 1/4/2022 Copyright © Carl M. Burnett 34
Sequential Search Algorithms �Uninformed Algorithm �Backward or Forward �Block �Binary �Interpolation 1/4/2022 Copyright © Carl M. Burnett 35
Informed Searching �Indexes �Clustered Index �Unclustered Index �Adversarial Search �Alpha-bets Pruning 1/4/2022 Copyright © Carl M. Burnett 36
String Searching Algorithms � Sequential Text � Boyer-Moore Algorithm � Rabin-Karp Algorithm � Shift Or Algorithm � Regular Expressions � Multiple Character Patterns � Alternate Patterns � Phonetically 1/4/2022 Copyright © Carl M. Burnett 37
Data Compression Algorithms �Lossless Data Compression �Run-Length Encoding (RLE) �Burrow-Wheeler Algorithm �Dictionary Encoding �LZ 77 Algorithm �LZW Algorithm �Lossy Data Compression 1/4/2022 Copyright © Carl M. Burnett 38
Encryption Algorithms �Stream Ciphers �Block Ciphers �Electronic Code Book (ECB) �Cipher-block Chaining (CBC) �Symmetrical Encryption �Asymmetrical Encryption �Hash Functions 1/4/2022 Copyright © Carl M. Burnett 39
Student Exercise Continue to work on your Getting Dressed Program. Identify and create a data dictionary for all the data types used in your process. These include: �Data Variables �Arrays �Collections �Classes �Objects �Metadata 1/4/2022 Copyright © Carl M. Burnett 40
Session Review � Data Structures � Chapter 1 - Structures and Arrays � Chapter 2 - Sets and Linked List � Chapter 3 - Collections and Dictionaries � Chapter 4 - Stack, Queues, and Deques � Chapter 5 – Graphs and Trees Next – Web Programming & Web Programming Languages � Algorithms � Chapter 1 - Sorting Algorithm � Chapter 2 - Searching Algorithms � Chapter 3 - String Searching � Chapter 4 - Data Compression Algorithms � Chapter 5 - Encryption Algorithm 1/4/2022 Copyright © Carl M. Burnett 41
- Slides: 41