Document Sample

Lecture DS & Algorithms:09 Abstract Data Types Lecture DS & Algorithms:09 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. 2 Lecture DS & Algorithms:09 Abstract Data Types Def. a collection of related data items together with an associated set of operations 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. 3 Lecture DS & Algorithms:09 Abstract Data Types 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. 4 Lecture DS & Algorithms:09 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 5 Lecture DS & Algorithms:09 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 6 Lecture DS & Algorithms:09 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 7 Lecture DS & Algorithms:09 Array representation • [5, 2, 4, 8,1] • Some of the implementations can be location(i) = i 5 2 4 8 1 location(i) = 9- i 1 8 4 2 5 location(i) = (7+i)%10 8 1 5 2 4 8 Lecture DS & Algorithms:09 Simple Data Types Also known as built-in data types Lecture DS & Algorithms:09 Boolean data Data values: {false, true} In C/C++: false = 0, true = 1 (or nonzero) Operations: and && or || not ! && 0 1 || 0 1 0 0 0 x !x 0 0 1 1 0 1 0 1 1 1 1 1 0 10 Lecture DS & Algorithms:09 Character Data Store numeric codes (ASCII, EBCDIC, Unicode) 1 byte for ASCII and EBCDIC, 2 bytes for Unicode ASCII/EBCDIC Unicode Basic operation: comparison to determine if Equal, Less than, Greater than, etc. use their numeric value. , 11 Lecture DS & Algorithms:09 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 = 00000000010110002 Signed integer: Store in a fixed number w of bits using one of the following representations: 12 Lecture DS & Algorithms:09 Sign-magnitude representation Save one bit (usually most significant) for sign (0 = +, 1 = – ) Use base-two representation in the other bits. 88 _000000001011000 0 sign bit –88 1 _000000001011000 13 Lecture DS & Algorithms:09 Two's complement representation Same as sign mag. For nonnegative n: Use ordinary base-two representation with leading (sign) bit 0 For negative n (–n): (1) Find w-bit base-2 representation of n (2) Complement each bit. (3) Add 1 Example: –88 1. 88 as a 16-bit base-two number 0000000001011000 2. Complement this bit string 1111111110100111 3. Add 1 1111111110101000 14 Lecture DS & Algorithms:09 Array ADT Lecture DS & Algorithms:09 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] 16 Lecture DS & Algorithms:09 Declaring arrays in C++ element_type array_name[CAPACITY]; 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 Can't input the capacity, Why? score[0] The compiler reserves a block of “consecutive” memory score[1] locations, enough to hold CAPACITY values of type score[2] element_type. score[3] . . . . The elements (or positions) of the array are indexed 0, 1, 2, . . . . ., CAPACITY - 1. score[99] e.g., double score[100]; 17 Lecture DS & Algorithms:09 How well does C/C++ implement an array ADT? As an ADT In C++ ordered indices numbered 0, 1, 2, . . ., CAPACITY - 1 fixed size CAPACITY specifies the capacity of the array same type elements element_type is the type of elements direct access subscript operator [] 18 Lecture DS & Algorithms:09 Array Initialization In C++, arrays can be initialized when they are declared. an array literal Numeric arrays: element_type num_array[CAPACITY] = {list_of_initial_values}; Example: double rate[5] = {0.11, 0.13, 0.16, 0.18, 0.21}; 0 1 2 3 4 rate 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}; 0 1 2 3 4 rate 0.11 0.13 0.16 0 0 19 Lecture DS & Algorithms:09 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: char vowel[5] = {'A', 'E', 'I', 'O', 'U'}; 0 1 2 3 4 vowel A E I O U 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'}; 0 1 2 3 4 5 6 7 8 9 collegeName C a l v i n \0 \0 \0 \0 20 Lecture DS & Algorithms:09 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] score 0x1396 [0] be translated into a memory address: [1] [2] score[3] 0x1396 + 3 * sizeof (double) 0x13ae [3] . . = 0x1396 + 3 * 8 . . . . = 0x13BA [99] 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 What will be the an array element is accessed. time complexity 21 Lecture DS & Algorithms:09 Character arrays 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 *(array_name + index) * is the dereferencing operator *ref returns the contents of the memory location with address ref For example, the following statements of pseudocode are equivalent: print score[3] print *(score + 3) 22 Lecture DS & Algorithms:09 Operations • Traverse • Insert • Delete • Search – Linear Search – Binary Search • Sorting 23 Lecture DS & Algorithms:09 Traversing Linear Arrays Repeat for K=LB to UB Apply PROCESS to Array[K] [End of Loop] Exit 24 Lecture DS & Algorithms:09 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? 25 Lecture DS & Algorithms:09 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; } } } 26 Lecture DS & Algorithms:09 • 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; • } • } • } • } 27 Lecture DS & Algorithms:09 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 28 Lecture DS & Algorithms:09 Bubble Sort: Example • After one loop, the largest element is in the last location • Repeat the procedure 29 Lecture DS & Algorithms:09 Bubble Sort: Example • Now the two largest elements are at the end • Repeat again 30 Lecture DS & Algorithms:09 Bubble Sort: Example • With this loop, 12 is brought to its appropriate location 31 Lecture DS & Algorithms:09 Bubble Sort: Example • Finally, we swap the last two entries to order them • At this point, we have a sorted array 32 Lecture DS & Algorithms:09 Problems with arrays • Capacity can not be changed during program execution – Memory wastage – Out of range errors 33

DOCUMENT INFO

Shared By:

Categories:

Tags:

Stats:

views: | 3 |

posted: | 2/25/2012 |

language: | |

pages: | 33 |

OTHER DOCS BY kamrann123

Docstoc is the premier online destination to start and grow small businesses. It hosts the best quality and widest selection of professional documents (over 20 million) and resources including expert videos, articles and productivity tools to make every small business better.

Search or Browse for any specific document or resource you need for your business. Or explore our curated resources for Starting a Business, Growing a Business or for Professional Development.

Feel free to Contact Us with any questions you might have.