610 likes | 629 Views
Welcome to Data Structures course where you'll learn about data organization, algorithms, and sorting methods like Insertion Sort. Understand the basics of data representation and pseudocode for efficient problem-solving. Explore fundamental data structures like arrays, lists, and stacks. Dive into Abstract Data Types and elementary data structures to grasp the core concepts. Join us to enhance your understanding of how data is organized and utilized in computing.
E N D
7 5 6 2 4 null Welcome to Data StructuresSpring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks
Welcome to Data StructuresSpring 2008 • Instructor • Yael Moses (yael@idc.ac.il) office hours: Thu. 10:30-11:30 or by appointment • TA • Ran Eshel
Textbook • Cormen, Leiserson, Rivest and Stein Introduction to Algorithms. (CLRS) • You will need this book also next year (for Algorithms) and maybe for additional elective courses. • A Hebrew translation exists (by Open univ.) • You will really need to read!!
Student's Responsibilities: During the semester you will need to submit: • 6-7 theoretical assignments • 3 programming projects
Final Grade If final exam < 60 then grade = fail else grade = 0.2 *assignmnets + 0.8 * final exam. Warning: DS is (considered) a difficult course.
Today • What is data structure? • What is it good for? • Getting started • An overview of the course
Solving a problem Input manipulation output
input algorithm output
Algorithm Data{input} {data manipulated} Data{output} 2,1,12,7 1,2,7,12 Shimon
Solving a problem Data: information for analysis. e.g., numbers, words, songs, movies Algorithm: A well-defined procedure to solve a problem Data structure: the way the data is organized
The Sorting Problem Input: A sequence of nnumbers <a1,a2,..,an> Output: A permutation (reordering) of the input sequence <b1,b2,..,bn> such that b1≤b2… ≤bn Note: the sets {a1,a2,..,an}= {b1,b2,..,bn} Example: • input: <31,41,59,26,41,58> • output: <26,31,41,41,58,59>
Insertion Sort • Picking the elements 1by 1 from the sequence • For each element insert it into correct place in the sequence of already sorted elements • Until last element inserted into place
Insertion Sort 5 24 6 1 3 2 5 46 1 3 2 4 5 61 3 2 4 5 6 13 1 2 4 5 6 3 1 2 3 4 5 6
What is missing? • How the data (numbers) are represented: • Each number: binary • The sequence of numbers: ? • The operations required in the chosen representation? • Picking the elements 1 by 1 from the sequence • For each element insert it into correct place in the sequence of already sorted elements • Until last element inserted into place
Insertion Sort (use array) Insertion-Sort(A) for i2 to length[A] keyA[i] ji-1 while j>0 and A[j]>key A[j+1]A[j] jj-1 end A[j+1]key end What is the worse case?
Pseudocode • In the lectures I will be presenting algorithms in pseudocode. • This is very common in the computer science literature • Pseudocode is usually easily translated to real code. • Pseudocode should also be used for homework
Pseudocode • The algorithms you design in homework will be read by a person, not a computer • The No Code Rule: • Do not turn in Java or C code when asked for pseudocode • Explain algorithm precisely, but without all the details needed for a computer code
Pseudocode example (good) Insertion-Sort(A) for i2 to length[A] keyA[i] ji-1 while j>0 and A[j]>key A[j+1]A[j] jj-1 end A[j+1]key end
Java code public static void insertionSort (int[] array){ for (int j = 1; j < array.length; j++) { int key = array[j]; int k = j - 1; while(k >= 0 && array[k] > key) { array[k+1] = array[k]; k--; } array[k+1] = key; } }
What is a Data Structure? The way the data is organized • What do we mean by “way”? • The organization should allow an efficient use of the data
Abstract Data Type An abstract mathematical definition of objects, with operations defined on them
head F R 1 2 4 3 7 8 5 6 Elementary Data Structures • Arrays • Lists • Stacks • Queues • Trees In some languages these are basic data types – in others they need to be implemented
Examples • Basic Types • integer, real (floating point), boolean (0,1), character • Arrays • A[0..99] : integer array • A[0..99] : array of images 0 1 2 3 4 5 6 7 99 … A 2 1 3 3 2 9 9 6 10 … 0 1 2 99
ADT: Array A mapping from an index set, such as {0,1,2,…,n}, into a cell type Objects:set ofcells Operations: • create(A,n) • put(A,v,i) or A[i] v • value(A,i)
Simple Linked List Group data together in a linear and flexible way. Example: A B A C R Add anelement (Firsy in the list)
Examples of Lists • List of numbers • List of names • List of lists • List of arrays • Representing a sparse high order polynomial
ADT: Finite Dynamic Sets Objects: Sets of elements (e.g., integers) Operations: • Create a set • Insert an element • Remove an element • Check membership • Minimum • Maximum {5,1,2,2…,3}
Data Structures • Representation of objects of an Abstract Data Type (ADT) • Algorithmsmanipulate the data structures to implement the operationsof the ADT ADT: An abstract mathematical definition of objects, with operations defined on them
ADT: Basic Linked List Objects: Finite sequences of nodes Operations: • Create • On a list • Get the first node • On a node • Get data • Get next node • Assign value to data • Assign next node A B A nil
Basic List Operations: in pseudo code • head[L] - the first node of L (in Java: L.head) • next[x] - the next node in L or NIL if x is the tail of L (in Java: x.next) • data[x] – the node data (in Java: x.data)
More list operations Low level: • List-Search(L,k) • List-head-Insert(L,x) • Insert-after-key(L,k,x) • List-tail-Insert(L,x) • List-Delete(L,k) Higher level: • Sort(L) • Reverse(L) Can be implemented using the basic list operations
Searching List-Search(L,k) x head[L] while (xNIL) and (data[x]k) do xnext[x] return(x)
Inserting into the head List-head-Insert(L,x) next[x] head[L] head[L] x
Insert after a key k L node node Value Next Value Next NULL k Value v Next Insert the value v after P
Insert after a key Insert-after-key(L,k,x) y List-Search(L,k) if yNIL next[x] next[y] next[y] x end
Linked List Delete a key L node node Value Next Next Value NULL Value Next k
node Next Value NULL Q Linked List Delete a key L node Value Next To delete the node pointed to by Q, need a pointer to the previous node;
Linked List Delete L node node Value Next Next Value NULL prev_x k
Deleting a key List-Delete(L,k) x head[L] prev_x= head[L] while (xNIL) and (data[x]k) do prev_xx xnext[x] end if prev_xNIL next[prev_x] next[x] end
Doubly Linked Lists • As mentioned, for delete we need ‘findPrevious’. This is a slow [O(N)] function - because we cannot go directly to previous node • Solution: Keep a "previous" pointer at each node. head prev prev prev
Double Link Pros and Cons • Advantage • Delete (not DeleteAfter) and FindPrev are faster • Disadvantages: • More space used up (double the number of pointers at each node) • More book-keeping for updating the two pointers at each node (pretty negligible overhead)
9 8 10 6 9 8 10 6 Insertion Sort Using Linked Lists 9 8 10 6
Data Structure: linked list implementation • Using records and pointers • Using arrays
Pointer Implementation Basic Idea • Data: For each node allocate memory • Pointer: Keep a pointer to the memory location. L node node Value Next Value Next NULL
Records A record (also called a struct, similar toobject) • Group data together that are related • To access the fields we use “dot” notation. Example:x: name and image Elton John name image Example: x.name x.image
Pointer • A pointer is a reference to a variable or record (or to an object in Java world). • In C, if X is of type pointer to Y then *X is of type Y – same for pseudocode Example: x : record pointer *x record
A node: a record which contains a pointer • Group data and a pointer • To access the fields we use Example:x : complex number Name: text Image: image next: node reference (a pointer) Example:x.name x.image x.next name[x] image [x] next[x] OR
Simple Linked List A linked list • Group data together in a flexible, dynamic way. L : node pointer 4 9 13 20 record node : ( data : integer (or any other structure) next : node pointer )
Pointer Implementation Issues • Whenever you break a list, your code should fix the list up as soon as possible • Draw pictures of the list to visualize what needs to be done • Pay special attention to boundary conditions: • Empty list • Single item – same item is both first and last • Two items – first, last, but no middle items • Three or more items – first, last, and middle items