Download Singly and Doubly Linked Lists: Data Structures and Operations and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!
DATA STRUCTURE AND
ALGORITHM
DATA STRUCTURE
- Organization of data in a well defined and structured manner so that the data can be processed efficiently and effectively.
- Container structure for data.
- is a specialized format for organizing, processing, retrieving and storing data.
TYPES OF DATA STRUCTURES
- Arrays - An array stores a collection of items at adjoining memory locations. Items that are the same type get stored together so that the position of each element can be calculated or retrieved easily. Arrays can be fixed or flexible in length. - Stacks - A stack stores a collection of items in the linear order that operations are applied. This order could be last in first out (LIFO) or first in first out (FIFO). - Queues - A queue stores a collection of items similar to a stack; however, the operation order can only be first in first out. - Linked lists - A linked list stores a collection of items in a linear order. Each element, or node, in a linked list contains a data item as well as a reference, or link, to the next item in the list.
ALGORITHM
- An algorithm is a step by step method of solving a problem. It is commonly used for data processing, calculation and other related computer and mathematical operations.
- used to manipulate data in various ways, such as inserting a new data item, searching for a particular item or sorting an item.
ALGORITHM EFFECTIVENESS
- Measured in notations. Eg: Big O
- Gives complexity of two dimensions; space and time
- Space complexity: refers to how much of the space the program needs based on the size of input.
- Time complexity: how much time it requires for the completion of execution if the input is changed?
- Space complexity is not cared as space can be reused.
- Time Complexity is important as time cannot be reused.
ABSTRACT DATA TYPES (ADT)
- Consider the statement: “I put my lunch in my bag.”
- What is meant by the term bag in this context?
- Most likely it is a backpack, or satchel, but it could also be a hand bag, shopping bag, sleeping bag, body bag... (but probably not a bean bag).
- It doesn’t actually matter. To parse the statement above, we simply understand that a bag is something that we can - 1. put things in, - 2. carry places, and - 3. take things out.
- Such a specification is an Abstract Data Type.
ABSTRACT DATA TYPES (ADT)
- Mathematical description of an object and the set of operations on the object
- is the interface of a data structure without any specification of the implementation.
- Eg: Class in object oriented programming.
- List: can be any and operations are specific to specific lists.
- But basic operations are: insert, retrieve, update, delete
Time Complexity Analysis
- Nested Loops
- We analyze the nested loop from inside out. Total time is always the product of all loop times.
- Total time = c × n × n = cn 2 = O(n 2 ).
Time Complexity Analysis
- Consecutive Statement
- We add time complexities of each statement.
- Total time = c 0 + c 1 n + c 2 n 2 = O(n^2 ).
Time Complexity Analysis
- Logarithmic complexity:
- n algorithm is O(logn) if it takes a constant time to cut the problem size by a fraction (usually by ½). As an example let us consider the following program:
- If we observe carefully, the value of i is doubling every time. Initially i = 1, in next step i= 2, and in subsequent steps i = 4,8 and so on. Let us assume that the loop is executing some k times. At kth step 2k = n, and at (k + 1)th step we come out of the loop. Taking logarithm on both sides, gives
- Total time = O(log n).
Time Complexity Analysis
- Recursive function:
- Since recursive function is called n times, the complexity is simply O(n)
- Total time = O(n).
- Sometimes if the code is cutting the number of recursive calls by half then the time complexity is Olog(n).
- So gewnerally depends upon how the recursive function is called.
Classwork
- Find the worst case time complexity of the following code in big O notation.
Classwork