2. History
Herman Hollerith’s card-sorting
Hollerith’s original (bad) idea: sort on most-
significant digit first.
Good idea: Sort on least-significant digit
first with auxiliary stable sort.
Digit-by-digit sort.
3. Definition
Radix sort is an integer sorting
algorithm that sorts data with integer keys
by grouping the keys by individual digits
that share the same significant position and
value (place value). Radix sort
uses counting sort as a subroutine to sort
an array of numbers. Because integers can
be used to
represent strings (by hashing the strings to
integers), radix sort works on data types
other than just integers.
5. Sample Program
#include <iostream>
using namespace std;
int size(int arr[], int a)
{
int high= arr[0];
for (int b = 1; b < a; b++)
if (arr[b] > high)
high = arr[b];
return high;
}
6. void countSort(int arr[], int a, int place)
{
int output[a], b, count[10] = {0};
for (b = 0; b < a; b++)
count[(arr[b] / place) % 10]++;
for (b = 1; b < 10; b++)
count[b] += count[b-1];
for (b = a - 1; b >= 0; b--)
{
output[count[(arr[b] / place) % 10] - 1] = arr[b];
count[(arr[b] / place) % 10]--;
}
for (b = 0; b < a; b++)
arr[b] = output[b];
}
7. void radixsort(int arr[], int a)
{
int place, m;
m = size(arr, a);
for (place = 1; m/place > 0; place *= 10)
countSort(arr, a, place);
}
8. int main()
{
int a, b;
cout<<"nEnter the number of records: ";
cin>>a;
int arr[a];
for(b = 0; b < a; b++)
{
cout<<"Enter number: "<<b+1<<": ";
cin>>arr[b];
}
radixsort(arr, a);
cout<<"nData are Sorted ";
for (b = 0; b < a; b++)
cout<<" "<<arr[b];
return 0;
}
11. History
Named after its inventor D. L. Shell.
This algorithm was improperly called the
Shell-Metzner sort by John P. Grillo
Crediting "one of the fastest" programs
for sorting by Fredrick Stuart
12. Definition
Shell sort is a generalization of insertion
sort that allows the exchange of items
that are far apart. The idea is to arrange
the list of elements so that, starting
anywhere, considering every hth
element gives a sorted list.
13. Operation of Shell Sort
To make it easy to understand, an interval of 4
positions is taken. The values are {35, 14}, {33,
19}, {42, 27} and {10, 44}
14. The values in each sub-list are compared
and swapped them (if necessary) in the
original array. After this step, the new array
appears as −
Then, the interval of 2 is taken and this gap
generates 2 sub-lists{14, 27, 35, 42}, {19,
10, 33, 44}
15. The values are compared and swapped, if
required, in the original array. After this step,
the array appears as −
16. Finally, the rest of the array is sorted using interval of value
1. Shell sort uses insertion sort to sort the array.
17. Sample Program
#include<iostream>
using namespace std;
void sort(int a[],int n)
{
int gap,i,j,temp;
for(gap=n/2;gap>0;gap/=2)
{
for(i=gap;i<n;i+=1)
{
temp=a[i];
for(j=i;j>=gap&&a[j-gap]>temp;j-=gap)
a[j]=a[j-gap];
a[j]=temp;
}
}
}
18. int main()
{
int a[20],i,n;
cout<<"Enter number of elements:";
cin>>n;
cout<<"Enter array elements:n";
for(i=0;i<n;++i)
cin>>a[i];
sort(a,n);
cout<<"nArray after shell sort:n";
for(i=0;i<n;++i)
cout<<a[i]<<" "
return 0;
}