1. Data Structures and Algorithms
“I Will, In Fact, Claim That The Difference Between A
Bad Programmer And A Good One Is Whether He
Considers His Code Or His Data Structures More
Important. Bad Programmers Worry About The
Code. Good Programmers Worry About Data
Structures And Their Relationships.” Linus Torvalds
The choice of data structure and algorithm can make the difference
between a program running in a few seconds or many days.
2. Algorithm:
a method or a process followed to solve a problem.
An algorithm takes the input to a problem and transforms it to the output.
Algorithm Efficiency There are often many approaches (algorithms)to solve
a problem. How do we choose between them?!!
At the heart of computer program design are two goals:
1. To design an algorithm that is easy to understand, code and debug.(software )
2. To design an algorithm that makes efficient use of the computer’s resources.( algorithm
analysis)
Critical resources:
• Time • Space (disk, RAM) • Programmer’s effort
• Ease of use
3. Searching:
When we maintain a collection of data, one of the operations we need is a search
routine to locate desired data quickly.
To gauge the performance of searching algorithm :
• We will count the number of comparisons the algorithms make to analyze their
performance. (time it take to finish every time we search using algorithm)
Best, Worst and Average Cases
Not all inputs of a given size take the same time.
Example:
Sequential search for K in an array of n integers:
• Begin at first element in array and look at each element in turn until K is found.
Best Case: [Find at first position: 1 compare]
Worst Case: [Find at last position: n compares]
Average Case: [(n + 1)/2 compares]
4. Computation Complexity
Definition: Measure the efficiency of an algorithm
in terms of time or space required.
Time tends to be more important.
The efficiency of an algorithm is always stated as a
function of the input size
The “Big-O” notation: O(f(n))
Worst case scenario The maximum number of
computation steps taken on any input size n.
5. In an increasing order of complexity:
Constant time: O(1)
Logarithmic time: O(logn)
Linear time: O(n)
Polynomial time: O(n2)
Exponential time: O(2n)
Suppose each step takes 1 microseconds (10-6):
n O(1) O(logn) O(n) O(n^2) O(2^n)
10 1 1 10 100 1024
100 1 2 100 10000 40196936841331500 years
1000 1 3 1000 1 sec …
10000 1 4 10000 1.67 min …
6. Example of search results:
Unsuccessful Search: search(45)
Successful Search: search(12)
NOT_FOUND
4
number
7. sequential Search Performance:
• We count how many times the search value is compared against the array
elements.
• Successful Search
• We analyze the successful and unsuccessful searches separately.
– Best Case – 1 comparison
– Worst Case – N comparisons (N – array size)
• Unsuccessful Search
– Best Case = Worst Case – N comparisons
Notes:
The order of this algorithm is O(N) as N is the number of elements in
array.
8. Searching “self organized search”
• If we want to make sequential more fast we can make some priorities to some
entries that we can need to get to them 80% of time more than others we can
use to method to make it :
• First by use algorithm of “move to front”:
we put our need entry at the top of the array so we can find it mush
easier when need it.
• Second by use algorithm of “ transpose”:
every time we access an entry we make only one shift to the position of the
element so we can reach it next time mush easier.
Note : both of to algorithms don’t need any sorting for the array to use with it.
9. • /* C program to input N numbers and store them in an array.
• * Do a linear search for a given key and report success
• * or failure.
• */
• #include <stdio.h>
• void main()
• {
• int array[10];
• int i, num, keynum, found = 0;
• printf("Enter the value of num n");
• scanf("%d", &num);
• printf("Enter the elements one by one n");
• for (i = 0; i < num; i++)
• {scanf("%d", &array[i]);
• }
• printf("Input array is n");
• for (i = 0; i < num; i++)
• {
• printf("%dn", array[i]);
• }
• printf("Enter the element to be searched n");
• scanf("%d", &keynum);
• /* Linear search begins */
• for (i = 0; i < num ; i++)
• {
• if (keynum == array[i] )
• {
• found = 1;
• break;
• }
• }
• if (found == 1)
• printf("Element is present in the arrayn");
• else
• printf("Element is not present in the arrayn");
• }
10. Binary Search
• If the array is sorted, then we can apply the binary
search technique.
number
• The basic idea is straightforward. First search the value
in the middle position. If X is less than this value, then
search the middle of the left half next. If X is greater
than this value, then search the middle of the right half
next. Continue in this manner.
55 12 17 23 38 44 77
0 1 2 3 4 5 6 7 8
84 90
12. Sequence of Successful Search - 2
high = mid-1=5
44<77
#2
#3
Low=mid +1 so low > high
Finished
13. Binary Search Performance:
Successful Search:
Best Case – 1 comparison
Worst Case – log2N comparisons
Unsuccessful Search:
Best Case =Worst Case – log2N comparisons
Since the portion of an array to search is cut into half after every comparison, we compute
how many times the array can be divided into halves.
So we can note in the example we discussed we can see that we make 3 search in an array
of 8 elements .
So binary search can make number of search equal to log N for base=2.
After K comparisons, there will be N/2K elements in the list. We solve for K when N/2K = 1,
deriving K = log2N.
14. This * Otherwise, a message "UNSUCCESSFUL SEARCH" is displayed.
5. */6.#
include <stdio.h>
7.
8.void main()
9.{
10.int array[20];
11.int i, low, mid, high, key, size;
12.
13.printf("Enter the size of an arrayn");
14.scanf("%d", &size);
15.printf("Enter the array elementsn");
16.for (i = 0; i < size; i++)
17.{
18.scanf("%d", &array[i]);
19.}
20.printf("Enter the keyn");
21.scanf("%d", &key);
22./* search begins */
23.low = 0;
24.high = (size - 1);
25.while (low <= high)
26.{
27.mid = (low + high) / 2;
28.if (key == array[mid])
29.{
30.printf("SUCCESSFUL SEARCHn");
31.return;
32.}
33.if (key < array[mid])
34.high = mid - 1;
35.else
36.low = mid + 1;
37.}
38.printf("UNSUCCESSFUL SEARCHn");
39.}
15.
16. Data Structure
Data structures organize data⇒ more efficient programs.
The choice of data structure and algorithm can make the difference between a
program running in a few seconds or many days.
Efficiency
• A solution is said to be efficient if it solves the problem within its resource
constraints.
• space [These are typical constraints for programs]
• time.
18. Where I can find tree data structure?!
. PDF is a tree based format
Some very successful physics/games engines build trees to accurately
simulate human movement.
file system on a computer
We can insert/delete keys in moderate time (quicker than Arrays )
21. For BST we put values which is smaller than node value
in the left and bigger values in the right
As 30
have to
be in the
left
22. Difference between BT & BST
A binary tree is simply a tree in which each node can have at most two
children.
A binary search tree is a binary tree in which the nodes are assigned
values, with the following restrictions :
1. The left sub tree of a node can only have values less than the
node
2. The right sub tree of a node can only have values greater than the
node
3. The left sub tree of a node is a binary search tree.
4. The right sub tree of a node is a binary search tree.
24. BST Traversal
Traversal mean how can BST rotate to visit every node in the tree
That can be use when we want to print every element in a tree or want
to get the max or min of tree elements .
25. Pre-order
In this way we first visit the root then left hand side then right hand side.
97. 1 10
1
10
6 3 5
5
63
Here we make delete for a root that contain 2
children to solve it we will choose from our tree the
largest value in the right or the smallest value in the
left.
98. BST Search Performance:
Successful Search:
Best Case – 1 comparison
Worst Case – log2N comparisons
Unsuccessful Search:
Best Case =Worst Case – log2N comparisons
99. Applications of BST
• allows you to do range searches efficiently.
• Used in internet routers.
• Finding square root of given number
• In The language rules and are often used to build dictionaries.
• One of the best ways can be modified to make search of data but sometimes
having problem in insertion & deletion that can be complex when achieving
best searching using types of BSTs.
100. AVL (Adelson-Velskii and Landis) tree.
An AVL tree is identical to a BST except
height of the left and right subtrees can differ by at most 1.
height of an empty tree is defined to be (–1).
AVL Tree
5
8
2
4
3
1 7
0
1
2
3
Height
Height is the longest distance from
the root to the most far leaf
101. 6
81
4
3
1
5
height
0
1
2
3
We have more than one node as difference between the left and right sub
trees that can make order of search and insert and delete become more than
log2n
So we need to make re-balance to the tree to make it AVL
Not an AVL tree
102. Note :
Balanced factor for every node :that is the
difference between the right and left sub trees to
make a decision if the difference more than 1 or
less than -1 it will need re-balance.
108. AVL Tree Building Example
Let us work through an example that inserts numbers in a balanced search tree.
We will check the balance after each insert and rebalance if necessary using rotations
Insert(1)
1
114. 140
120 -1
-2
0
insert 140 Go back to
every parent
to see it’s
balance
factor
It’s over weight in
the right .
It’s first case right-
right unbalance
115. 140
120
Null To replace we
make change for
nodes pointers
make 120 to link
for 100 &100 to
link for what 120
link for in the left
hand side of it
0
0
0
120. 140
120
0
80
60
0
90
We will choose 100
as it between 80
and 120 and make
it the root
0
1
Null
We make the right side of 100 to be left
side of 120 and left side of 100 right
side of 80 as 80<100<120
0
123. • When we maintain a collection of data, many applications call
for rearranging the data in certain order, e.g. arranging Person
information in ascending order of age.
• Here’s the problem statement:
Given an array of N integer values, arrange the values into ascending
order.
• We will count the number of comparisons the algorithms make
to analyze their performance.
– The ideal sorting algorithm will make the least possible number of
comparisons to arrange data in a designated order.
• We will compare different sorting algorithms by analyzing their
worst-case performance.
Sorting
124. 1. Find the smallest
element in the list.(by
putting element by
element as min and
compare with all of the
array)
2. Exchange the element in
the first position and the
smallest element. Now
the smallest element is
in the first position.
3. Repeat Step 1 and 2
with the list having one
less element till make it
sort.
exchange
first
First min
0 1 2 3 4 5 6 7 8
5 17 23 90 12 44 38 84 77
0 1 2 3 4 5 6 7 8
5 17 23 90 12 44 38 84 77
sorted unsorted
Selection Sort
125. 0 1 2 3 4 5 6 7 8
5 17 23 90 12 44 38 84 77
sorted
Pass #
1
2
3
7
5 12 23 90 17 44 38 84 77
5 12 17 90 23 44 38 84 77
5 12 17 23 38 44 77 84 90
5 12 17 23 38 44 77 84 90
8
We can
note that
this
method
make a lot
of rotation
we don’t
need it as
we can
see the no
difference
between
as 7&8
126. 1./*
2. * C program to accept N numbers and arrange them in an ascending order
3. */
4.#include <stdio.h>
5.
6.void main()
7.{
8.int i, j, a, n, number[30];
9.
10.printf("Enter the value of N n");
11.scanf("%d", &n);
12.printf("Enter the numbers n");
13.for (i = 0; i < n; ++i)
14.scanf("%d", &number[i]);
15.for (i = 0; i < n; ++i)
16.{
17.for (j = i + 1; j < n; ++j)
18.{
19.if (number[i] > number[j])
20.{
21.a = number[i];
22.number[i] = number[j];
23.number[j] = a;
24.}
25.}
26.}
27.printf("The numbers arranged in ascending order are given below n");
28.for (i = 0; i < n; ++i)
29.printf("%dn", number[i]);
30.}
Selection sort C-code
127. Selection Sort Performance
• We derive the total number of comparisons by
counting the number of times the inner loop is
executed.
• For each execution of the outer loop, the inner
loop is executed length – start times
• Complexity O(n^2) as:
• We loop n+n-1+n-2+n-3……..+1 nearly we have
order of n*n.
• We can use this method if I need to get first one
or two or three elements for getting first
element in its right place this method will be
convenient but if need a very large array to be
sort it will take a lot of time !!
• If n=1000 we need to loop 1000000 time its to
mush !!
• Adv:But we can see its very simple to
implement(for software).
• Adv. :Very good for storage as it needn’t to
waste space in memory it sort in place.
• Disadv:It’s Blind sort as it will loop O(N^2) even
having a sorted array.
128. Bubble Sort
• With the selection sort, we make one exchange at the end of
one pass.
• The bubble sort improves the performance by making more than
one exchange during its pass.
• By making multiple exchanges, we will be able to move more
elements toward their correct positions using the same number
of comparisons as the selection sort makes.
• The key idea of the bubble sort is to make pairwise comparisons
and exchange the positions of the pair if they are out of order.
129. 0 1 2 3 4 5 6 7 8
23 17 5 90 12 44 38 84 77
One Pass of Bubble Sort
exchange
17 23 5 90 12 44 38 84 77
exchange
17 5 23 90 12 44 38 84 77
exchange
Ok
17 5 23 12 90 44 38 84 77
exchange
17 5 23 12 44 90 38 84 77 17 5 23 12 44 38 90 84 77
exchange
17 5 23 12 44 38 84 90 77 17 5 23 12 44 38 84 77 90
The largest value 90 is at the end of
the list.
1) 2)
3)
4)
5)
6)
7) 8)
131. Bubble Sort Performance
• In the worst case, the outer while loop is executed N-1 times for
carrying out N-1 passes.
• For each execution of the outer loop, the inner loop is executed
bottom+1 times. The number of comparisons in each successive
pass is N-1, N-2, … , 1. Summing these will result in the total
number of comparisons.
• So the performances of the bubble sort and the selection sort
are approximately equivalent.
However, on the average, the bubble sort performs much better
than the selection sort because it can finish the sorting without
doing all N-1 passes .
Example : if we have sorted array it will loop only one
time not as selection sort o(n^2)
Its not Blind sort.