Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
16 views6 pages

1.4 Sorting in Linear Time

The document discusses three linear time sorting algorithms: Counting Sort, Radix Sort, and Bucket Sort. Counting Sort operates by counting occurrences of each element and placing them in the correct position, with a time complexity of θ(k+n). Radix Sort sorts numbers by their digits from least to most significant, while Bucket Sort distributes elements into buckets based on their value, both achieving a time complexity of θ(n).
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

1.4 Sorting in Linear Time

The document discusses three linear time sorting algorithms: Counting Sort, Radix Sort, and Bucket Sort. Counting Sort operates by counting occurrences of each element and placing them in the correct position, with a time complexity of θ(k+n). Radix Sort sorts numbers by their digits from least to most significant, while Bucket Sort distributes elements into buckets based on their value, both achieving a time complexity of θ(n).
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

5.

Sorting in Linear Time:


5.1 Count Sort:

 Counting sort assumes that each of the n input elements is an integer in the range 0 to k, for some
integer k.
 The basic idea of counting sort is to determine, for each input element x, the number of elements
less than x. This information can be used to place element x directly into its position in the output
array.
o Eg. if there are 17 elements less than x, then x belongs in output position 18.

5.1.1 Algorithm:

Counting-Sort (A, B, k)
Where A is an array of n elements (non-negative integers), B is an output array of n size; k is the
maximum value of Array A.

1. for i 0 to k
2. do C[i]  0 // C is an array of (k+1) size.
3. for j 1 to length [A]
4. do C[A[j]] C[A[j]] + 1
5. for i 1 to k
6. do C[i]  C[i] + C[i-1]
7. for j length[A] down to 1
8. do B[C[A[j]]]  A[j]
9. C[A[j]]  C[A[j]] – 1

5.1.2 Analysis of Counting Sort:

There are 4 for loops in the algorithm. Two loops are running n times and two are running k times.
Therefore the time complexity of Counting Sort is θ(k+n). In practice, we usually use counting sort when
we have k= θ(n), in which case the running time is θ(n).

Example: Illustrate the operation of Counting-Sort on the array A= {6,0,2,0,1,3,4,6,1,3,2}.

1 2 3 4 5 6 7 8 9 10 11
A 6 0 2 0 1 3 4 6 1 3 2

Maximum number in A is 6. Create an array C from 0 to 6 with initial value 0.


0 1 2 3 4 5 6
C 0 0 0 0 0 0 0

Read array A from 1-10, increment in C[A[i]] by one.


0 1 2 3 4 5 6
C 2 2 2 2 1 0 2

Now calculate C[i]= C[i] + C[i-1]

37 | P a g e
Shashank Yadav(IT-dept KIET)
0 1 2 3 4 5 6
C 2 4 6 8 9 9 11

Now array C tells us the exact position of number A[i] in Array B.


Eg. number 2 will be placed at 6th position of array B.
Now read array A from 11-1, and place element in array B according to array C.
And decrease C[i] by one every time.
0 1 2 3 4 5 6
C 0 2 4 6 8 9 9

1 2 3 4 5 6 7 8 9 10 11
B 0 0 1 1 2 2 3 3 4 6 6

5.1.3 C program for Count Sort.

#include<stdio.h>
#include<conio.h>
#include<alloc.h>

int Maximum(int *A,int n)


{
int max, i;
max=A[1];
for(i=2;i<=n;i++)
{
if(max<A[i])
{
max=A[i];
}
}
return(max);
}

void CountSort(int *A, int *B, int n, int k)


{
int i,j, *C;
C=(int*)calloc((k+1),sizeof(int));

for(j=1;j<=n;j++)
C[A[j]]= C[A[j]]+1;

for(i=1;i<=k;i++)
C[i]= C[i]+C[i-1];

for(j=n;j>=1;j--)
{
B[C[A[j]]]= A[j];
C[A[j]]= C[A[j]]-1;

38 | P a g e
Shashank Yadav(IT-dept KIET)
}
free(C);
}

