Anda di halaman 1dari 12

ALIGARH MUSLIM UNIVERSITY

Department of Computer Science


Course: MCA CS-112: Programming & Problem Solving Using C
Academic Session 2012-2013
Handout-11
Topic: Sorting & Searching Algorithms
Dr. Rafiqul Zaman Khan, Associate Professor.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Objectives:
To learn about the Bubble Sort, Linear search & Binary Search algorithms & their
implementation in C-Language.

What is sorting?
Sorting is the re-arrangement of a collection of data according to some key-field.
We shall consider only Bubble Sort of sorting methods in this Lecture.

Bubble sort:
The bubble sort algorithm is one of the simplest sorting algorithms.

It involves following steps:

a) Compare adjacent pairs of an array.


b) If the first is larger than the second, interchange the two elements.
c) Repeat the above two steps until the array is sorted in increasing order.

After one pass, the element with the largest key has been bubbled to the end of the list.
The procedure continues by successively increasing the starting point in the list by 1.

44 33 33 22
33 44 22 33
55 22 44 44
22 55 75 55
Original Array pass1 pass2 pass3
Total number of pass required in bubble sort algorithm =n-1

Total number of comparisons required in first pass =n-1


Total number of comparisons required in second pass =n-2
Total number of comparisons required in third pass =n-3
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- -- - -- - - - -
- -- - - - - - - - - - - - - -
Total number of comparisons required in (n-1)s pass = n-(n-1)=1
Where n is size of the list (or Array).

1
Algorithm for bubble sort:
void b_sort(int x[], int size)
{
int pass, comp, temp;
for(pass=1; pass<=size-1; pass++)
for(comp=1; comp<=size-pass; comp++)
if(x[comp-1]>x[comp])
{
temp=x[comp-1];
x[comp-1]=x[comp];
x[comp]=temp;
} // end of if
return;
} // end of function

Implementation of bubble sort algorithm in C-Language:

#include<stdio.h>
void b_sort(int x[], int size)
{
int pass, comp, temp;
for(pass=1; pass<=size-1; pass++)
for(comp=1; comp<=size-pass; comp++)
if(x[comp-1]>x[comp])
{
temp=x[comp-1];
x[comp-1]=x[comp];
x[comp]=temp;
}
return;
}

void main()
{
int i, size;
int list[10];
printf("Enter size of list which is to be sorted : ");
scanf("%d",&size);
printf ("Enter % d elements of the list from key board : \n",size);
printf("-----------------------------------------------------\n");

for(i=0; i<=size-1; i++)


{
scanf("%d", &list[i]); // to read array from key board
}

2
b_sort(list,size); // Function call

printf("\nThe sorted list is : \n");


printf("*******************************");

for(i=0; i<=size-1; i++)


{
printf("\n %d", list[i]);
}
return ;
}
Sample Output:

What is searching?

Searching means scanning though a list of items or records to find a particular one exists.
It usually requires the user to specify the target item or target key.
If the target item is found, the record or its location is returned, otherwise, an appropriate
message or flag is returned.

Basic searching algorithms are: Linear search (or Sequential Search) and Binary Search.

Linear Search can be used for sorted and unsorted data both.
Binary Search can be used only for sorted data. If we have sorted list of data use of
binary search is more efficient in comparison to linear search.

Linear Search:

This involves searching through the list sequentially until the target item is found or the
list is exhausted.
If the target is found, its location is returned, otherwise a flag such as 1 is returned.

Algorithm for Linear Search:

3
Let given a sorted or unsorted array, determine whether a given value or item or
data is present in the array or not:

int lin_search(int x[], int size, int key)


{
int k;
for(k=0; k<size; k++)
if(key==x[k])
return 1;
return 0;
}
OR

int lin_search (int list[], int size, int target)


{
int loc=0;
while(loc<size && list[loc] != target)
loc++;
if(list[loc]==target)
return loc;
else
return -1;
}

Implementation of Linear Search algorithm in C-Language:

#include<stdio.h>
int lin_search (int list[], int size, int target)
{
int loc=0;
while(loc<size && list[loc] != target)
loc++;
if(list[loc]==target)
return loc;
else
return -1;
} // end of lin_search function

