Lecture DS & Algorithms:09 Abstract Data Types. Lecture DS & Algorithms:09 2 Abstract Data Types Data Type: A data type is a collection of values and.

Slides:



Advertisements
Similar presentations
Linear Lists – Array Representation
Advertisements

One Dimensional Arrays
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Programming with Collections Collections in Java Using Arrays Week 9.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
8 November Forms and JavaScript. Types of Inputs Radio Buttons (select one of a list) Checkbox (select as many as wanted) Text inputs (user types text)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Data types and variables
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
String Escape Sequences
‘C’ LANGUAGE PRESENTATION.  C language was introduced by Dennis Ritchie..  It is a programming language, which can make a interaction between user and.
Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Objectives You should be able to describe: Data Types
CPS120: Introduction to Computer Science Lecture 8.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Summer 2014 Chapter 1: Basic Concepts. Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Chapter Overview Welcome to Assembly Language.
Assembly Language for x86 Processors 7th Edition
Input & Output: Console
Computer Science 111 Fundamentals of Programming I Number Systems.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Chapter 8 Arrays and Strings
CPS120: Introduction to Computer Science
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Pointers OVERVIEW.
Chapter 19 Number Systems. Irvine, Kip R. Assembly Language for Intel-Based Computers, Translating Languages English: Display the sum of A times.
1 Intro to Data Structures and ADTs Chapter 2. 2 Goal of Data Structures Organize data Facilitate efficient … –storage –retrieval –manipulation Select.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
CSC 107 – Programming For Science. The Week’s Goal.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009.
Data Structure Introduction.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CPS120: Introduction to Computer Science Variables and Constants.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Data Structure and Algorithm: CIT231 Lecture 3: Arrays and ADT DeSiaMore DeSiaMorewww.desiamore.com/ifm1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
Windows Programming Lecture 03. Pointers and Arrays.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
List Representation - Array
Introduction to Abstract Data Types
Review of Arrays and Pointers
Data Structures (CS212D) Week # 2: Arrays.
Chapter 2: Introduction to C++.
Introduction to Data Structure
Abstract Data Types, Elementary Data Structures and Arrays
Intro to Data Structures and ADTs
Presentation transcript:

Lecture DS & Algorithms:09 Abstract Data Types

Lecture DS & Algorithms:09 2 Abstract Data Types Data Type: A data type is a collection of values and a set of operations on those values. The collection and operations form a mathematical construct An ADT refers to the mathematical concept that defines the data type Each ADT operation is defined by its inputs and outputs.

Lecture DS & Algorithms:09 3 Def. e.g. whole numbers (integers) and arithmetic operators for addition, subtraction, multiplication and division. e.g. Flight reservation Basic operations: find empty seat, reserve a seat, cancel a seat assignment Why "abstract?" Data, operations, and relations are studied independent of implementation. What not how is the focus. a collection of related data items together with an associated set of operations Abstract Data Types

Lecture DS & Algorithms:09 4 Def.Consists of storage structures (data structures) to store the data items and algorithms for the basic operations. The storage structures/data structures used in implementations are provided in a language (primitive or built-in) or are built from the language constructs (user-defined). In either case, successful software design uses data abstraction: Separating the definition of a data type from its implementation. Abstract Data Types

Lecture DS & Algorithms:09 5 Separation of interface and implementation Think of ADT as a black box ADT is represented by an interface and implementation is hidden from the user –This means that the ADT can be implemented in various ways, as long as it adheres to interface –For example, a ListADT can be represented using an array based implementation or a linked list implementation

Lecture DS & Algorithms:09 6 Linear list data structure Def: An ordered collection of elements –some examples are an alphabetized list of students, a list of gold medal winners ordered by year, etc. With these examples in mind, we feel the need to perform the following operations on a linear list –Determine whether the list is empty –Determine the size of list –Find the element with a given index –Find the index of a given element –Delete an element given its index –Insert a new element

