2. Searching
Internal Search
The search in which the whole list resides in
the main memory is called internal search.
External Search
The search in which the whole list resides in
the Secondary memory is called external
search.
3. Types of searching Algorithm
• Linear or Sequential searching
• Binary searching
4. Algorithm For Linear Search
LINEAR(DATA ,N,ITEM,LOC)
1.Set K:=1 and LOC := 0.
2.Repeat Steps 3 and 4 while LOC = 0 and K<=N.
3.If ITEM = DATA[K] ,then Set LOC:= K.
4.Set K:=K+1.
5.If LOC = 0,then:
Write: ITEM is not in the array DATA.
Else:
Write: LOC is the location of ITEM.
6.Exit.
6. Algorithm For Binary Search
BINARY(DATA , LB , UB, ITEM , LOC)
1. Set BEG:= LB, END:=UB and
MID = INT((BEG+END)/2).
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.
4. Set MID := INT((BEG+END)/2).
7. Algo. Cont.
5. If DATA[MID]=ITEM, then:
Set LOC:= MID.
Else:
Set LOC:= NULL.
6. Exit.
9. Introduction to Sorting
• Sorting: An operation that arranges items into groups
according to specified criterion.
A = { 3 1 6 2 1 3 4 5 9 0 }
A = { 0 1 1 2 3 3 4 5 6 9 }
10. Why Sort and Examples
Consider:
• Sorting Books in Library (Dewey system)
• Sorting Individuals by Height (Feet and
Inches)
• Sorting Movies in Blockbuster
(Alphabetical)
• Sorting Numbers (Sequential)
11. Types of Sorting Algorithms
There are many different types of sorting
algorithms, but the primary ones are:
• Bubble Sort
• Selection Sort
• Insertion Sort
• Merge Sort
• Quick Sort
• Radix Sort
• Heap Sort
12. Complexity
• Most of the primary sorting algorithms run
on different space and time complexity.
• Time Complexity is defined to be the time
the computer takes to run a program (or
algorithm in our case).
• Space complexity is defined to be the
amount of memory the computer needs to
run a program.
13. Complexity (cont.)
Complexity in general, measures the
algorithms efficiency in internal factors such
as the time needed to run an algorithm.
External Factors (not related to complexity):
•Size of the input of the algorithm
•Speed of the Computer
•Quality of the Compiler
14. O(n), Ω(n), & Θ(n)
• An algorithm or function T(n) is O(f(n))
whenever T(n)'s rate of growth is less than or
equal to f(n)'s rate.
• An algorithm or function T(n) is Ω(f(n))
whenever T(n)'s rate of growth is greater than
or equal to f(n)'s rate.
• An algorithm or function T(n) is Θ(f(n)) if
and only if the rate of growth of T(n) is equal
to f(n).
15. 10
Common Big-Oh’s
Time complexity Example
O(1) constant Adding to the front of a linked list
O(log N) log Finding an entry in a sorted array
O(N) linear Finding an entry in an unsorted array
O(N log N) n-log-n Sorting n items by ‘divide-and-conquer’
O(N2
) quadratic Shortest path between two nodes in a graph
O(N3
) cubic Simultaneous linear equations
(Binary)
Finding 8:
9
50
22
21
8
5
1
(Linear)
Finding 8:
9
50
22
21
8
5
1Front
Initial:
Final:63
http://www.cs.sjsu.edu/faculty/lee/cs146/23FL3Complexity.ppt
17. Time Efficiency
• How do we improve the time efficiency of a
program?
• The 90/10 Rule
–90% of the execution time of a program is spent in
–executing 10% of the code
• So, how do we locate the critical 10%?
– software metrics tools
– global counters to locate bottlenecks (loop
executions, function calls)
18. Time Efficiency Improvements
• Possibilities (some better than others!)
• Move code out of loops that does not belong
there (just good programming!)
• Remove any unnecessary I/O operations (I/O
operations are expensive time-wise)
• Code so that the compiled code is more
efficient
•Moral - Choose the most appropriate
algorithm(s) BEFORE program implementation
20. Selection Sort
MIN(A,K,N,LOC)
1. Set MIN = A[K] and LOC = K
2. Repeat for J=K+1, K+2 ……… N
If MIN>A[J], then Set MIN = A[J]
and LOC = J
End of Loop
3. Return
21. Selection Sort
SELECTION (A,N)
1. Repeat steps 2 and 3 for K=1, 2 ……… N-1
2. Call MIN(A,K,N,LOC)
3. Set TEMP = A[K]
A[K] = A[LOC]
A[LOC] = TEMP
End of Step 1 Loop
4. Exit
22. Complexity of the Selection Sort
• Worst Case
– O(n2)
• Average Case
– O(n2)
25. Insertion Sort
INSERTION (A,N)
1. Set A[0] = - ∞
2. Repeat Steps 3 to 5 for K = 2,3,….N
3. Set TEMP = A[K] and PTR = K-1
4. Repeat while TEMP<A[PTR]
1. Set A[PTR+1] = A[PTR]
2. Set PTR = PTR – 1
End of Loop
5. Set A[PTR+1] = TEMP
End of Step 2 Loop
6. Return
26. Complexity of the Insertion Sort
• Worst Case
– O(n2)
• Average Case
– O(n2)
28. Algorithm for BUBBLE SORT
BUBBLE(DATA , N)
1.Repeat Steps 2 and 3 for K=1 to N-1.
2.Set PTR := 1. // Initialize pass pointer PTR
3.Repeat while PTR<=N-K.
a) If DATA[PTR]>DATA[PTR+1],then
Interchange DATA[PTR] and
DATA[PTR+1]
b) Set PTR:= PTR+1
4. Exit.
32. MERGE SORT
MERGE_SORT (A, BEG, END)
1. IF BEG<ENG
SET MID=(BEG+END)/2
CALL MERGE_SORT (A, BEG, MID)
CALL MERGE_SORT (A, MID+1, END)
MERGE(A, BEG, MID, END)
2. END
33. MERGE(A, BEG, MID, END)
1. SET I = BEG, J=MID+1, INDEX=0
2. REPEAT WHILE (I<=MID) AND (J<=END)
IF A[I]<A[J], THEN
SET TEMP [INDEX] = A[I]
SET I=I+1
ELSE
SET TEMP [INDEX]=A[J]
SET J=J+1
SET INDEX = INDEX +1
34. 3. IF I>MID THEN
REPEAT WHILE J<=END
SET TEMP [INDEX]=A[J]
SET INDEX=INDEX+1
SET J=J+1
ELSE
REPEAT WHILE I<=MID
SET TEMP[INDEX]=A[I]
SET INDEX = INDEX +1
SET I=I+1
35. 4. SET K=0
5. REPEAT WHILE K<INDEX
SET A[K]=TEMP[K]
SET K=K+1
6. END
36. QUICKSORT
Starting with first element of list do the operations
1. Compare from right to find element less than selected
and interchange
2. Compare from left to find element greater than selected
and interchange
44,33,11,55,77,90,40,60,33,22,88,66
22,33,11,55,77,90,40,60,99,44,88,66
22,33,11,44,77,90,40,60,99,55,88,66
22,33,11,40,77,90,44,60,99,55,88,66
22,33,11,40,44,90,77,60,99,55,88,66
22,33,11,40,44,90,77,60,99,55,88,66
Two Sublists are created:
Before 44 and After 44
37. 37
Quick Sort Algorithm
QUICK (A, N, BEG, END, LOC)
1. [Initialize] Set LEFT= BEG, RIGHT=END and LOC=BEG
2. [Scan from right to left]
a) Repeat while A[LOC]<=A[RIGHT] and LOC != RIGHT
RIGHT=RIGHT-1
[End of loop]
b) If LOC=RIGHT then : Return
c) If A[LOC]>A[RIGHT] then
i) Interchange A[LOC] and A[RIGHT]
ii) Set LOC=RIGHT
iii) Go to step 3
[End of If structure]
38. 3. [Scan from left to right]
a) Repeat while A[LEFT]<=A[LOC] and LEFT!=LOC
LEFT=LEFT+1
[End of loop]
b) If LOC=LEFT, then Return
c) If A[LEFT]>A[LOC], then
i) Interchange A[LEFT] and A[LOC]
ii) Set LOC=LEFT
iii) Goto step 2
[End of If structure]
38
39. (QuickSort) This algorithm sorts an array A with N elements.
1. TOP=NULL
2. If N>1 then TOP=TOP+1, LOWER[1]=1, UPPER[1]=N
3. Repeat Steps 4 to 7 while TOP!=NULL
4. Set BEG=LOWER[TOP], END=UPPER[TOP]
TOP=TOP-1
5. Call QUICK(A, N, BEG, END, LOC)
6. If BEG<LOC-1 then
TOP=TOP+1, LOWER[TOP]=BEG, UPPER[TOP]=LOC-1
[End of If structure]
7. If LOC+1<END then
TOP=TOP+1, LOWER[TOP]=LOC+1, UPPER[TOP]=END
[End of If Structure]
[End of Step 3 loop]
8. Exit
39
42. Radix Sort (or Bucket Sort)
• It is a non comparative integer sorting
algorithm
• It sorts data with integer keys by grouping
keys by the individual digits which share the
same significant position and value.
• A positional notation is required, but because
integers can represent strings of characters
(e.g., names or dates) and specially formatted
floating point numbers, Radix sort is not
limited to integers.
43. Radix Sort Algorithm
Let A be an array with N elements in the memory
• Find the largest element of the array
• Find the total no. of digits ‘NUM’ in the largest element
• Repeat Steps 4, 5 for PASS = 1 to NUM
• Initialize buckets (0 to 9 for numbers, A to Z for
Characters)
For i = 0 to (N-1)
Set DIGIT = Obtain digit number PASS of A [i]
Put A [i] in bucket number DIGIT
End of for loop
5. Collect all numbers from the bucket in order
6. Exit