Lecture 1
Lecture 1
AND APPLICATIONS
LECTURE 1
HUMA TABASSUM
LECTURER
DEPT. OF COMPUTER SCIENCE & INFORMATION TECHNOLOGY
NED UNIVERSITY OF ENGINEERING & TECHNOLOGY
INTRODUCTION
• Data: facts or figures.
• Structure: arrangement of, and relations between the parts or elements.
• Algorithm: a finite sequence of precise instructions for performing a
computation or for solving a problem.
• Applications: areas or domains in which the concept, design, strategy,
etc. can be used.
• Space complexity: The data structure should use as little memory as possible.
return 0;
}
Row and Column Major Ordering
• For these operations, there are various algorithms; some of which will
be explored in this course.
Searching
• In data structures, the process of finding the desired information from
the set of items stored in the form of elements in the computer
memory is referred to as searching.
• These sets of items are in various forms, such as an array, tree, graph,
or linked list.
• We can also define searching in data structures as locating the
desired element of specific characteristics in a collection of items.
• The two most common searching algorithms are
• Linear Search
• Binary Search
Linear Search
(Linear Search) LINEAR(DATA, N, ITEM)
Here DATA is a linear array with N elements, and ITEM is a given item of information. This algorithm
finds the location LOC of ITEM in DATA or sets LOC:=NULL if the search is unsuccessful.
1. [Initialize] Set K := 1 and LOC := NULL.
2. Repeat steps 3 and 4 while LOC = NULL and K ≤ N.
3. If ITEM = DATA[K], then: Set LOC := K.
4. Set K := K + 1 [Increments counter.]
[End of Step 2 loop.]
5. [Successful?]
If LOC = NULL, then:
Write: ITEM is not in the array DATA.
Else:
Write: LOC is the location of ITEM.
[End of IF structure.]
6. Exit.
Example
Complexity Analysis of Linear Search
• Best Case
• The best case occurs when ITEM is the first element in the array DATA.
• Thus, f(N) = 1 => O(f(N)) = 1 => O(1)
• Worst Case
• Clearly the worst case occurs when ITEM is the last element in the array DATA
or is not there at all.
• Thus, f(N) = N (or N+1) => O(f(N)) = N => O(N)
• Average Case
𝑁+1
• f(N) = => O(f(N)) = N => O(N)
2
Binary Search
(Binary Search) BINARY(DATA, N, ITEM)
Here DATA is a sorted array with lower bound LB and upper bound UB, and ITEM is a given item of
information. The variables BEG, END, and MID denote, respectively, the beginning, end, and middle
locations of a segment of elements of DATA. This algorithm finds the location LOC of ITEM in DATA,
or sets LOC:=NULL.
1. [Initialize] Set BEG := LB, END := UB, MID := int((BEG + END)/2), and LOC := NULL.
2. Repeat steps 3 and 4 while BEG ≤ END and DATA[MID] ≠ ITEM.
3. If ITEM < DATA[MID], then: Set END := MID – 1.
Else: Set BEG := MID + 1.
[End OF If Structure.]
4. Set MID := int((BEG + END)/2).
[End of Step 2 loop.]
5. If DATA[MID] = ITEM, then:
Set LOC:= MID.
Write: LOC is the location of ITEM.
Else:
Write: ITEM is not in the array DATA.
[End of IF structure.]
6. Exit.
Example
Complexity Analysis of Binary Search
• Best Case
• The best case occurs when ITEM is the MID element in the array DATA.
• Thus, f(N) = 1 => O(f(N)) = 1 => O(1)
• Worst Case
• Clearly the worst case occurs when ITEM is the first or last element in the
array DATA, or is not there at all.
• At first iteration, length of DATA is N. In second iteration, it becomes N/2; in third, N/4,
and so on until only 1 element remains.
• Thus, f(N) = log2N => O(f(N)) = log2N => O(log2N)
• Average Case
• f(N) = log2N => O(f(N)) = log2N => O(log2N)