Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009.

Similar presentations


Presentation on theme: "Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009."— Presentation transcript:

1 Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009

2 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

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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:345 358 490 501 513 555 561 701 724 797 location:0 1 2 3 4 5 6 7 8 9 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

15 List implementation using an Array Example: suppose we begin with the following list: data:345 358 490 501 513 555 561 701 724 797 location:0 1 2 3 4 5 6 7 8 9 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:345 358 490 501 513 555 561 701 724 797 location:0 1 2 3 4 5 6 7 8 9 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:345 358 490 501 513 555 561 701 724 797 location:0 1 2 3 4 5 6 7 8 9 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

16 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:345 358 490 501 513 555 561 701 724 797 location:0 1 2 3 4 5 6 7 8 9 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:345 490 501 513 555 561 701 724 797 location:0 1 2 3 4 5 6 7 8 9 1.Reorganize the list: move the remaining elements. (space=9) data:345 490 501 513 555 561 701 724 797 ?(797) location:0 1 2 3 4 5 6 7 8 9 CSE 246 Data Structures and Algorithms Fall 2009 Lecture #116

17 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: 345 358 490 501 513 555 561 701 724 797 location:0 1 2 3 4 5 6 7 8 9 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: 345 358 490 501 513 555 561 701 724 797 location:0 1 2 3 4 5 6 7 8 9 10 3.Insert 505 data: 345 358 490 501 505 513 555 561 701 724 797 location:0 1 2 3 4 5 6 7 8 9 10 CSE 246 Data Structures and Algorithms Fall 2009 Lecture #117

18 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

19 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

20 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

21 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


Download ppt "Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009."

Similar presentations


Ads by Google