Understanding Algorithms and Data Structures

Slides:



Advertisements
Similar presentations
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Advertisements

C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title: Overview of Data Structure.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Introduction to Data Structures. Definition Data structure is representation of the logical relationship existing between individual elements of data.
Chapter 9 (modified) Abstract Data Types and Algorithms Nell Dale John Lewis.
Chapter 9 Abstract Data Types and Algorithms Nell Dale John Lewis.
9-1 Abstract Data Types Abstract data type A data type whose properties (data and operations) are specified independently of any particular implementation.
Understanding Data Types and Collections Lesson 2.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Understanding General Software Development Lesson 3.
Onlinedeeneislam.blogspot.com1 Design and Analysis of Algorithms Slide # 1 Download From
Data Structure and Algorithms
Understanding General Software Development Lesson 3.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
CPS120: Introduction to Computer Science Sorting.
Arrays Chapter 7.
Mohammed I DAABO COURSE CODE: CSC 355 COURSE TITLE: Data Structures.
Understanding Data Types and Collections Lesson 2.
Introduction toData structures and Algorithms
Lecture 6 of Computer Science II
Review Array Array Elements Accessing array elements
Cpt S 122 – Data Structures Abstract Data Types
Elementary data structures
Week 4 - Monday CS221.
Pointers and Linked Lists
Data Structure By Amee Trivedi.
G64ADS Advanced Data Structures
Course Developer/Writer: A. J. Ikuomola
Pointers and Linked Lists
Stacks and Queues.
September 29 – Stacks and queues
Design & Analysis of Algorithm Priority Queue
12 C Data Structures.
Chapter 15 Lists Objectives
John Hurley Cal State LA
CS 1114: Implementing Search
Stacks and Queues.
Data Structures Interview / VIVA Questions and Answers
Cinda Heeren / Geoffrey Tien
Stack and Queue APURBO DATTA.
Stacks and Queues.
Stacks and Queues.
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
CS1S467 GUI Programming LECTURE 11 Collections
Stacks, Queues, and Deques
structures and their relationships." - Linus Torvalds
Algorithm design and Analysis
Stacks and Queues.
Stacks, Queues, and Deques
Introduction to Data Structures
CS2013 Lecture 4 John Hurley Cal State LA.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Stacks and Queues CLRS, Section 10.1.
The List, Stack, and Queue ADTs
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Introduction to Data Structures
Stacks and Queues.
Introduction to Data Structure
Data Structures & Algorithms
Fundaments of Game Design
Important Problem Types and Fundamental Data Structures
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
BY PROF. IRSHAD AHMAD LONE.
structures and their relationships." - Linus Torvalds
Stacks and Queues.
LINEAR DATA STRUCTURES
Presentation transcript:

Understanding Algorithms and Data Structures LESSON 3.3 98-361 Software Development Fundamentals Understanding Algorithms and Data Structures

Lesson Overview Students will understand algorithms and data structures In this lesson, you will learn about: Arrays Stacks Queues Linked lists Sorting algorithms Performance implications of various data structures Choosing the right data structure

Guiding Questions How many different data structures do you recall? In which situations would you choose to use one over another? Why? Use the guiding question as a class starter allowing the students time to answer the question in their journal. Discuss student answers to the question.

Activator In what ways are data structures similar? In what ways are data structures different? All data structures have methods for accessing and setting data. Some data structures store data in a certain order. Data is input and accessed in different ways. Some data structures are more appropriate for some situations that others; they have different performance implications in different situations.

Review Terms array – a list of data values, all of the same type, any element of which can be referenced by an expression consisting of the array name followed by an indexing expression. data structure – an organizational scheme, such as a record or array, that can be applied to data to facilitate interpreting the data or performing operations on it. linked list – a list of nodes or elements of a data structure connected by pointers.

Review Terms queue – a multi-element data structure from which (by strict definition) elements can be removed only in the same order in which they were inserted; that is, it follows a first-in-first-out (FIFO) constraint. sort algorithm – an algorithm that puts a collection of data elements into some sequenced order, sometimes based on one or more key values in each element. stack – represents a variable size last-in-first-out (LIFO) collection of instances of the same arbitrary type. 6

What is a data structure? Classes used to organize data and provide various operations upon that data The type of data structure used for an algorithm can determine its performance Choice of data structure is dependent on the context A stack is best when you want the elements to be accessed in a last-in-first-out order A linked list is best when there will be a number of insertions into the data structure From Wikipedia 7

Arrays Arrays are one of the simplest and most widely used data structures in computer programs Arrays in any programming language all share a few common properties: The contents of an array are usually stored in contiguous memory All of the elements of an array must be of the same type or of a derived type; hence arrays are referred to as homogeneous data structures Array elements can be directly accessed. If you know you want to access the ith element of an array, you can simply use one line of code: arrayName[i] 8

Arrays If a specified array index is out of bounds, an IndexOutOfRangeException is thrown To change the number of elements an array holds, create a new array instance of the specified size, and then copy the contents of the old array into the new, resized array Searching an unsorted array is acceptable when working with small arrays, or when performing very few searches If an application stores large arrays that are frequently searched, there are other data structures better suited for the job than arrays Give an example here - If you ask for the fourth element in an array that has only three elements then you will get an IndexOutOfRangeException. Array access takes a constant running time, possible solely because an array's elements are stored contiguously, hence a lookup only requires knowledge of the array's starting location in memory, the size of each array element, and the element to be indexed. Linear run time means that the search algorithm’s efficiency has a linear relationship with the number of elements in the array. 9