void main()
{
int *A, *B, n, i, k;
clrscr();

printf("\nEnter number of elements in Array ");


scanf("%d",&n);

A=(int*)malloc((n+1)*sizeof(int));
B=(int*)malloc((n+1)*sizeof(int));

printf("\nEnter %d numbers ",n);


for(i=1;i<=n;i++)
scanf("%d",&A[i]);

k=Maximum(A,n);

CountSort(A,B,n,k);

printf("\nSorted Array is \n");

for(i=1;i<=n;i++)
printf("%d\t",B[i]);
free(A);
free(B);
getch();
}

5.2 Radix Sort:

 Radix sort solves the problem of card sorting, by sorting on the least significant digit first.
 For sorting numbers we take 10 columns (0, 1, 2… 9), and for sorting alphabetic words we take
26 columns (a, b… z).
 There should be same digit numbers for radix sort.
 Firstly we arrange the numbers in the columns according to least digit of numbers, and collect
numbers according to columns. After then we arrange the numbers in the columns according to
2nd digit of numbers, and collect numbers according to columns. And continue this step for all
digits.

5.2.1 Algorithm:

RADIX-SORT(A, d)
Where A is an array of d digits numbers.
1. for i= 1 to d
a. use a stable sort to sort array A on digit i

Time complexity of Radix-Sort is θ(n).

39 | P a g e
Shashank Yadav(IT-dept KIET)
11.7.2 Example

Initial Array
348 143 361 423 538 128 321 543 366

Input 0 1 2 3 4 5 6 7 8 9
348 348
143 143
361 361
423 423
538 538
128 128
321 321
543 543
366 366
a) First Pass

Input 0 1 2 3 4 5 6 7 8 9
361 361
321 321
143 143
423 423
543 543
366 366
348 348
538 538
128 128
b) Second Pass

Input 0 1 2 3 4 5 6 7 8 9
321 321
423 423
128 128
538 538
143 143
543 543
348 348
361 361
366 366
c) Third Pass
Sorted Array is
128 143 321 348 361 366 538 543

40 | P a g e
Shashank Yadav(IT-dept KIET)
11.7.3 C program for Radix Sort.

#include<stdio.h>
#include<conio.h>

void main()
{
int A[50],B[50],n,i,j,k,d,m,x,digit,r;
clrscr();
printf("Enter number of elments in the array ");
scanf("%d",&n);
printf("\nEnter %d same digits numbers ",n);
for(i=0;i<n;i++)
scanf("%d",&A[i]);

digit=0;
x=A[0];
while(x!=0)
{
digit++;
x=x/10;
}

for(k=1;k<=digit;k++)
{
j=0;
for(d=0;d<=9;d++)
{
for(i=0;i<n;i++)
{
x=A[i];
for(m=1;m<=k;m++)
{
r=x%10;
x=x/10;
}
if(r==d)
{
B[j]=A[i];
j++;
}
}
}
printf("\n\n");
for(i=0;i<n;i++)
{
A[i]=B[i];
printf("%d ",A[i]);
}

41 | P a g e
Shashank Yadav(IT-dept KIET)
printf("\nSorted Array is \n");
for(i=0;i<n;i++)
printf("%d ",A[i]);
getch();
}

5.3 Bucket Sort:

 Bucket sort runs in linear time when the input is drawn from a uniform distribution.
 Bucket sort assumes that the input is generated by a random process that distributes elements
uniformly over the interval [0,1).
 The idea of bucket sort is to divide the interval [0,1) into n equal-sized subintervals, or buckets,
and then distribute the n input numbers into the buckets.

5.3.1 Algorithm

Bucket-Sort (A)
Where A is an array n floating point numbers.

1. n  length[A]
2. for i 1 to n
3. do insert A[i] into list B[ └n A[i] ┘]
4. for i 0 to n-1
5. do sort list B[i] with insertion sort
6. Concatenate the lists B[0], B[1], . . . , B[n-1] together in order

Time complexity of Bucket sort is θ(n).

5.3.2 Example:

42 | P a g e
Shashank Yadav(IT-dept KIET)

You might also like