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.
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
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
#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");
2
b_sort(list,size); // Function call
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.
3
Let given a sorted or unsorted array, determine whether a given value or item or
data is present in the array or not:
#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");
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:
The following functions implements this approach using both iteration and
recursion.
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)
}
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 */
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
Problem#2:
How many comparisons are needed in best and worst cases of Binary search algorithms?
Explain with example.
Answer:
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
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
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:
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?
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
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]
Problem#6:
Trace binary search algorithm for ITEM=85 for above DATA items.
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:
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