Module 5, Chapter 14
Searching and Sorting
Introduction
• Searching means to find whether a particular
value is present in an array or not.
• If the value is present in the array, then searching
is said to be successful and the searching process
gives the location of that value in the array.
• However, if the value is not present in the array,
the searching process displays an appropriate
message and in this case searching is said to be
unsuccessful.
Types
• There are two popular methods for searching
the array elements:
1. linear search
2. binary search
Linear Search
• Linear search, also called as sequential search, is a very
simple method used for searching an array for a
particular value.
• It works by comparing the value to be searched with
every element of the array one by one in a sequence
until a match is found.
• Linear search is mostly used to search an unordered
list of elements (array in which data elements are not
sorted).
Example
• If an array A[] is declared and initialized as,
int A[] = {10, 8, 2, 7, 3, 4, 9, 1, 6, 5};
and the value to be searched is VAL = 7,
then searching means to find whether the value ‘7’ is
present in the array or not.
• If yes, then it returns the position of its
occurrence.
• Here, POS = 3 (index starting from 0).
LINEAR_SEARCH(A,N,VAL,POS)
• Step 1: [INITIALIZE] SET POS = -1
• Step 2: [INITIALIZE] SET I = 0
• Step 3: Repeat Step 4 while I<N
• Step 4: IF A[I] = VAL, then
• SET POS = I
• PRINT POS
• Go to Step 6
• [END OF IF]
• [END OF LOOP]
• Step 5: PRINT “Value Not Present In The Array”
• Step 6: EXIT
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define size 20 // Added so the size of the array
can be altered more easily
int main(int argc, char *argv[])
{
int arr[size], num, i, n, found = 0, pos = -1;
printf("\n Enter the number of elements in
the array : ");
scanf("%d", &n);
printf("\n Enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
printf("\n Enter the number that has to be
searched : ");
scanf("%d", &num);
for(i=0;i<n;i++)
{
if(arr[i] == num)
{
found =1;
pos=i;
printf("\n %d is found in the array at
position= %d",num,i+1);
break;
}
}
if (found == 0)
printf("\n %d does not exist in the array",
num);
return 0;
}
Binary Search
• Binary search is a searching algorithm that
works efficiently with a sorted list.
• The mechanism of binary search can be better
understood by an analogy of a telephone
directory.
• Now, let us consider how this mechanism is
applied to search for a value in a sorted array.
• Consider an array A[] that is declared and
initialized as
int A[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
• and the value to be searched is VAL = 9.
The algorithm will proceed in the following manner.
• BEG = 0, END = 10, MID = (0 + 10)/2 = 5
• Now, VAL = 9 and A[MID] = A[5] = 5
• A[5] is less than VAL, therefore, we will now search for the
value in the later half of the array. So, we change the values
of BEG and MID.
• Now, BEG = MID + 1 = 6, END = 10, MID = (6 + 10)/2 =16/2 =
8
• Now, VAL = 9 and A[MID] = A[8] = 8
• A[8] is less than VAL, therefore, we will now search for the
value in the later half of the array. So, again we change the
values of BEG and MID.
• Now, BEG = MID + 1 = 9, END = 10, MID = (9 + 10)/2 = 9
• Now VAL = 9 and A[MID] = 9.
BINARY_SEARCH(A, lower_bound, upper_bound, VAL,
POS)
• Step 1: [INITIALIZE] SET BEG = lower_bound, END =
upper_bound, POS = -1
• Step 2: Repeat Step 3 and Step 4 while BEG <= END
• Step 3: SET MID = (BEG + END)/2
• Step 4: IF A[MID] = VAL, then
• POS = MID
• PRINT POS
• Go to Step 6
• IF A[MID] > VAL then;
• SET END = MID - 1
• ELSE
• SET BEG = MID + 1
• [END OF IF]
• [END OF LOOP]
• Step 5: IF POS = -1, then
• PRINTF “VAL IS NOT PRESENT IN THE ARRAY”
• [END OF IF]
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define size 10 // Added to make changing size of array easier
int smallest(int arr[], int k, int n); // Added to sort array
void selection_sort(int arr[], int n); // Added to sort array
int main(int argc, char *argv[])
{
int arr[size], num, i, n, beg, end, mid, found=0;
printf("\n Enter the number of elements in the array:
");
scanf("%d", &n);
printf("\n Enter the elements: ");
for(i=0;i<n;i++)
{
printf("\n The sorted array is: \n");
for(i=0;i<n;i++)
printf(" %d\t", arr[i]);
printf("\n\n Enter the number that has to be
searched: ");
scanf("%d", &num);
beg = 0, end = n-1;
while(beg<=end)
{
mid = (beg + end)/2;
if (arr[mid] == num)
{
printf("\n %d is present in the array
at position %d", num, mid+1);
found =1;
break;
}
else if (arr[mid]>num)
end = mid-1;
else
beg = mid+1;
}
if (beg > end && found == 0)
printf("\n %d does not exist in the array",
num);
return 0;
}
INTERPOLATION SEARCH
• Interpolation search, also known as
extrapolation search, is a searching technique
that finds a specified value in a sorted array.
• For example, when looking for a name “Bharat”
in a telephone directory, we know that it will be
near the extreme left, so applying a binary
search technique by dividing the list in two
halves each time is not a good idea.
• We must start scanning the extreme left in the
first pass itself.
• In each step of interpolation search, the
remaining search space for the value to be
found is calculated.
• The calculation is done based on the values at
the bounds of the search space and the value
to be searched.
• The value found at this estimated position is
then compared with the value being searched
for. If the two values are equal, then the
search is complete.
Difference
• However, the important difference between the
two techniques is that binary search always selects
the middle value of the remaining search space.
• It discards half of the values based on the
comparison between the value found at the
estimated position and the value to be searched.
• But in interpolation search, interpolation is used to
find an item near the one being searched for, and
then linear search is used to find the exact item.
• Given a list of numbers a[] = {1, 3, 5, 7, 9, 11, 13, 15,
17, 19, 21}. Search for value 19 using interpolation
search technique.
Solution
• Low = 0, High = 10, VAL = 19, a[Low] = 1, a[High] = 21
Middle = Low + (High – Low)×((VAL – a[Low]) /(a[High] –
a[Low] ))
= 0 +(10 – 0) × ((19 – 1) / (21 – 1) )
= 0 + 10 × 0.9 = 9
a[middle] = a[9] = 19 which is equal to value to be
searched.
Write a program to search an element in an array using
interpolation search.
#include <stdio.h>
#include <conio.h>
#define MAX 20
int interpolation_search(int a[], int low, int high, int val)
{
int mid;
while(low <= high)
{
mid = low + (high – low)*((val – a[low]) /
(a[high] – a[low]));
if(val == a[mid])
return mid;
high = mid – 1;
else
low = mid + 1;
}
return –1;
}
int main()
{
int arr[MAX], i, n, val, pos;
clrscr();
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
printf("\n Enter the elements : ");
for(i = 0; i <n; i++)
scanf("%d", &arr[i]);
printf("\n Enter the value to be searched : ");
scanf("%d", &val);
pos = interpolation_search(arr, 0, n–1, val);
if(pos == –1)
printf("\n %d is not found in the array", val);
else
printf("\n %d is found at position %d", val, pos);
getche();
return 0;
JUMP SEARCH
• In jump search, it is not necessary to scan all the elements in the list to
find the desired value.
• We just check an element and if it is less than the desired value, then
some of the elements following it are skipped by jumping ahead.
• After moving a little forward again, the element is checked.
• If the checked element is greater than the desired value, then we have
a boundary and we are sure that the desired value lies between the
previously checked element and the currently checked element.
• However, if the checked element is less than the value being searched
for, then we again make a small jump and repeat the process.
• Once the boundary of the value is determined,
a linear search is done to find the value and its
position in the array.
• For example, consider an array
a[] = {1,2,3,4,5,6,7,8,9}
• The length of the array is 9.
• If we have to find value 8 then following steps
are performed using the jump search technique.
Advantage of Jump Search over Linear
Search
• Suppose we have a sorted list of 1000
elements where the elements have values 0,
1, 2, 3, 4,…, 999, then sequential search will
find the value 674 in exactly 674 iterations.
• But with jump search, the same value can be
found in 44 iterations.
• Hence, jump search performs far better than a
linear search on a sorted list of elements.
Advantage of Jump Search over Binary
Search
• No doubt, binary search is very easy to implement,
but in case of a list having very large number of
elements, jumping to the middle of the list to make
comparisons is not a good idea because if the value
being searched is at the beginning of the list then one
(or even more) large step(s) in the backward direction
would have to be taken.
• In such cases, jump search performs better as we have
to move little backward that too only once.
• Hence, when jumping back is slower than jumping
forward, the jump search algorithm always performs
better.
How to Choose the Step Length?
• For the jump search algorithm to work efficiently,
we must define a fixed size for the step.
• If the step size is 1, then algorithm is same as
linear search.
• Now, in order to find an appropriate step size, we
must first try to figure out the relation between
the size of the list (n) and the size of the step (k).
• Usually, k is calculated as √n.
#include <stdio.h>
#include <math.h>
#include <conio.h>
#define MAX 20
int jump_search(int a[], int low, int high, int val, int n)
{
int step, i;
step = sqrt(n);
for(i=0;i<step;i++)
{
if(val < a[step])
high = step – 1;
else
low = step + 1;
}
for(i=low;i<=high;i++)
{
if(a[i]==val)
return i;
}
return –1;
}
int main()
{
int arr[MAX], i, n, val, pos;
clrscr();
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
printf("\n Enter the elements : ");
for(i = 0; i <n; i++)
scanf("%d", &arr[i]);
printf("\n Enter the value to be searched : ");
scanf("%d", &val);
pos = jump_search(arr, 0, n–1, val, n);
if(pos == –1)
printf("\n %d is not found in the array", val);
else
printf("\n %d is found at position %d", val, pos);
getche();
return 0;
}
INTRODUCTION TO SORTING
• Sorting means arranging the elements of an
array so that they are placed in some relevant
order which may be either ascending or
descending.
• There are two types of sorting:
1. Internal sorting which deals with sorting the
data stored in the computer’s memory
2. External sorting which deals with sorting the
data stored in files. External sorting is applied
when there is voluminous data that cannot
be stored in the memory.
INSERTION SORT
• Insertion sort is a very simple sorting
algorithm in which the sorted array (or list) is
built one element at a time.
• The main idea behind insertion sort is that it
inserts each item into its proper place in the
final list.
Technique
Insertion sort works as follows:
1. The array of values to be sorted is divided into two sets. One
that stores sorted values and another that contains unsorted
values.
2. The sorting algorithm will proceed until there are elements in
the unsorted set.
3. Suppose there are n elements in the array. Initially, the
element with index 0 (assuming LB = 0) is in the sorted set.
Rest of the elements are in the unsorted set.
4. The first element of the unsorted partition has array index 1 (if
LB = 0).
5. During each iteration of the algorithm, the first element in the
unsorted set is picked up and inserted into the correct position
in the sorted set.
Advantages of Insertion Sort
• It is easy to implement and efficient to use on small sets of data.
• It can be efficiently implemented on data sets that are already
substantially sorted.
• It performs better than algorithms like selection sort and bubble sort.
• Insertion sort algorithm is simpler than shell sort, with only a small
trade-off in efficiency.
• It is over twice as fast as the bubble sort and almost 40 per cent
faster than the selection sort.
• It requires less memory space (only O(1) of additional memory
space).
• ΣI t is said to be online, as it can sort a list as and when it receives
new elements.
INSERTION-SORT (ARR, N)
• Step 1: Repeat Steps 2 to 5 for K = 1 to N – 1
• Step 2: SET TEMP = ARR[K]
• Step 3: SET J = K - 1
• Step 4: Repeat while TEMP <= ARR[J]
• SET ARR[J + 1] = ARR[J]
• SET J = J - 1
• [END OF INNER LOOP]
• Step 5: SET ARR[J + 1] = TEMP
• [END OF LOOP]
• Step 6: EXIT
#include <stdio.h>
#include <conio.h>
#define size 5
void insertion_sort(int arr[], int n);
void main()
{
int arr[size], i, n;
printf("\n Enter the number of elements in the array: ");
scanf("%d", &n);
printf("\n Enter the elements of the array: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
insertion_sort(arr, n);
printf("\n The sorted array is: \n");
for(i=0;i<n;i++)
printf(" %d\t", arr[i]);
getch();
void insertion_sort(int arr[], int n)
{
int i, j, temp;
for(i=1;i<n;i++)
{
temp = arr[i];
j = i-1;
while((temp < arr[j]) && (j>=0))
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = temp;
}
}
Quick Sort
The quick sort algorithm works as follows:
1. Select an element pivot from the array elements
2. Re-arrange the elements in the array in such a way that all
elements that are less than the pivot appear before the
pivot and all elements greater than the pivot element come
after it (equal values can go either way). After such a
partitioning, the pivot is placed in its final position. This is
called the partition operation.
3. Recursively sort the two sub-arrays thus obtained. (One
with sub-list of lesser value than that of the pivot element
and the other having higher value elements).
• The main task in the quick sort algorithm is to
find the pivot element, which will partition the
array into two halves. To understand how we
find the pivot element follow the steps given
below. (we will take the first element in the
array as pivot)
1. Set the index of the first element in the array
to loc and left variables. Also set the index of
the last element of the array to the right
variable.
That is, loc =0, left = 0 and right = n-1
(where n in the number of elements in the
array)
2. Start from the element pointed by right and scan the array
from right to left, comparing each element on the way with
the element pointed by variable loc.
That is, a[loc]should be less than a[right].
a) If that is the case then simply continue comparing
until right becomes equal to loc. Because once right = loc,
then it means the pivot has been placed in its correct
position.
b) However, if at any point we have a[loc]>a[right] then,
interchange the two values and jump to step 3.
c) Set loc = right
3. Start from the element pointed by left and scan the array
from left to right, comparing each element on the way with
the element pointed by variable loc.
That is, a[loc]should be greater than a[left].
a) If that is the case then simply continue comparing until
left becomes equal to loc. Because once left = loc, then it
means the pivot has been placed in its correct position.
b) However, if at any point we have a[loc]<a[left] then,
interchange the two values and jump to step 2
c) Set loc = left.
Quick Sort
Example: Consider the array given below and sort the elements using quick sort
algorithm.
27 10 36 18 25 45
We choose the first element as the pivot. Set loc = 0, left = 0,
right = 5 as,
27 10 36 18 25 45
Loc Righ
Lef t
t
Scan from right to left. Since a[loc] < a[right], decrease the value of right.
27 10 36 18 25 45
Loc Righ
Lef t
t
Quick Sort
Since, a[loc] > a[right], interchange the two values and set loc = right.
25 10 36 18 27 45
Lef Right, Loc
t
25 10 36 18 27 45
Lef
t Right, Loc
Quick Sort
Start scanning from left to right. Since, a[loc] > a[left], increment the
value of left.
25 10 36 18 27 45
Lef
Right, Loc
t
Now since, a[loc] < a[left], interchange the values and set loc = left
25 10 27 18 36 45
Left, Loc
Right
,
Quick Sort
Scan from right to left. Since a[loc] < a[right], decrement the value of
right.
25 10 27 18 36 45
Left, Loc Right
,
Since, a[loc] > a[right], interchange the two values and set loc =
right. 25 10 18 27 36 45
Le Right, Loc
ft
Start scanning from left to right. Since, a[loc] > a[left], increment
the value of left. 25 10 18 27 36 45
Left, Right,
Loc
PARTITION ( ARR, BEG, END, LOC)
Step 1: [Initialize] SET LEFT = BEG, RIGHT = END, LOC = BEG,
FLAG = 0
Step 2: Repeat Steps 3 to while FLAG = 0
Step 3: Repeat while ARR[LOC]<= ARR[RIGHT] AND LOC!= RIGHT
SET RIGHT = RIGHT – 1
[END OF LOOP]
Step 4: IF LOC == RIGHT, then
SET FLAG = 1
ELSE IF ARR[LOC] > ARR[RIGHT], then
SWAP ARR[LOC] with ARR[RIGHT]
SET LOC = RIGHT
[END OF IF]
Step 5:IF FLAG = 0, then
Repeat while ARR[LOC] >= ARR[LEFT] AND LOC != LEFT
SET LEFT = LEFT + 1
[END OF LOOP]
Step 6: IF LOC == LEFT, then
SET FLAG = 1
ELSE IF ARR[LOC] < ARR[LEFT], then
SWAP ARR[LOC] with ARR[LEFT]
SET LOC = LEFT
[END OF IF]
[END OF IF]
Step 7:[END OF LOOP]
• #include <stdio.h>
• #include <conio.h>
• #define size 100
• int partition(int a[], int beg, int end);
• void quick_sort(int a[], int beg, int end);
• void main()
• {
int arr[size], i, n;
printf("\n Enter the number of elements in the array: ");
scanf("%d", &n);
printf("\n Enter the elements of the array: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
quick_sort(arr, 0, n-1);
printf("\n The sorted array is: \n");
for(i=0;i<n;i++)
{
printf(" %d\t", arr[i]); getch();
}
}
• int partition(int a[], int beg, int end)
{
int left, right, temp, loc, flag;
loc = left = beg; right = end;
flag = 0;
while(flag != 1)
{
while((a[loc] <= a[right]) && (loc!=right))
right--;
if(loc==right)
flag =1;
else if(a[loc]>a[right])
{
temp = a[loc];
a[loc] = a[right];
a[right] = temp;
loc = right;
}
if(flag!=1)
{
while((a[loc] >= a[left]) && (loc!=left))
left++;
if(loc==left)
flag =1;
else if(a[loc] <a[left])
{
temp = a[loc];
a[loc] = a[left];
a[left] = temp;
loc = left;
}
}
} return loc;
}
void quick_sort(int a[], int beg, int end)
{
int loc;
if(beg<end)
{
loc = partition(a, beg, end);
quick_sort(a, beg, loc-1);
quick_sort(a, loc+1, end);
}
}
Merge Sort
• Merge sort is a sorting algorithm that uses the divide, conquer
and combine algorithmic paradigm. Where,
• Divide means partitioning the n-element array to be sorted into
two sub-arrays of n/2 elements in each sub-array. (If A is an array
containing zero or one element, then it is already sorted.
However, if there are more elements in the array, divide A into
two sub-arrays, A1 and A2, each containing about half of the
elements of A).
• Conquer means sorting the two sub-arrays recursively using
merge sort.
• Combine means merging the two sorted sub-arrays of size n/2
each to produce the sorted array of n elements.
Merge Sort
The basic steps of a merge sort algorithm are as
follows:
• If the array is of length 0 or 1, then it is already
sorted. Otherwise:
• (Conceptually) divide the unsorted array into two
sub- arrays of about half the size.
• Use merge sort algorithm recursively to sort each
sub-array
• Merge the two sub-arrays to form a single sorted
list
Merge Sort
Example: Sort the array given below using merge sort
9 9 81 45 90 27 72 18
Divide and conquer the array
39 9 81 45 90 27 72 18
39 9 81 45
90 27 72 18
90 27 72 18
39 9 81 45
90 27 72 18
39 9 81 45
39 9 81 45 00 27 18 72
9 39 45 81 27 90 18 72
9 39 45 81 18 27 72 90
9 18 39 45 72 81 90
Combine the elements to form a sorted array
Merge Sort
• To understand the merge algorithm, consider figure 14.4 which
shows how we merge two lists to form one list. For the sake of
understanding we have taken two sub-lists each containing four
elements. The same concept can be utilized to merge 4 sub-lists
containing two elements, and eight sub-lists having just one
element.
9 39 45 81 18 27 72 90
BEG, I MID J
END
TEMP
INDEX
9 39 45 81 18 27 72 90
BEG I mID J END
Merge Sort
TEMP
9 18
INDEX
9 39 45 81 18 27 72 90
BEG I Mid J END
TEMP
9 18 27
INDEX
9 39 45 81 18 27 72 90
BEG I Mid J END
TEMP
9 18 27 39
INDEX
Merge Sort
9 39 45 81 18 27 72 90
BEG I Mid J END
9 18 27 39 45
INDEX
9 39 45 81 18 27 72 90
BEG I,Mid J END
9 18 27 39 45 72
INDEX
When I is greater than MID copy the remaining elements of the right sub-array in TEMP
9 18 27 39 45 72 72 81 90
INDEX
Merge Sort
MERGE (ARR, BEG, MID, END)
Step 1: [Initialize] SET I = BEG, J = MID + 1, INDEX = 0
Step 2: Repeat while (I <= MID) AND (J<=END)
IF ARR[I] < ARR[J], then
SET TEMP[INDEX] = ARR[I]
SET I = I + 1
ELSE
SET TEMP[INDEX] = ARR[J]
SET J = J + 1
[END OF IF]
SET INDEX = INDEX + 1
[END OF LOOP]
Step 3: [ Copy the remaining elements of right sub-array, if any] IF I > MID, then
Repeat while J <= END
SET TEMP[INDEX] = ARR[J]
SET INDEX = INDEX + 1, SET J = J + 1
[END OF LOOP]
[Copy the remaining elements of left sub-array, if any] Else
Repeat while I <= MID
SET TEMP[INDEX] = ARR[I]
SET INDEX = INDEX + 1, SET I = I + 1
[END OF LOOP]
[END OF IF]
Step 4: [Copy the contents of TEMP back to ARR] SET K=0
Step 5: Repeat while K < INDEX
a. SET ARR[K] = TEMP[K]
b. SET K = K + 1
[END OF LOOP]
Step 6: END
Merge Sort
MERGE_SORT( ARR, BEG, END)
Step 1: IF BEG < END, then
SET MID = (BEG + END)/2
CALL MERGE_SORT( ARR, BEG, MID)
CALL MERGE_SORT (ARR, MID + 1, END)
MERGE (ARR, BEG, MID, END)
[END OF IF]
Step 2: END
Radix Sort
• Radix sort is a linear sorting algorithm for integers that
uses the concept of sorting names in alphabetical order.
• When we have a list of sorted names, the radix is 26 (or 26
buckets) because there are 26 letters of the alphabet.
• Observe that words are first sorted according to the first
letter of the name.
• That is, 26 classes are used to arrange the names, where
the first class stores the names that begins with ‘A’, the
second class contains names with ‘B’, so on and so forth.
Radix Sort (conti..)
• During the second pass, names are grouped
according to the second letter.
• After the second pass, the names are sorted
on the first two letters.
• This process is continued till nth pass, where n
is the length of the names with maximum
letters.
Radix Sort (conti..)
• After every pass, all the names are collected in
order of buckets.
• That is, first pick up the names in the first
bucket that contains names beginning with ‘A’.
• In the second pass collect the names from the
second bucket, so on and so forth.
Radix SortOn integers
• When radix sort is used on integers, sorting is
done on each of the digits in the number.
• The sorting procedure proceeds by sorting the
least significant to most significant digit.
• When sorting numbers, we will have ten
buckets, each for one digit (0, 1, 2…, 9) and
the number of passes will depend on the
length of the number having maximum digits.
• Example: Sort the numbers given below using
radix sort.
345, 654, 924, 123, 567, 472, 555, 808, 911
• Hint - In the first pass the numbers are sorted
according to the digit at ones place.
Pass 1
Pass 2
Pass 3
Advantages of Radix Sort
• Radix sort is a very simple algorithm.
• When programmed properly, radix sort is one
of the fastest sorting algorithms for numbers
or strings of letters.
Disadvantages of Radix Sort
1. Radix sort takes more space than other sorting
algorithms.
• Besides the array of numbers, we need 10 buckets to sort
numbers, 26 buckets to sort strings containing only
characters.
2. Another drawback of radix sort is that the algorithm
is dependent on digits or letters.
• For every different data type, the algorithm has to be
rewritten.
• Even if the sorting order changes, the algorithm has to be
rewritten.
WAP to implemnt Radix Sort
#include <stdio.h>
#include <conio.h>
#define size 10
int largest(int arr[], int n);
void radix_sort(int arr[], int n);
void main()
{
int arr[size], i, n;
printf("\n Enter the number of elements in the array: ");
scanf("%d", &n);
printf("\n Enter the elements of the array: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
radix_sort(arr, n);
printf("\n The sorted array is: \n");
for(i=0;i<n;i++)
printf(" %d\t", arr[i]);
getch();
int largest(int arr[], int n)
{
int large=arr[0], i;
for(i=1;i<n;i++)
{
if(arr[i]>large)
large = arr[i];
}
return large;
}
void radix_sort(int arr[], int n)
{
int bucket[size][size], bucket_count[size];
int i, j, k, remainder, NOP=0, divisor=1, large, pass;
large = largest(arr, n);
while(large>0)
{
NOP++;
large/=size;
}
for(pass=0;pass<NOP;pass++) // Initialize the buckets
{
for(i=0;i<size;i++)
bucket_count[i]=0;
for(i=0;i<n;i++)
{
remainder = (arr[i]/divisor)%size;
bucket[remainder][bucket_count[remainder]] =
arr[i];
bucket_count[remainder] += 1;
}
// collect the numbers after PASS pass
i=0;
for(k=0;k<size;k++)
{
for(j=0;j<bucket_count[k];j++)
{
arr[i] = bucket[k][j];
i++;
}
}
divisor *= size;
}
}
}