Data Structure & Algorithm

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Practice Quiz Question
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
C Programming : Elementary Data Structures 2009/04/22 Jaemin
Computability Start complexity. Motivation by thinking about sorting. Homework: Finish examples.
Lecture - 1 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Data Type and Data Structure Data type Set of possible values for variables.
CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Lists CSE1303 Part A Data Structures and Algorithms.
Data Structures Using C++ 2E
Data Structure & Algorithm Lecture 3 –Algorithm Analysis JJCAO.
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.
Copyright © Wondershare Software Introduction to Data Structures Prepared by: Eng. Ahmed & Mohamed Taha.
1 MT258 Computer Programming and Problem Solving Unit 9.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Data Structures Winter What is a Data Structure? A data structure is a method of organizing data. The study of data structures is particularly important.
Lecture No.01 Data Structures Dr. Sohail Aslam
Chapter 8 Data Abstractions Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
C++ Classes and Data Structures Jeffrey S. Childs
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Information and Computer Sciences University of Hawaii, Manoa
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Data Structure & Algorithm Lecture 1 – Selection & Insertion Sort JJCAO Steal some from Prof. Yoram Moses.
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
C++ STL CSCI 3110.
Data structures Abstract data types Java classes for Data structures and ADTs.
Data Structure Introduction.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
Elementary Data Organization. Outline  Data, Entity and Information  Primitive data types  Non primitive data Types  Data structure  Definition 
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory Example 2: A simple text editor –Maintain a sequence of lines.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
2/21/20161 List Operations Advanced Programming Ananda Gunawardena.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Data Structure and Algorithms
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the basic concepts of linked lists ❏ To introduce the basic concepts.
Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.
Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 8a. Sorting(1): Elementary Algorithms.
Click to edit Master text styles Stacks Data Structure.
Stacks Chapter 3 Objectives Upon completion you will be able to
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
LINKED LISTS.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
CSE 1342 Programming Concepts
Cpt S 122 – Data Structures Abstract Data Types
Data Structure By Amee Trivedi.
Data Structure Interview Question and Answers
UNIT-3 LINKED LIST.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Arrays and Linked Lists
A Robust Data Structure
Introduction to Data Structure
Data Structures & Algorithms
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
Presentation transcript:

Data Structure & Algorithm Lecture 2 – Basic Data Structure JJCAO Steal some from Prof. Yoram Moses

The Sorting Problem Example: Input: A sequence of n numbers < 𝑎 1 , 𝑎 2 , …, 𝑎 n > Output: A permutation (reordering) < 𝑏 1 , 𝑏 2 , …, 𝑏 n > of the input sequence such that 𝑏 1 ≤ 𝑏 2 …≤ 𝑏 n

Recitation Selection Sort 𝑂( 𝑛 2 ) Insertion Sort 𝑂( 𝑛 2 ) k=i for i = 1:n, k = i //invariant: a[k] smallest of a[i..n] for j = i+1:n if a[j] < a[k] then k = j //invariant: a[1..i] in final position swap a[i,k] end key = A[j]

How is Data Represented? Sort a sequences: Array? List? Heap? There are many options Efficiency of the algorithm also depends on the data structure used

Elementary Data Structures Arrays Lists Stacks Queues Trees “Mankind’s progress is measured by the number of things we can do without thinking.” In some languages or libs, some of them are the “off-the-shelf” components.

Components of Data Structure There are two aspects to any data structure: The abstract operations which it supports. (Abstract Data Types, ADT) The implementation of these operations. (Data Structure) Data structures such as array, list, stacks, …

Data Type vs. Data Structure Data Structure is the way an ADT is implemented. Must be distinguished from the ADT itself!

C++ Implementation of Stack

Data Structure vs. Algorithm Data Structures Represent objects of the ADT Algorithms Manipulate the data structures to implement a mission using the operations of the ADT

Contiguous vs. Linked Data Structures Data structures can be neatly classified as either contiguous or linked depending upon whether they are based on arrays or pointers: Contiguously-allocated structures are composed of single slabs of memory, and include arrays, matrices, heaps, and hash tables. Linked data structures are composed of multiple distinct chunks of memory bound together by pointers, and include lists, trees, and graph adjacency lists.

Array Array of integer Array of images An array is a number of data items of the same type arranged contiguously in memory. Fixed-size 𝑚_𝑛𝑆𝑖𝑧𝑒

Advantages of contiguously-allocated arrays Constant-time access given the index. Space efficiency - Arrays consist purely of data, so no space is wasted with links or other formatting information. Memory locality - Physical continuity (memory locality) between successive data accesses helps exploit the high-speed cache memory on modern computer architectures.

Dynamic Array Unfortunately we cannot adjust the size of simple arrays in the middle of a program’s execution. Start with an array of size 1, and double its size from m to 2m each time we run out of space. How many times will we double for n elements? Only log⁡(𝑛) 1 + 2 + 4 + 8 + … + 2^i =2^{i+1}=n => i = log(n)

How much total work The apparent waste in this procedure involves the recopying of the old contents on each expansion. If half the elements move once, a quarter of the elements twice, and so on, the total number of movements M is given by Thus each of the n elements move an average of only twice, and the total work of managing the dynamic array is the same O(n) as a simple array.

Remove an Element

Notes Practical implementations usually include more operations Initialization/Destruction “Luxury” operations: size() print() operators

Bubble Sort Bubble sort: beginning of first pass: 1. Compare two players. 2. If the one on the left is taller, swap them. 3. Move one position right. End of first pass

Bubble Sort for i = 1:n, swapped = false for j = n:i+1, if a[j] < a[j-1], swap a[j,j-1] swapped = true break if not swapped end Is there any problem?

Several Sort Algorithms http://www.sorting-algorithms.com http://www.sorting-algorithms.com

The Linked List Structures typedef struct list { item type item; struct list *next; } list; Pointers represent the address of a location in memory. A cell-phone number can be thought of as a pointer to its owner as they move about the planet.

Searching a List Searching in a linked list can be done iteratively or recursively. list *search_list(list *l, item type x) { if (l == NULL) return(NULL); if (l->item == x) return(l); else return( search_list(l->next, x) ); }

Insertion into a List Since we have no need to maintain the list in any particular order, we might as well insert each new item at the head. void insert_list(list **l, item_type x) { list *p = new list; p->item = x; p->next = *l; *l = p; } Note the **l, since the head element of the list changes.

Deleting from a List

Deleting from a List

Advantages of Linked Lists Overflow on linked structures can never occur unless the memory is actually full. 2. Insertions and deletions are simpler than for contiguous (array) lists. 3. With large records, moving pointers is easier and faster than moving the items themselves.

Various List

Stack last-in, first-out (LIFO) http://www.algolist.net/Data_structures/Stack

Stack ADT & std::vector comments create() constructor of vector creates empty stack bool isEmpty() empty() tells whether the stack s is empty push(Item e) push_back(Item e) put e on top of the stack s Item peek() Item back() returns topmost element in stack s pop() pop_back() removes topmost element from the stack s destroy() Deconstructor of vector destroys stack s reserve() push_back() pop_back() back() size() empty()

Stack Implementation Using a Linked List

Stack Example 1: Reversing a Word part trap

Stack Example 2: Delimiter Matching When compilers compile your code When you want to write a program to parse a math formula

Stack Example 2: Delimiter Matching A successful example: b(c[d]e) Character Read Stack Contents b ( c [ [( d ] e )

Queue first-in, first-out

Implementation of Queue

A Circular Queue

Homework 1 Basic Dynamic Array & Selection Sort Deadline: 22:00, Sep. 10, 2011