Example: Array code public static void Main() { int [] nums = new int[3]; num[0] = 23; num[1] = 45; num[2] = 19; Console.WriteLine(“Index 0: ” + nums[0]); Console.WriteLine(“Size: ” + nums.Length); for (int i = 0; i < nums.length; i++) { Console.WriteLine(nums[i]); } } Use this slide to review array syntax. You can also create an array using the following syntax: int[] nums = new int[3] {23, 45, 19}; There are also multi-dimensional arrays 10

Stacks Last-In-First-Out (LIFO) data structure Does not allow random access to elements Can be visualized graphically as a vertical collection of items When an item is pushed onto the stack, it is placed on top of all other items. Popping an item removes the item from the top of the stack Useful when you need access to the item most recently added A stack does not allow random access. That is, you cannot look at the item that was placed first into the stack without popping the two items on top of it. See the graphic on the next slide. 11

Stacks The following animation demonstrates items 1, 2, and 3 being pushed onto the stack in that order, and then a single pop. 3 3 Pushing onto the stack A pop 2 2 This slide is animated 1 1 12

Example: Stack code Push(), Pop(), and Count public static void Main() { Stack myStack = new Stack(); myStack.Push(“1"); myStack.Push(“2"); myStack.Push(“3"); Console.WriteLine(“Last in: ”+myStack.Pop()); Console.WriteLine(“Elements left: ”+myStack.Count); } Last in: 3 Elements left: 2 13

Queues First-In-First-Out (FIFO) data structure Does not allow random access to elements Ideal for situations where you are only interested in processing items in the same order in which they were received Enqueue() and Dequeue() are used to input and access elements in the data structure Behind the scenes, the Queue class maintain an internal circular array and two variables that serve as markers for the beginning and ending of the circular array: head and tail. 14

Queue The following animation demonstrates adding items 1, 2, and 3 into the queue (enqueue) and removing an item (dequeue) Enqueue into the queue 1 2 3 This slide is animated Dequeue from the queue 1 2 3 15

Example: Queue code Enqueue(), Dequeue(), and Count public static void Main() { Queue myQ = new Queue(); myQ.Enqueue(“1"); myQ.Enqueue(“2"); myQ.Enqueue(“3"); Console.WriteLine(“First in: ”+myQ.Dequeue()); Console.WriteLine(“Elements left: ”+myQ.Count); } First in: 1 Elements left: 2 16

Linked Lists A list of nodes or elements of a data structure connected by pointers A singly linked list has one pointer in each node pointing to the next node in the list A doubly linked list has two pointers in each node that point to the next and previous nodes In a circular list, the first and last nodes of the list are linked Does not allow random access to elements Displaying some pictures of linked lists may be helpful. 17

Linked Lists Allow more efficient insertion and removal of elements in the middle of the sequence Because only links are changed, an insertion or removal only affects the neighbors of the inserted or removed element, and does not affect the entire structure Insertion and removal is independent of the number of elements in the list, assuming the spot of insertion or deletion is located Searching for an element has a linear relationship to the number of elements 18

Example: Linked List code public static void Main() { LinkedList<String> myList = new LinkedList <String>(); myList.AddFirst(“one"); myList.AddLast(“two"); Console.WriteLine(“First spot: ”+myList.RemoveFirst()); } First spot: one 19

Sorting Algorithms Steps that put a collection of data elements into some sequenced order, sometimes based on one or more key values in each element Bubble sort Selection sort Insertion sort You might also want to go over Merge Sort and Quick Sort. There are a number of other sorts as well. 20

Bubble Sort Compares each item in the list with the item next to it, and swapping them if needed In one pass, the largest number will eventually end up in the last spot (if the list is being sorted smallest to largest) Repeats this process N-1 times, where N is the number of items to sort Bubble Sort is traditionally considered to be the least efficient. 21

5 6 9 7 2 1 5 6 7 2 1 9 Bubble Sort: 22 This slide is animated 5 6 9 7 2 1 5 6 7 2 1 9 This slide is animated Each line shows a pass through the outer for-loop of the sorting algorithm. Have students guess what the next line will look like. 5 6 9 7 2 1 are the original numbers. In each pass, the largest number is pushed to its spot. 22

Selection Sort Finds the smallest number in the list and swapping it with the number in the index it belongs in In each pass, the part of the array that gets scanned for the smallest number decreases Repeats this process N-1 times, where N is the number of items to sort Students should understand that the smallest number is swapped with the number in the index it belongs in, not inserted. 23

5 6 9 7 2 1 1 6 9 7 2 5 Selection Sort: 24 This slide is animated 5 6 9 7 2 1 1 6 9 7 2 5 This slide is animated Each line shows a pass through the outer for-loop of the sorting algorithm. Have students guess what the next line will look like. 5 6 9 7 2 1 are the original numbers. 24

Insertion Sort The first element in an array is sorted with respect to itself. The array can be said to be composed of a sorted side and an unsorted side Elements from the unsorted side are inserted in the correct order into the sorted side one at a time To create the insertion spot, elements are moved down to make room Repeats this process N-1 times, where N is the number of items to sort 25

5 6 9 7 2 1 5 6 9 7 2 1 Insertion Sort: 26 This slide is animated 5 6 9 7 2 1 5 6 9 7 2 1 This slide is animated 5 6 9 7 2 1 are the original numbers. 26

Lesson Review What program specifications would exist that would cause a programmer or developer to select each of the following data structures? Why? Arrays Stacks Queues Linked lists Describe the steps of how the following data would be sorted using bubble sort, selection sort, and insertion sort. 5 6 2 4 8 3 1 This is the last slide of the presentation.