void main( )
{
int size, key, LOC, i;
int list[10];
printf("Enter size of list(array) : ");

4
scanf("%d", &size);
printf ("Enter % d elements of the list from key board : \n", size);
printf("-------------------------------------------------------------------\n");

for(i=0; i<size; i++)


{
scanf("%d", &list[i]); // to read array from key board
}
printf("-------------------------------------------------------------------\n");
printf("Please Input target or key value which you want to search in list :");
scanf("%d", &key);

LOC = lin_search(list, size, key); // Function call

if(LOC !=-1)
{
printf("\nThe location of target %d in list of array is : %d\n ", key,LOC);
printf("\n\n\t(Dear, Remember position of first array value counted from 0)\n");
}
else
printf("\nDear Sorry , target %d not found in list of array", key);

return ;
} // end of main

Sample Output:

5
Binary Search:

For a list of n elements, the linear search takes an average of n/2 comparisons to
find an item, with the best case being 1 comparison and the worst case being n
comparisons.

However, if the list is ordered, it is a waste of time to look for an item using linear
search (it would be like looking for a word in a dictionary sequentially). In this
case we apply binary search, which will be more efficient.

Binary search works by comparing the target with the item at the middle of the
list. This leads to one of three results:

1. If the middle item is the target we are done.


2. If the middle item is less than target in that case we apply the algorithm
to the upper half of the list.
3. If the middle item is bigger than the target in that case we apply the
algorithm to the lower half of the list.This process is repeated until the
item is found or the list is exhausted.

The following functions implements this approach using both iteration and
recursion.

In this assuming that the array is sorted in increasing order:

Algorithm for Binary Search:

Iterative Function for Binary Search:

int bin_search1(int x[], int low, int high, int target)


{
int middle;

while (low <= high)


{
middle = (low + high)/2;
if (target == x[middle])
return (middle);
else if (x[middle] < target)
low = middle + 1;
else
high = middle 1;
}
return (-1);
}

6
Recursive Function for Binary Search:

