150 likes | 302 Views
CSE 246 Data Structures and Algorithms. Spring2012 Lecture#2. Introduction. 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
E N D
CSE 246Data Structures and Algorithms Spring2012 Lecture#2
Introduction • 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. Quratulain Rajput
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 a software. Quratulain Rajput
Why need Data structures • In computer systems we do not have the option of using or not using data structure. Computers has to store and use data. Computers cannot do without data, and wherever data exists it must have some kind of data structure. • Data structures helps to organized dataefficiently. • Data structures can support to increase efficiency of a program without hardware change. Quratulain Rajput
Organized data • The mechanism to store collection of record that can be search, process and modified. • The selection of data structures and algorithms makes the difference in making a program efficient. Quratulain Rajput
What is efficiency? • A solution of a problem is called efficient if its solve the problem within its resource constraints. • Time • Space • The cost of the solution is the amount of resources that the solution consumes. Quratulain Rajput
How to select data structure? • Analyze a problem to identify resource constraints need for solution. • Analyze basic operation requires in a solution. • Then select a data structures to meet the above requirements Quratulain Rajput
Basic operations • Insertion: where have to insert or only insert all at once. • Deletion: • Search Quratulain Rajput
Data structures • Each data structure has cost and benefits. • No data structure is ideal for all problem. • The use of data structures requires: • Memory to store data • Time to perform operation • Programming effort Quratulain Rajput
Arrays • Array is a built-in data structure in programming languages. intarr={10, 29,44,34,2,1}; • Each cell contains same type of data. • Array is linear list of consecutive memory locations. • To access we used arr[3]; • Array arr is a reference to the beginning of an array list. Quratulain Rajput
Dynamic Array • When size of data is not fixed then we can use dynamic array. Int[] arr=new int[n]; Quratulain Rajput
List data structures • List is a most generic data structure. Examples are grocery list, shopping list, list of people, list of students. • Items in a list is in linear order. • Insertion, deletion can be done in any order. Quratulain Rajput
List Operations Useful operations • createList(): create a new list (presumably empty) • copy(): set one list to be a copy of another • clear(); clear a list (remove all elments) • insert(X, ?): Insert element X at a particular position in the list • remove(?): Remove element at some position in the list • get(?): Get element at a given position • update(X, ?): replace the element at a given position with X • find(X): determine if the element X is in the list • length(): return the length of the list.
How to implement a List using Array • First define interface of the List then implement operation of the List defined in interface. Quratulain Rajput
Analysis of Array List • Insertion or deletion from particular location of List requires shift operation. • Worst case, when need to insert/delete at the beginning of a List. • Find operation search item in a list. In worst case, item is not present in a list or at end that requires to search the entire array. Quratulain Rajput