Lecture DS & Algorithms:09 7 ADT - linearList The ADT specification is independent of any representation and programming language AbstractDataType linearList { elements ordered finite collection of zero or more elements operations empty(): return true if the list is empty size(): return the list size get(index): return the indexed element indexOf(x): return the index of the first occurance of x in the list, returns -1, if x is not in the list erase(index): delete the indexth element, element with higher index have their index reduced by 1 insert(index, x): insert x as the indexth element output(): output the list elements from left to right

Lecture DS & Algorithms:09 8 Array representation [5, 2, 4, 8,1] Some of the implementations can be location(i) = i location(i) = 9- i location(i) = (7+i)%10

Lecture DS & Algorithms:09 Simple Data Types Also known as built-in data types

Lecture DS & Algorithms:09 10 Boolean data Data values: { false, true } In C/C++: false = 0, true = 1 (or nonzero) Operations: and && or || not ! x !x && |

Lecture DS & Algorithms:09 11 Character Data Store numeric codes (ASCII, EBCDIC, Unicode) 1 byte for ASCII and EBCDIC, 2 bytes for Unicode Basic operation: comparison to determine if Equal, Less than, Greater than, etc. use their numeric value., ASCII/EBCDIC Unicode

Lecture DS & Algorithms:09 12 Integer Data Non-negative (unsigned) integer: Store its base-two representation in a fixed number w of bits (e.g., w = 16 or w = 32) 88 = Signed integer: Store in a fixed number w of bits using one of the following representations:

Lecture DS & Algorithms:09 13 Sign-magnitude representation Save one bit (usually most significant) for sign (0 = +, 1 = – ) Use base-two representation in the other bits. 88  _  sign bit –88  _ 11

Lecture DS & Algorithms:09 14 Two's complement representation For negative n (–n): (1) Find w-bit base-2 representation of n (2) Complement each bit. (3) Add 1 Example: – as a 16-bit base-two number Same as sign mag. For nonnegative n: Use ordinary base-two representation with leading (sign) bit 0 2. Complement this bit string 3. Add

Lecture DS & Algorithms:09 Array ADT

Lecture DS & Algorithms:09 16 Linear Arrays A linear array is a finite number N homogeneous data elements –Elements are referenced respectively by an index set of N consecutive numbers –Elements are stored respectively in successive memory locations –Fixed number of elements Index always integer Length=UB-LB+1 Notation A1 or A(1) or A[1]

Lecture DS & Algorithms:09 17 Declaring arrays in C++ where element_type is any type array_name is the name of the array — any valid identifier CAPACITY (a positive integer constant ) is the number of elements in the array score[0] score[1] score[2] score[3] score[99] element_type array_name[CAPACITY]; e.g., double score[100]; The elements (or positions) of the array are indexed 0, 1, 2,..., CAPACITY - 1. Can't input the capacity, Why? The compiler reserves a block of “consecutive” memory locations, enough to hold CAPACITY values of type element_type.

Lecture DS & Algorithms:09 18 indices numbered 0, 1, 2,..., CAPACITY - 1 How well does C/C++ implement an array ADT? As an ADTIn C++ ordered fixed size same type elements direct access element_type is the type of elements CAPACITY specifies the capacity of the array subscript operator []

Lecture DS & Algorithms:09 19 an array literal Array Initialization Example: double rate[5] = {0.11, 0.13, 0.16, 0.18, 0.21}; Note 1: If fewer values supplied than array's capacity, remaining elements assigned 0. double rate[5] = {0.11, 0.13, 0.16}; In C++, arrays can be initialized when they are declared. Numeric arrays: element_type num_array[CAPACITY] = {list_of_initial_values};

Lecture DS & Algorithms:09 20 Note 1: If fewer values are supplied than the declared size of the array, the zeroes used to fill un-initialized elements are interpreted as the null character '\0' whose ASCII code is 0. const int NAME_LENGTH = 10; char collegeName[NAME_LENGTH]={'C', 'a', 'l', 'v', 'i', 'n'}; char vowel[5] = {'A', 'E', 'I', 'O', 'U'}; Character Arrays Character arrays may be initialized in the same manner as numeric arrays. declares vowel to be an array of 5 characters and initializes it as follows:

Lecture DS & Algorithms:09 21 The contents of the memory word with this address 0x13BA can then be retrieved and displayed. An address translation like this is carried out each time an array element is accessed. What will be the time complexity Addresses When an array is declared, the address of the first byte (or word) in the block of memory associated with the array is called the base address of the array. Each array reference must be translated into an offset from this base address. For example, if each element of array score will be stored in 8 bytes and the base address of score is 0x1396. A statement such as cout << score[3] << endl; requires that array reference score[3] be translated into a memory address: 0x * sizeof (double ) = 0x * 8 = 0x13BA score[3]  [0] [1] [2] [3] [99] score  0x1396 0x13ae

Lecture DS & Algorithms:09 22 The value of array_name is actually the base address of array_name array_name + index is the address of array_name[index]. An array reference array_name[index] is equivalent to For example, the following statements of pseudocode are equivalent: print score[3] print *(score + 3) * is the dereferencing operator *ref returns the contents of the memory location with address ref *(array_name + index) Character arrays

Lecture DS & Algorithms:09 23 Operations Traverse Insert Delete Search –Linear Search –Binary Search Sorting

Lecture DS & Algorithms:09 24 Traversing Linear Arrays Repeat for K=LB to UB Apply PROCESS to Array[K] [End of Loop] Exit

Lecture DS & Algorithms:09 25 Sorting BUBBLE(DATA,N) 1. Repeat Steps 2 and 3 for K=1 to N-1 2. Set PTR:=1 3. Repeat while PTR<=N-K a) if DATA[PTR]>DATA[PTR+1] then: Swap DATA[PTR] and DATA [PTR+1] b) Set PTR:=PTR+1 4. Exit Complexity of Bubble Sort? n(n-1)/2 + O(n)=? Can you see any problem? How to make bubble sort efficient?

Lecture DS & Algorithms:09 26 Bubble Sort Source Code for(x = 0; x < n; x++) { for(y = 0; y < n-1; y++) { if(array[y] > array[y+1]) { temp = array[y+1]; array[y+1] = array[y]; array[y] = temp; }

Lecture DS & Algorithms:09 27 void bubbleSort(int numbers[], int array_size) { int i, j, temp; for (i = (n - 1); i >= 0; i--) { for (j = 1; j <= i; j++) { if (numbers[j-1] > numbers[j]) { temp = numbers[j-1]; numbers[j-1] = numbers[j]; numbers[j] = temp; }

Lecture DS & Algorithms:09 28 Bubble Sort: Example Consider the unsorted array to the right We start with the element in the first location, and move forward: –if the current and next items are in order, continue with the next item, otherwise –swap the two entries

Lecture DS & Algorithms:09 29 Bubble Sort: Example After one loop, the largest element is in the last location Repeat the procedure

Lecture DS & Algorithms:09 30 Bubble Sort: Example Now the two largest elements are at the end Repeat again

Lecture DS & Algorithms:09 31 Bubble Sort: Example With this loop, 12 is brought to its appropriate location

Lecture DS & Algorithms:09 32 Bubble Sort: Example Finally, we swap the last two entries to order them At this point, we have a sorted array

Lecture DS & Algorithms:09 33 Problems with arrays Capacity can not be changed during program execution –Memory wastage –Out of range errors