int bin_search2( int x[], int low, int high, int target)
{
int middle;
if (low > high) /*base case1:target not found*/
return 1;
middle = (low + high)/2;
if (x[middle] == target)
return (middle); /*base case2:target found*/
else if (x[middle] < target)
return(bin_search2(x, middle+1,high,target)
else
return(bin_search2(x, low, middle-1,target)
}

Explanation for Binary Search:

NOTE: In this diagram first is used in place of low, last is used in place of high.

7
Implementation of binary search algorithm in C-Language:

#include <stdio.h>
#define MAX_ARY_SIZE 12
int binarySearch (int x[], int high, int target, int *locn); //function prototype

int main ()
{
int i ,locn, target;
int ary[MAX_ARY_SIZE] = { 4, 7, 8, 10, 14, 21, 22, 36, 62, 77, 81, 91 };
printf("Data: ");
for (i = 0; i < MAX_ARY_SIZE; i++)
printf("%3d", ary[i]);
printf("\n\nEnter a key for search : ");
scanf("%d", &target);

do
{
if (binarySearch (ary, MAX_ARY_SIZE - 1, target, &locn))
printf("%3d found at location: %2d\n", target, locn);
else
printf("%3d NOT found at locn: %2d\n", target, locn);
printf("Enter next key or -1 to quit: ");
scanf("%d", &target);
} while (target != -1);

printf("\n\nEnd of search.\n");
return 0;
} /* end of main */

int binarySearch (int x[ ],int end, int target, int *locn)


{
int low, mid, high;
low = 0;
high=end;

while (low <= high)


{
mid = (low + high) / 2;
if (target > x[mid])
/* look in upper half */
low = mid + 1;
else if (target < x[mid])
/* look in lower half */
high = mid - 1;
else

8
/* found equal: force exit */
low = high + 1;
} /* end while */

*locn = mid;
return target == x [mid];
} /*end of binarySearch function*/

Sample Output:

Exercises

Problem#1:
Write a program using bubble sort algorithm to sort a list of names in alphabetical
order.

Answer:

#include<stdio.h>
#include<string.h>
#define ITEMS 5
#define MAXCHAR 20

void main()
{
char string[ITEMS][MAXCHAR],dummy[MAXCHAR];
int i=0,j=0;
printf("Enter names of %d items\n",ITEMS);

while(i<ITEMS)
{
scanf("%s",string[i++]);
} // end of while

9
for(i=1;i<ITEMS;i++)
{
for(j=1;j<+ITEMS-i;j++)
{
if(strcmp(string[j-1],string[j])>0)
{
strcpy(dummy,string[j-1]);
strcpy(string[j-1],string[j]);
strcpy(string[j],dummy);
} // end of if
} // end of inner for

printf("\nAlphabetical list is :\n\n");


for(i=0;i<ITEMS;i++)
printf("%s\n",string[i]);
} // end of outer for
} // end of main

Problem#2:
How many comparisons are needed in best and worst cases of Binary search algorithms?
Explain with example.

Answer:

Complexity of the Binary Search Algorithm:

The complexity is measured by the number f(n) of comparisons to locate ITEM in DATA
where DATA contains n elements. Observe that each comparison reduces the sample size
in half. Hence we require at most f(n) comparisons to locate ITEM where

2f(n) > n OR equivalently f(n)=[log2 n]+1

That is, the running time for the worst case is approximately equal to log2 n . One can
also show that the running time for the average case is approximately equal to the
running time for the worst case.

Problem#3:
Suppose DATA list contains 1000 000 elements. Estimate number of comparisons
required using binary search algorithm for finding location of an item in data array with
1000000 elements?

Answer:

Observe that

210 =1024>100 and hence 220 >10002 =1000 000

10
Accordingly, using the binary search algorithm, one requires only about 20 comparisons
to find the location of an item in a data array with 1000 000 elements.

Problem#4:
What are the limitations of binary search algorithm? Explain.

Answer:

Limitations of Binary Search Algorithm:

Since the binary search algorithm is very efficient (e.g., it requires only about 20
comparisons with an initial list of 1 000000 elements), why would one want to use any
other search algorithm?

Observe that the algorithm requires two conditions:

(1) The list must be sorted and


(2) One must have direct access to the middle element in any sub list. This means that
one must essentially use a sorted array to hold the data. But keeping data in a sorted array
is normally very expensive when there are many insertions and deletions. Accordingly, in
such situations, one may use a different data structure, such as a linked list or a binary
search tree, to store the data.

Problem#5:
Let DATA be the following sorted 13-elements array:

DATA: 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99

Suppose you apply binary search algorithm to search ITEM=40, show all intermediate
steps for searching this ITEM.

Answer:

Our search item is ITEM=40. The search for Item is in the array DATA is pictured
below, where the values of DATA [BEG] and DATA [END] in each stage of the
algorithm are indicated by green color and the values of DATA [MID] by a pink color.

Specifically, BEG, END, and MID will have the following successive values:

1)
Initially, BEG=0 and END=12,
Hence MID=(0+12)/2=6 and so DATA[MID]=55

2)
Since 40<55, END has its value changed by END=MID-1=5

11
Hence MID=(0+5)/2=2 and so DATA[MID]=30

3)
Since 40>30, BEG has its value changed by BEG=MID+1=3. hence
MID=(3+5)/2=4 and so DATA[MID]=40

We have found ITEM in location LOC=MID=4

1) 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99
2) 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99
3) 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99 [Successful]

Fig: Binary search for ITEM=40.

Problem#6:
Trace binary search algorithm for ITEM=85 for above DATA items.

Answer: Students will do during Lecture.

Problem#7:
Given that the following array of integers is stored in sorted order as shown below:

5 10 15 20 25 30 35 40 45 50 55 60 65

What will the number of comparisons that would be made in a search for the value of 50
if:

i) Linear Search Algorithm is used?


ii) Binary Search Algorithm is used?
Explain your answer.

Answer:
i) In sequential search: 10 Comparisons.
ii) In binary search algorithm : Ist comparison with 35 (Middle Element)
II nd comparison with 50 (New middle)
Found: 2 Comparison made (Follow all steps)

12

Anda mungkin juga menyukai