Data Structure & Algorithms: Prepared By: Kashafad-Dooja
Data Structure & Algorithms: Prepared By: Kashafad-Dooja
Prepared by:
kashafAd-Dooja
2
Instructor Contact Details
Reinforce the concept that costs and benefits exist for every data structure.
Following contents will be covered throughout the semester in week wise lectures:
Abstract data types, complexity analysis, Big Oh notation, Stacks (linked lists and array
implementations), Recursion and analyzing recursive algorithms, divide and conquer
algorithms, Sorting algorithms (selection, insertion, merge, quick, bubble, heap, shell, radix,
bucket), queue, dequeuer, priority queues (linked and array implementations of queues),
linked list & its various types, sorted linked list, searching an unsorted array, binary search for
sorted arrays, hashing and indexing, open addressing and chaining, trees and tree traversals,
binary search trees, heaps, M-way tress, balanced trees, graphs, breadth-first and depth-first
traversal, topological order, shortest path, adjacency matrix and adjacency list
implementations, memory management and garbage collection.
Any organization for a collection of records that can be searched, processed in any
order, or modified.
The choice of data structure and algorithm can make the difference between a
program running in a few seconds or many days.
9
Efficiency
Are all data inserted into the data structure at the beginning, or are
insertions interspersed with other operations?
x[0]
Array cells are contiguous in
computer memory x[1]
The memory can be thought of as
an array x[2]
x[3]
x[4]
x[5]
17
What is Array Name?
Real life:
a. shopping list,
b. groceries list,
c. list of people to invite to dinner
d. List of presents to get
20
Lists
A list is collection of items that are all of the same type (grocery items,
integers, names)
The items, or elements of the list, are stored in some particular order
It is possible to insert new elements into various positions in the list and
remove any element of the list
21
Lists
The order is important here; this is not just a random collection of elements,
it is an ordered collection
22
List Operations
Useful operations
1. Use the actual index of element: insert after element 3, get element
number 6. This approach is taken by arrays
2. Use a “current” marker or pointer to refer to a particular position in the
list.
24
List Operations
If we use the “current” marker, the following four methods would be useful:
We have designed the interface for the List; we now must consider how to
implement that interface.
Implementing Lists using an array: for example, the list of integers (2, 6, 8,
7, 1) could be represented as:
current size
A 2 6 8 7 1
3 5
1 2 3 4 5
add(9); current position is 3. The new list would thus be: (2, 6, 8, 9, 7, 1)
We will need to shift everything to the right of 8 one place to the right to make place for the new
element ‘9’.
current size
step 1: A 2 6 8 7 1
3 5
1 2 3 4 5 6
current size
step 2: A 2 6 8 9 7 1
4 6
1 2 3 4 5 6
Next ():
current size
A 2 6 8 9 7 1
4 6
1 2 3 4 5 6 5
We will have to worry about these when we write the actual code.
current size
Step 1: A 2 6 8 9 1
5 6
1 2 3 4 5 6 5
current size
Step 2: A 2 6 8 9 1
5 5
1 2 3 4 5
current size
Step 1: A 2 6 8 9 1
5 6
1 2 3 4 5 6 5
current size
Step 2: A 2 6 8 9 1
5 5
1 2 3 4 5
int find(int X)
{
int j;
for(j=1; j < size+1; j++ )
if( A[j] == X ) break;
Other operations:
add
we have to move every element to the right of current to make space for the
new element.
Worst-case is when we insert at the beginning; we have to move every element
right one place.
Average-case: on average we may have to move half of the elements
remove
Worst-case: remove at the beginning, must shift all remaining elements to the left.
Average-case: expect to move half of the elements.
find
Worst-case: may have to search the entire array
Average-case: search at most half the array.
Please feel free to ask any thing related to topic covered in these slides.
Text Book:
Nell Dale : C++ Plus Data Structures, Latest Edition, Jones & Bartlett
Reference Text Books:
Data Structure using C/C++ by Mark Alan.
Yadidyah Langsam, Moshe J. Augenstein, Aaron M. Tenenbaum: Data Structures
using C and C++.
Handouts:
Dr. Sohail Aslam: Data Structures Handouts.