Linear Search


Search: locate an item in a list of information

Two algorithms we will examine:
◦ Linear search
◦ Binary search


Also called the sequential search
Starting at the first element, this algorithm
sequentially steps through an array
examining each element until it locates the
value it is searching for.

Array numlist contains:
17


23
5
11
2
29
3
Searching for the the value 11, linear search
examines 17, 23, 5, and 11
Searching for the the value 7, linear search
examines 17, 23, 5, 11, 2, 29, and 3

Algorithm:
set found to false; set position to –1; set index to
0
while index < number of elts. and found is false
if list[index] is equal to search value
found = true
position = index
end if
add 1 to index
end while
return position
#include<stdio.h>
int main()
{
int N,key;int A[N];int i,indx=-1;
printf("enter the number of elements:");
scanf("%d",&N);
printf("enter the array\'s elements:");
for(i=0;i<N;i++)
{
scanf("%d",&A[i]);
}
printf("enter the key:");
scanf("%d",&key);
for(i=0;i<N;i++)
{
if(A[i]==key)
{
indx=i;
break;
}
}
if(indx==-1)
printf("the key does not exist");
}
else printf("the key does exist at index: %d ",indx);
return 0;

Benefits:
◦ Easy algorithm to understand
◦ Array can be in any order

Disadvantages:
◦ Inefficient (slow): for array of N elements, examines
N/2 elements on average for value in array, N
elements for value not in array
1.
2.
3.
Requires array elements to be in order
Divides the array into three sections:
◦ middle element
◦ elements on one side of the middle element
◦ elements on the other side of the middle
element
If the middle element is the correct value,
done. Otherwise, go to step 1. using only
the half of the array that may contain the
correct value.
Continue steps 1. and 2. until either the
value is found or there are no more
elements to examine

Array numlist2 contains:
2


3
5
11
17
23
29
Searching for the the value 11, binary search
examines 11 and stops
Searching for the the value 7, linear search
examines 11, 3, 5, and stops
Set first index to 0.
Set last index to the last subscript in the array.
Set found to false.
Set position to -1.
While found is not true and first is less than or equal to last
Set middle to the subscript half-way between array[first] and array[last].
If array[middle] equals the desired value
Set found to true.
Set position to middle.
Else If array[middle] is greater than the desired value
Set last to middle - 1.
Else
Set first to middle + 1.
End If.
End While.
Return position.
int binarySearch(int array[],
{
int first = 0,
last = size - 1,
middle,
position = -1;
bool found = false;
int size, int value)
//
//
//
//
//
First array element
Last array element
Mid point of search
Position of search value
Flag
while (!found && first <= last)
{
middle = (first + last) / 2;
if (array[middle] == value)
{
found = true;
position = middle;
}
else if (array[middle] > value)
last = middle - 1;
else
first = middle + 1;
}
return position;
}
// Calculate mid point
// If value is found at mid
// If value is in lower half
// If value is in upper half
0
1
2
3
4
5
6
10
14
19
27
31
35
42
log2N

Benefits:
◦ Much more efficient than linear search. For array of
N elements, performs at most log2N comparisons

Disadvantages:
◦ Requires that array elements be sorted

Sort: arrange values into an order:
◦ Ascending numeric
◦ Descending numeric

Two algorithms considered here:
◦ Bubble sort
◦ Selection sort



Bubble sort algorithm starts by comparing the first two elements
of an array and swapping if necessary, i.e., if you want to sort the
elements of array in ascending order and if the first element is
greater than second then, you need to swap the elements but, if
the first element is smaller than second, you mustn't swap the
element. Then, again second and third elements are compared
and swapped if it is necessary and this process go on until last
and second last element is compared and swapped. This
completes the first step of bubble sort.
If there are n elements to be sorted then, the process mentioned
above should be repeated n-1 times to get required result. But,
for better performance, in second step, last and second last
elements are not compared becuase, the proper element is
automatically placed at last after first step. Similarly, in third
step, last and second last and second last and third last elements
are not compared and so on.
A figure is worth a thousand words so, acknowledge this figure
for better understanding of bubble sort.

Here, there are 5 elements to the sorted.
-2
45
0
11
-9
Bubble Sort Algorithm(cont.)
step=0;step<n-2
i=0;i<n-1-step
Bubble Sort Algorithm(cont.)
/*C Program To Sort data in ascending order using bubble sort.*/
#include <stdio.h>
int main()
{
int data[100],i,n,step,temp;
printf("Enter the number of elements to be sorted: ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("%d. Enter element: ",i+1);
scanf("%d",&data[i]);
}
for(step=0;step<n-2;++step)
for(i=0;i<n-step-1;++i)
{
if(data[i]>data[i+1])
/* To sort in descending order, change > to < in this line. */
{
temp=data[i];
data[i]=data[i+1];
data[i+1]=temp; }
}
printf("In ascending order: ");
for(i=0;i<n;++i)
printf("%d ",data[i]);
return 0;
}
Bubble Sort Algorithm(cont.)
Complexity:
Order of (n) for the best case
Order of (n2) for the worst case,

Benefit:
◦ Easy to understand and implement

Disadvantage:
◦ Inefficient: slow for large arrays





The list is divided into two sublists, sorted and
unsorted, which are divided by an imaginary
wall.
We find the smallest element from the unsorted
sublist and swap it with the element at the
beginning of the unsorted data.
After each selection and swapping, the
imaginary wall between the two sublists move
one element ahead, increasing the number of
sorted elements and decreasing the number of
unsorted ones.
Each time we move one element from the
unsorted sublist to the sorted sublist, we say
that we have completed a sort pass.
A list of n elements requires n-1 passes to
completely rearrange the data.
CENG 213 Data Structures
Sorted
Unsorted
23
78
45
8
32
56
Original List
8
78
45
23
32
56
After pass 1
8
23
45
78
32
56
After pass 2
8
23
32
78
45
56
After pass 3
8
23
32
45
78
56
After pass 4
8
23
32
45
56
78
After pass 5
CENG 213 Data Structures
#include <stdio.h>
int main() {
int indexMin,i,j;
int intArray[]={5,3,6,8,1,9};
int MAX=6;
// loop through all numbers
for(i = 0; i < MAX; i++) {
// set current element as minimum
indexMin = i;
// check the element to be minimum
for(j = i+1; j<MAX; j++) {
if(intArray[j] < intArray[indexMin]) {
indexMin = j;
}
}
if(indexMin != i) {
printf("Items swapped: [ %d, %d ]\n" , intArray[i], intArray[indexMin]);
// swap the numbers
int temp = intArray[indexMin];
intArray[indexMin] = intArray[i];
intArray[i] = temp;
}
printf("Iteration %d#:\n",(i+1));
}
for (i=0;i<MAX;i++)
printf("%d\n",intArray[i]);
}

Benefit:
◦ More efficient than Bubble Sort, since fewer
exchanges

Disadvantage:
◦ May not be as easy as Bubble Sort to understand