Professional Documents
Culture Documents
Searching Algorithms:
1) Sequential Search
2) Binary Search
3) Interpolation Search
Sorting Algorithms:
1) Selection Sort
2) Bubble Sort
3) Shell Sort
I. INTRODUCTION
A. Sequential Search
In searching algorithms, the user has to give how many
number of input, the set of data and a number to be searched.
Then select the particular algorithm from the list and then the
194
pos
List
Target
pos
195
C. Interpolation Search
Interpolation search is an algorithm for searching a given
target value in an indexed array that has been sorted in
ascending order. In each search step it calculates where in the
remaining search space the target might, be based on the
values at the bounds of the search space and the value of the
target, usually via a linear interpolation. The value actually
found at this estimated position is then compared to the target
value. If it is not equal, then depending on the comparison, the
remaining search space is reduced to the part before or after
the estimated position.
Algorithm Interpolation Search (Target, N)
196
pos
ct.d(). When the list is unsorted, it sorts the list first and then
starts searching.
to be exchanged
pos1
flg
-temporary variable
Target
197
A. Selection sort
The algorithm works as follows:
1.
2.
3.
Effectively, we divide the list into two parts: the sub list of
items already sorted, which we build up from left to right and
is found at the beginning, and the sub list of items remaining
to be sorted, occupying the remainder of the array.
Fig. 9 Selection sort- Choosing the algorithm
pos
From the first element to the last element, find the smallest
element and its position is assigned to the variable min.
Variable pos is assigned the value i that represent the position
in the array starting from 1 and variable pos1 is assigned the
value min. Then repaint() function is called to swap the values
in the positions given in variable pos and pos1 as illustrated in
Fig.10.
to be exchanged
pos1
-temporary variable
min
198
Step10: t=List[j]
Step11: List[j] =List [j+1]
Step12: List [j+1] =t
Step13: thread_var=0
Step14: End if
Step15: End for j
Step16: End for i
Step17: calls ct.d1()//function that stops the timer
Fig.12 depicts choosing the bubble sort algorithm from the
given list. The program starts and the timer is started using the
function ct.d().
Fig. 11 Selection sort- Sorting process completed
B. Bubble sort
The bubble sort algorithm makes number of passes through
the list of elements .On each pass it compares adjacent
element values. If they are out of order, they are swapped. We
start each of the passes at the beginning of the list .On first
pass, once the algorithm reaches the largest element, it will be
swapped with all of the remaining elements, moving it to the
end of the list. The second pass will move the second largest
element down the list until it is in the second to last location.
The process continues with each additional pass moving one
more of the larger values down in the list. If on any pass there
are no swaps, all of the elements are now in order and the
algorithm can stop.
pos
pos1
-temporary variable
The program stops after the N-1 number of passes and the
List is sorted as given in Fig.14. The timer is stopped using the
function ct.d1().
199
pos
to be exchanged
pos1
200
No. of
Inputs
Sequential
Searching
in Sec.
0
1
2
3
4
5
6
7
8
Binary
Searching
in Sec.
0
1.3
1.9
2.6
3.2
4.3
6.4
8
9.6
0
1.3
3.1
7.8
23.2
27
32.3
51.2
60.8
No. of
Inputs
Interpolation
Searching in Sec.
Selection
Sorting in
Sec.
0
0
1.3
3.8
7.7
23.7
28.1
34.2
55
61.4
2.8
2.8
2.8
5.8
6.7
6.7
7.6
13.4
9.6
9.6
25
13.4
16.3
35.5
20.1
16.8
40.3
25
17.3
55.7
30.8
AVERAGE VALUES
COMPARISON OF SORTING
ALGORITHMS
70
40
BINARY
30
60
50
SECONDS
SECONDS
60
SEQUENTIAL
INTERPOLATIO
N
20
Shell
Sorting in
Sec.
0
COMPARISON OF SEARCHING
ALGORITHMS
50
Bubble
Sorting in
Sec.
0
10
40
SELECTION
BUBBLE
SHELL
30
20
10
0
0
NUMBER OF INPUTS
NUMBER OF INPUTS
201
[4]
[5]
[6]
202