CSE 230 Computer Science II (Data Structure) Introduction Fall 2017 Stony Brook University Instructor: Shebuti Rayana
Basic Terminologies Data types Data structure Phases of S/W development Specification Design Ø Algorithm Ø Pseudocode Implementation Ø Iterations Analysis of Complexity Shebuti Rayana (CS, Stony Brook University) 2
Data Types Intuitively, data can be of a certain type 1, 2, are INTEGER type a, b, are CHARACTER type ant, antelope, are STRING type Some basic data types are called primitive data types In Java, these can be (among others) int, char, boolean etc. Composite data types can be constructed using the primitive or non-primitive types. Example: an Array is a composite data type that stores a sequence of elements in a particular order. Shebuti Rayana (CS, Stony Brook University) 3
Abstract Data Types Mathematical specifications for data types Defined by their, (i) possible values (ii) possible operations on them and (iii) behavior of the operations Theoretical concept, may not have an actual implementation In reality, though, many abstract data types have implementation that captures their behavior Example: The abstract data type Set, it has proper mathematical behavior for union, intersection, checking membership, etc. Shebuti Rayana (CS, Stony Brook University) 4
What is a Data Structure? A data structure is a collection of data, organized so that items can be stored and retrieved by some fixed techniques. A data structure is an actual code implementation of abstract data types Example: a Java array is an implementation of a collection of elements, each identified by its location in the collection. In most programming languages this location is given by the index, denoted by A[0], A[1], etc. Shebuti Rayana (CS, Stony Brook University) 5
Data Structures In this course, we will learn to Implement many composite abstract data types using complex data structures (stacks, queues, binary trees, ) Use them in several algorithms to solve concrete computational problems (like sorting algorithms) See how the correct (or incorrect) use of data structures effect these Algorithms e.g., how the right use of a data structures can make an algorithm efficient. Learn how a data structure can be suitable for some algorithms but not suitable for others e.g., arrays can access any element really fast (good for searching algorithms) but an array can only hold elements of one data type (bad for mapping key-value pairs) Shebuti Rayana (CS, Stony Brook University) 6
Phases of S/W Development Problem statement Specification of the task Design of a solution Implementation (coding) of the solution Analysis of the solution Testing and debugging Documentation and Maintenance Obsolescence These phases do not necessarily occur one after another, in practice, they blur into each other Shebuti Rayana (CS, Stony Brook University) 7
Specification The specification is a precise description of the problem Example Problem: Sort a sequence of integers in ascending order Given a sequence of numbers as input (24, 10, 32, 19, 27, 39) Output the same numbers in ascending/increasing order (10, 19, 24, 27, 32, 39) For small problems sometimes a sample of the input and desired output is precise enough Shebuti Rayana (CS, Stony Brook University) 8
Specification of a Java Method Short Introduction Short description of the action of the method Parameter Description List of method s parameters Precondition Condition that is supposed to be true when the method is called Postcondition or the return condition Condition that is true when the method finishes Throwing exception When the precondition fails the method throws exception Shebuti Rayana (CS, Stony Brook University) 9
Example: Specification Sort a sequence of integers and return the sorted sequence public static int[] insertionsort(int [] a) Parameter: an array a containing the unsorted integers Precondition: array not empty Postcondition: returns the reference of a integer typed array a Throws: IllegalArgumentException Indicates the array is empty Shebuti Rayana (CS, Stony Brook University) 10
Design The design phase consists of formulating the steps (or algorithm) to solve the problem Algorithm: Informally, an algorithm is a welldefined computational procedure that takes some value (or values) as input and produces some value (or values) as output It is a sequence of computational steps or instructions that transform the input into the output transformation Output Input Shebuti Rayana (CS, Stony Brook University) 11
Pseudocode Algorithms are sometimes represented in a mixture of English and programming language, which is called Pseudocode. Not real code A high-level description of an algorithm that is Clear Concise Human-Readable Resembles (but is not) actual code. Shebuti Rayana (CS, Stony Brook University) 12
Example Algorithm Sorting Algorithm Sort a sequence of integer values in ascending order Given a sequence of numbers as input (24, 10, 32, 19, 27, 39) Output the same numbers in ascending/increasing order (10, 19, 24, 27, 32, 39) Shebuti Rayana (CS, Stony Brook University) 13
Example: Insertion Sort This is the method people normally use for sorting cards in hand 1. Start with an empty left hand (and the cards are face down table) on the 2. Remove one card at a time from the table and insert it into the correct position in the left hand. I. To find the correct position for a card, compare it with each of the cards already in the hand from left to right Shebuti Rayana (CS, Stony Brook University) 14
Example: Insertion Sort This is the method people normally use for sorting cards in hand Shebuti Rayana (CS, Stony Brook University) 15
Design: Subroutine As algorithms get more complex, the programming, too, gets more complicated. It becomes necessary to break down a single algorithm into smaller chunks. This allows us to organize, manage and debug even extremely long programs. These smaller chunks are called subroutines. Functions are subroutines that return values. Subroutines that, in turn, call themselves, are called recursive subroutine. Recursion allows us to write complex algorithms with shorter code In this course, we will study recursion to solve many complex problems Shebuti Rayana (CS, Stony Brook University) 16
Implementation The implementation is the actual code to carry out the design Java code for insertion sort Shebuti Rayana (CS, Stony Brook University) 17
Iteration Iterations are ways of moving through collections of statements Example: for( ) { } while( ){ } foreach() Shebuti Rayana (CS, Stony Brook University) 18
Complexity The complexity of an algorithm can be thought of as its running time. The running time of an algorithm on a particular input is the number of basic (?) operations needed for the algorithm to transform the input into the required output. The running time of the whole algorithm is the sum of the running times of each statement. Let s go back to insertion sort algorithm... Shebuti Rayana (CS, Stony Brook University) 19
Running Time of Insertion Sort Let n be the length of array A Shebuti Rayana (CS, Stony Brook University) 20
Running Time of Insertion Sort n n - 1 n - 1?? n - 1 Let n be the length of array A Shebuti Rayana (CS, Stony Brook University) 21
Running Time of Insertion Sort n n - 1 n - 1?? n - 1 Even for input of a given fixed size n, the running time depends on which input of size n is given. The number of times the while-loop must be executed depends on the values in the array! Shebuti Rayana (CS, Stony Brook University) 22
Running Time of Insertion Sort The running time of the algorithm is the sum of running times for each statement executed Shebuti Rayana (CS, Stony Brook University) 23
Running Time of Insertion Sort To compute T(n), the running time of INSERTION-SORT on an input of n values, we sum the products of the cost and times columns, obtaining In next class, we will learn how to represent this running time T(n) with Big O Notation Shebuti Rayana (CS, Stony Brook University) 24
Thank You Shebuti Rayana (CS, Stony Brook University) 25