Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009
Introduction to Data Structures In computer science, a data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently. For Example ◦ Imagine that you are hired by company XYZ to organize all of their records into a computer database. ◦ The first thing you are asked to do is create a database of names with all the company's management and employees. CSE 246 Data Structures and Algorithms Fall 2009 Lecture #1 2
Introduction You also want your database to represent the relationships between management and employees at XYZ These two diagrams are examples of different data structures This is very useful for keeping the names of the employees in alphabetical order so that we can locate the employee's record very quickly. However, this structure is not very useful for showing the relationships between employees. A tree structure is much better suited for this purpose. CSE 246 Data Structures and Algorithms Fall 2009 Lecture #13
Introduction During this course, you will learn how data structures are created inside a computer. You will find there is quite a difference between your mental picture of a data structure and the actual way a computer stores a data structure in memory. You will also discover that there are many different ways of creating the same data structure in a computer. These various approaches are tradeoffs that programmers must consider when writing software. CSE 246 Data Structures and Algorithms Fall 2009 Lecture #14
Computer Memory Every piece of data that is stored in a computer is kept in a memory cell with a specific address. We can think of these memory cells as being a long row of boxes where each box is labeled with an address. Computer memory is linear. The computer can store many different types of data in its memory. These include basic data types integers, float and characters etc. Once the computer stores data in the memory cells, it can access the data by using the address of the data cells. CSE 246 Data Structures and Algorithms Fall 2009 Lecture #15
Computer Memory For example, consider the following instructions for adding two integers together. For example, suppose we want a data structure that can store a group of characters as a word. In many computer languages, this data structure is called a string. If we store the characters for the string 'apple' in the computer's memory CSE 246 Data Structures and Algorithms Fall 2009 Lecture #16
Computer Memory But what happens when we try to represent our tree diagram of company XYZ? It doesn't make sense to store the names one after the other because the tree is not linear. Now we have a problem. We want to represent a nonlinear data structure using computer memory that is linear CSE 246 Data Structures and Algorithms Fall 2009 Lecture #17
Elementary Data structures Elementary Data Structure are fundamental approaches to organizing data. These are the building blocks that will be used to implement more complex Abstract Data Types. 1. Scalar (built-in) data types 2. Arrays 3. Linked Lists 4. Strings CSE 246 Data Structures and Algorithms Fall2009 Lecture #18
Arrays Data structures that store a large collection of data and allow direct access to the elements by using an index. ◦ Have a fixed length. A programmer must specify the size of an array at the point of its declaration. ◦ Applications often need storage structures, which grow and contract as data is added and removed. CSE 246 Data Structures and Algorithms Fall2009 Lecture #19
Array List Indexing features of an array Created with initial capacity that designates the available space to hold elements. Ability to grow dynamically to meet the runtime needs of a program CSE 246 Data Structures and Algorithms Fall2009 Lecture #110
Array List Not an efficient storage structure for general insertion and deletion of items at an arbitrary position in a list. ◦ Insert requires shifting a block of elements to the right to make room for a new item. ◦ Deletion requires shifting a block of elements to the left to fill in the gap created by the missing item. CSE 246 Data Structures and Algorithms Fall2009 Lecture #111
Data Structures and Algorithms Data structures have operations that manipulate the data. The design and implementation of these operations involve algorithms that consist of a finite sequence of instructions to perform a task. CSE 246 Data Structures and Algorithms Fall2009 Lecture #112
Data structures and Algorithms There is a famous saying by the renowned teacher and scholar, Nicolas Wirth that “Algorithms + Data Structures = Programs” (Wirth) “For many applications, the choice of the proper data structure is the only major decision involving the implementation: once the choice is made, the necessary algorithms are simple.” (Sedgewick) CSE 246 Data Structures and Algorithms Fall 2009 Lecture #113
Data Structure Suppose we have a list of sorted data on which we have to perform the following operations: ◦ Search for an item ◦ delete a specified item ◦ insert (add) a specified item Example: suppose we begin with the following list: data: location: What is a list? ◦ A list is a data structure where data is represented linearly. ◦ Finite sequence of items from the same data type ◦ If arrays are used, items are stored contiguously in the memory CSE 246 Data Structures and Algorithms Fall 2009 Lecture #114
List implementation using an Array Example: suppose we begin with the following list: data: location: Now, delete item 358 from the above list Q:What is the algorithm to delete an item? Q: What is the cost of deleting an item? data: location: Q: When we delete 358, what happens to that location? Now, add item 498 onto the above list Q:Where would that item go? data: location: Q: What is the cost of inserting an item? Conclusion: Using a list representation of data, what is the overall efficiency of searching, adding, and deleting items? CSE 246 Data Structures and Algorithms Fall 2009 Lecture #1 15
Deletion of an Element from a List Algorithm: 1.locate the element in the list (this involves searching) 2.delete the element 3.reorganize the list and index Example: data: location: Delete 358 from the above list: 1.Locate 358:if we use ‘linear search’, we’ll compare 358 with each element of the list starting from the location 0. 2.Delete 358: remove it from the list (space=10) data: location: Reorganize the list: move the remaining elements. (space=9) data: ?(797) location: CSE 246 Data Structures and Algorithms Fall 2009 Lecture #116
Insertion of an Element in List Algorithm: 1.locate the position where the element in to be inserted (position may be user-specified in case of an unsorted list or may be decided by search for a sorted list) 2.reorganize the list and create an ‘empty’ slot 3.insert the element Example: (sorted list) data: location: Insert 505 onto the above list: 1.Locate the appropriate position by performing a binary search. 505 should be stored in location 4. 2.Create an ‘empty’ slot data: location: Insert 505 data: location: CSE 246 Data Structures and Algorithms Fall 2009 Lecture #117
Methods for defining a collection of objects Array ◦ successive items locate a fixed distance disadvantage ◦ data movements during insertion and deletion ◦ waste space in storing n ordered lists of varying size possible solution ◦ linked list ◦ linked lists are dynamically allocated and make extensive use of pointers CSE 246 Data Structures and Algorithms Fall 2009 Lecture #118
Lab # 01 Task 1: Find max number from array list of 10 item. Find total number of comparisons need to perform this task. CSE 246 Data Structures and Algorithms Fall 2009 Lecture #119
Lab # 01 Design a program that records and reports the weekly sales amounts for the salespeople of a company. Each of the n salespeople is assigned a unique identification number in the range of 1 to n. The program prompts the user to enter the number of salespeople, and it uses that value to create an array of sales amount. CSE 246 Data Structures and Algorithms Fall2009 Lecture #120
Sample code import java.util.Scanner; public static void main(String args[]) { Scanner input = new Scanner(System.in); System.out.print("Enter TWO dates: "); String myStr = ""; String myStr2 = ""; while (input.hasNext()) { myStr = input.next(); myStr2 = myStr2 + myStr; } input.close(); System.out.print("You entered:" + myStr2); } } CSE 246 Data Structures and Algorithms Fall2009 Lecture #121