You are on page 1of 19

LESSON 3

Searching and Sorting-I

Ibrahim
Mesecan
Page 1
Course Objectives
The course objectives :
To understand the need of searching and
sorting
To be able search in lists sequentially
To understand and be able to use Binary search
To understand Selection sort

Ibrahim
Mesecan
Page 2 Searching and Sorting-I
Why do we need searching?
We frequently work with lists:
List of numbers,
List of names,
List of structures (books, students,)
Etc.
And in many cases, we need to search a specific
value in these lists:
The largest element
The smallest element
If a certain value exists or not
Etc.
Ibrahim
Mesecan
Page 3 Searching and Sorting-I
Types of searching
There are two common algorithms for
searching there.
Sequential Search: When the list is unordered. To
decide if a certain element is in the list or not, we
need to go through the entire list.
Binary Search: When the list is already sorted.

Ibrahim
Mesecan
Page 4 Searching and Sorting-I
Sequential search
Sequential Search: The list is unordered:
5, 3, 7, 9, 2, 10, 15, 3
Is there 6 in the list?
What is the smallest number in the list?
To decide such questions, we need to start from
the beginning of the list and go until the end of
the list.
Every element of the array is compared one by
one with the searching value.

Ibrahim
Mesecan
Page 5 Searching and Sorting-I
Sequential Search

int seqSearch(int *a, int n, int num)


{
for(int pos=0; pos<n; pos++)
if(a[pos]==num)
return pos;

return -1;
}

Ibrahim
Mesecan
Page 6 Searching and Sorting-I
Sequential Search Max

int searchMax(int *a, int n)


{
int maxPos=0;
for(int pos=1; pos<n; pos++)
if(a[pos]>a[maxPos])
maxPos = pos;

return maxPos;
}

Ibrahim
Mesecan
Page 7 Searching and Sorting-I
Binary search
The list is ordered:
2, 3, 3, 5, 7, 9, 10, 15, 25, 35, 37, 52, 61,
What is the smallest number in the list?
What is the biggest number
To decide such questions is very easy, just show
the first( or the last) element of the list as its
the smallest(or the largest) element in the list.
Is there 6 in the list?
We compare all the elements one by one with
the searching value.
Ibrahim
Mesecan
Page 8 Searching and Sorting-I
Binary search
The list is ordered:
2, 3, 3, 5, 7, 9, 10, 15, 25, 35, 37, 52, 61,
Is there 6 in the list?
We compare the number with the mid element
always.
If the number is bigger than mid number, then the
searching number might be in the upper half.
If the number is smaller than mid number, then the
searching number can be in the lower half of the list.
Otherwise (mid elements not greater and its not
smaller then the number), you found the number
Ibrahim
Mesecan
Page 9 Searching and Sorting-I
Binary search
The list is ordered; Searching for: 50
2, 3, 3, 4, 5, 7, 9, 10, 15, 25, 35, 37, 52, 61, 63,
Normally seq search takes 15 operations. For
Binary search, we compare 50 with mid num;
2, 3, 3, 4, 5, 7, 9, 10, 15, 25, 35, 37, 52, 61, 63,
Because 50 is greater than 10, then it may be
between 9th and 15th elements (15 and 63)
15, 25, 35, 37, 52, 61, 63,
50 is also greater than 37. Thus, it may be
between 13th and 15th elements (52 and 63)
Ibrahim
52, 61, 63,
Mesecan
Page 10 Searching and Sorting-I
Iterative Binary search

int bSearch(int *a,int l,int r, int num)


{
while(l <= r)
{
int mid = (l + r)/2;
if(a[mid] < num) l = mid+1;
else if(a[mid] > num) r = mid-1;
else return mid;
}
return -1;
}
Ibrahim
Mesecan
Page 11 Searching and Sorting-I
Insertion Sort

Click here to see the


animated image

Ibrahim
Mesecan
Page 12 Searching and Sorting-I
Insertion Sort
void InSort(int a[], int n)
{
for (int i = 1; i < n; i ++)
{
int k = a[i], j = i-1;
while ( j>=0 && a[j] > k )
{
a[j+1]=a[j];
j--;
}
a[j+1]=k;
}
}
Ibrahim
Mesecan
Page 13 Searching and Sorting-I
Selection Sort

Click here to see the


animated image

Ibrahim
Mesecan
Page 14 Searching and Sorting-I
Selection Sort

A[7]={5, 9, 17, 14, 3, 6, 10}


i j min

i position of active element


j Search starting from next of i until the end;
min the position of the current smallest
element

Ibrahim
Mesecan
Page 15 Searching and Sorting-I
Selection Sort
A[7]={5, 9, 17, 14, 3, 6, 10}
i j min
A[7]={3, 9, 17, 14, 5, 6, 10}
i j min
A[7]={3, 5, 17, 14, 9, 6, 10}
i j min

A[7]={3, 5, 6, 14, 9, 17, 10}


i j-min

A[7]={3, 5, 6, 9, 14, 17, 10}


i j min

A[7]={3, 5, 6, 9, 10, 17, 14}


i j-min

Ibrahim A[7]={3, 5, 6, 9, 10, 14, 17}


Mesecan
Page 16 Searching and Sorting-I
Selection Sort
A[7]={5, 9, 17, 14, 3, 6, 10}
i j min

For (i1 to n-1){// Active element


min=i; // position of smallest element

For(j=i+1 to n){//Search until the end


//If any element is smaller the smallest

if(A[j]<A[min])min=j;// new min


swap(A[i],A[min])// put smlst to active pos
}
Ibrahim
Mesecan
Page 17 Searching and Sorting-I
Selection Sort

For (i1 to n-1){// n-1 times

min=i;
For(j=i+1 to n){
if(A[j]<A[min])min=j This loop is
swap(A[i],A[min]) executed
} n-1 times

In the first pass, it counts from two until n (n-2 steps); In the
second pass, it has n-3 steps; then n-4, then. 3, 2,1 steps in
the end. That means 1+2+3n-3+n-2
Ibrahim
Mesecan
Page 18 Searching and Sorting-I
Selection Sort
void selSort(int a[], int n)
{
for (int i = 0; i < n-1; i++)
{
int min = i;
for (int j = i+1; j < n; j++)
if (a[j] < a[min])
min = j;
swap (a[min], a[i]);
}
}
Ibrahim
Mesecan
Page 19 Searching and Sorting-I

You might also like