400 likes | 418 Views
Learn why studying data structures is crucial for designing efficient and well-structured algorithms. Explore different data structures, their implementation in programming languages, and how to choose the right one for your application.
E N D
09 | 20090317Programming Fundamentals #1Fundamental Data Structures Fazmah Arif Yulianto CS1013 – PengantarTeknikInformatika - 20082
Why studying data structures? • Heart of software design • choosing a proper data structure • designing a correct, efficient, and well-structured algorithm
So, what is data structure? • A data structure is a way of organizing input data and operations which can be performed on this data (e.g. add, delete, find an element). • a way of storing data in a computer so that it can be used efficiently ( efficient algorithm)
The choice & implementation • The choice of the data structure often begins from the choice of an abstract data type • Data structures are implemented by a programming language as data types and the references and operations they provide.
Data structure or algorithm first? • DS first: • After the data structures are chosen, the algorithms to be used often become relatively obvious. • Alg first: • Data structures are chosen because certain key tasks have algorithms that work best with particular data structures. • In either case, the choice of appropriate data structures is crucial
Programming Language Features • Most languages feature some sort of module system, allowing data structures to be safely reused in different applications by hiding their verified implementation details behind controlled interfaces. • OOPL: C++ and Java in particular use classes • standard libraries and APIs: C++'s containers, Java Collections Framework, Microsoft .NET Framework.
Abstract Data Types • a specification of a set of data and the set of operations that can be performed on the data. • Abstractindependent of various concrete implementations
List of data structures • Base DS • Primitive types: Boolean, Character, Integer, String, Double, Float,... • Composite types: Struct, Bit field, Union, Gap Buffer,... • Linear DS • List: array, linked-list, buffer (stack, queue,...), ... • Associative array: hash table, ... • Non-linear DS • Graph • Tree
Problems • 8 bits unsigned integer • 200 + 100 = ? • 50 x 9 = ? • 22 / 7 = ? • 8 bits signed integer • -100 – 50 = ?
Finite-precision numbers • Problems: • Overflow error • Underflow error • Round-off error • ...
Numeric data: fixed-point • a real data type for a number that has a fixed number of digits after (and sometimes also before) the radix point (e.g., after the decimal point '.' in English decimal notation). • M.F Limited precision • Useful: • the executing processor has no floating point unit (FPU) • fixed-point provides improved performance or accuracy for the application at hand.
Some uses of fixed-point • Tremor and Toast: software libraries that decode the OggVorbis and GSM Full Rate audio formats respectively. • many audio decoding HW do not have an FPU (partly to save money, but primarily to save power - integer units are much smaller in silicon area than an FPU) • a SW implementation of floating-point on low-speed devices would not produce output in real time. • All 3D graphics engines on Sony's original PlayStation, Sega's Saturn, Nintendo's Game Boy Advance (only 2D) and Nintendo DS (2D and 3D) video game systems • to gain throughput on an architecture without an FPU.
Two’s complement • the value obtained by subtracting the number from a large power of two • from 2N for an N-bit two's complement • (r-1)’s complement of a number is determined by the formula: N*=rn – r -m –N
Problems • multiplying two fixed point numbers • format: I integer bits, and Q fractional bits • the answer: could have up to 2×I integer bits, and 2×Q fractional bits • keeping the middle bits: • the I-number of least significant integer bits, and the Q-number of most significant fractional bits • Fractional bits lost a precision loss => OK • Integer bits lost overflow => • Overflow flag
Numeric data: foating-point • the radix point can "float": that is, it can be placed anywhere relative to the significant digits of the number • a computer realization of scientific notation • 7 significant digits : • Fixed-point: 12345.67 • Floating-point: 1.234567, 123456.7, 0.00001234567, 1234567000000000 • IEEE 754-2008: The IEEE Standard for Floating-Point Arithmetic
IEEE 754-2008 • Finite numbers • Binary & decimal • (−1)s × c × bq • s sign • c must be an integer in the range zero through bp−1 • q must be an integer such that 1−emax ≤ q+p−1 ≤ emax
IEEE 754 Basic formats • In C: • binary32 ‘float’ • binary64 ‘double’
Rounding errors • the difference between the calculated approximation of a number and its exact mathematical value
Rounding in IEEE standard • truncation: chop off the remaining digits • 0.142857 ≈ 0.142 (dropping all significant digits after 3rd) • round to nearest: round to the nearest value; round up or round down. • 0.142857 ≈ 0.143 (rounding the 4th significant digit. This is rounded up because 8≥5) • 0.142857 ≈ 0.14 (rounding the 3rd significant digit. This is rounded down because 2<5) • round to -∞: always round to the left on the number line • round to ∞: always round to the right on the number line
Character • a unit of information that roughly corresponds to a symbol, in the written form of a natural language. • Exp: a letter, numeral, punctuationmark, control characters (used to process text in one or more languages, exp: carriage return, tab etc.) • character encoding that assigns each character to an integer quantity represented by a sequence of bits • ASCII • UTF-8
String • In most languages, a string is equivalent to an array of characters or code units • Java treats them as distinct types: • java.lang.String; char[] • In C: • Exp of character: 'A‘, '4‘, '$‘, '\t' (tab character) • Exp of string: "A“, "Hello World“, “6 years old" • C-string: stored implicitly by using a special terminating character • P-string: stored explicitly, for example by prefixing the string with the length as a byte value
Composite types • datatypes which can be constructed in a programming language out of that language's basic primitive types and other composite types • Struct in C struct Account { intaccount_number; char *first_name; char *last_name; float balance; }; • to create new var: struct Account myAccount; • to access: myAccount.account_number
Arrays • is a data structure consisting of a group of elements that are accessed by indexing • In most PL each element has the same data type and the array occupies a contiguous area of storage • Multidimensional array • Static vs dynamic array
Pointers • data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address • Pointers to: data, functions • allow largely unprotected access to memory addresses be careful
Exp: pointer in C inta = 5; int *money = NULL; money = &a; *money = 8;
Linked structures • Singly-linked list • Doubly-linked list • Circularly-linked list
Singly-linked list record Node { data // The data being stored in the node next // A reference to the next node, null for last node } recordList { Node firstNode// points to first node of list; null for empty list }
List traversal node := list.firstNode whilenode not null { (do something with node.data) node := node.next }
Stack • an ADT and data structure based on the principle of Last In First Out (LIFO)
Queue • a First-In-First-Out (FIFO) data structure • Operations: • addition of entities to the rear terminal position • removal of entities from the front terminal position • the queue performs the function of a buffer.
Queue implementations • Sequential • Circular • Linked queue
Hash table • a data structure that associates keys with values • Lookup function: • given a key (e.g., a person's name), find the corresponding value (e.g., that person's telephone number).
Lookup • It works by transforming the key using a hash function into a hash, a number that is used as an index in an array to locate the desired location ("bucket") where the values should be