0% found this document useful (0 votes)
3 views36 pages

Unit 1 Counting

The document discusses non-comparison sorting algorithms, highlighting their differences from comparison sorting algorithms, which have a performance limit of Ω(n log n). It introduces three types of non-comparison sorting: Counting sort, Radix sort, and Bucket sort, which operate under specific assumptions about the data. The document provides a detailed explanation of the Counting sort algorithm, including its steps and example execution.

Uploaded by

pooja0100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views36 pages

Unit 1 Counting

The document discusses non-comparison sorting algorithms, highlighting their differences from comparison sorting algorithms, which have a performance limit of Ω(n log n). It introduces three types of non-comparison sorting: Counting sort, Radix sort, and Bucket sort, which operate under specific assumptions about the data. The document provides a detailed explanation of the Counting sort algorithm, including its steps and example execution.

Uploaded by

pooja0100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 36

Non-Comparison Sorting

Algorithms
Examples of Comparison Sorting
1. Bubble sort
2. Insertion sort
3. Selection sort
4. Quick sort
5. Heap sort
6. Merge sort

2
Limitations of Comparison Sorting
 There are fundamental limits on the performance of
comparison sorts.
 To sort n elements, comparison sorts must make (n log n)
comparisons in the worst case.
 That is a comparison sort must have a lower bound of
Ω(n log n) comparison operations, which is known as linear
or linearithmic time.
 This is a consequence of the limited information available
through comparisons alone

3
Non Comparison Sorting
 There are some sorting algorithms that perform sorting without
comparing the elements rather by making certain assumptions
about the data. They are called the non comparison sorting.
 Non comparison sorting include:
1. Counting sort (indexes using key values)
2. Radix sort (examines individual bits of keys)
3. Bucket sort (examines bits of keys)
 These are Linear sorting algorithms. Linear sorts are NOT
“comparison sorts”.
 They make certain assumptions about the data. These type of
sorting algorithm does not need to go through the comparison
decision tree.

4
Counting sort
 Counting sort assumes that each of the n input elements is an
integer in the range 0 to k. that is n is the number of elements and
k is the highest value element.
 Consider the input set : 4, 1, 3, 4, 3. Then n=5 and k=4
 Counting sort determines for each input element x, the number of
elements less than x. And it uses this information to place
element x directly into its position in the output array. For
example if there exits 17 elements less that x then x is placed into
the 18th position into the output array.
 The algorithm uses three array:
Input Array: A[1..n] store input data where A[j]  {1, 2, 3, …, k}
Output Array: B[1..n] finally store the sorted data
Temporary Array: C[1..k] store data temporarily
Counting Sort
1. Counting-Sort(A, B, k)
2. Let C[0…..k] be a new array
3. for i=0 to k
4. C[i]= 0;
5. for j=1 to A.length or n
6. C[ A[j] ] = C[ A[j] ] + 1;
7. for i=1 to k
8. C[i] = C[i] + C[i-1];
9. for j=n or A.length down to 1
10. B[ C[ A[j] ] ] = A[j];
11. C[ A[j] ] = C[ A[j] ] - 1;
Counting Sort
1. Counting-Sort(A, B, k)
2. Let C[0…..k] be a new array
3. for i=0 to k [Loop 1]
4. C[i]= 0;
5. for j=1 to A.length or n [Loop 2]
6. C[ A[j] ] = C[ A[j] ] + 1;
7. for i=1 to k [Loop 3]
8. C[i] = C[i] + C[i-1];
9. for j=n or A.length down to 1 [Loop 4]
10. B[ C[ A[j] ] ] = A[j];
11. C[ A[j] ] = C[ A[j] ] - 1;
Counting-sort example
1 2 3 4 5 6 7 8

A: 5 3 0 2 3 0 3

0 1 2 3 4 5

C:

1 2 3 4 5 6 7 8

B:
Executing Loop 1
1 2 3 4 5 6 7 8

A: 5 3 0 2 3 0 3

0 1 2 3 4 5

C: 0 0 0 0 0 0

1 2 3 4 5 6 7 8

B:
Executing Loop 2
1 2 3 4 5 6 7 8

A: 5 3 0 2 3 0 3

0 1 2 3 4 5

C: 0 0 1 0 0 0

1 2 3 4 5 6 7 8

B:
Executing Loop 2
1 2 3 4 5 6 7 8

A: 5 3 0 2 3 0 3

0 1 2 3 4 5

C: 0 0 1 0 0 1

1 2 3 4 5 6 7 8

B:
Executing Loop 2
1 2 3 4 5 6 7 8

A: 5 3 0 2 3 0 3

0 1 2 3 4 5

C: 0 0 1 1 0 1

1 2 3 4 5 6 7 8

B:
Executing Loop 2
1 2 3 4 5 6 7 8

A: 5 3 0 2 3 0 3

0 1 2 3 4 5

C: 1 0 1 1 0 1

1 2 3 4 5 6 7 8

B:
Executing Loop 2
1 2 3 4 5 6 7 8

A: 5 3 0 2 3 0 3

0 1 2 3 4 5

C: 1 0 2 1 0 1

1 2 3 4 5 6 7 8

B:
Executing Loop 2
1 2 3 4 5 6 7 8

A: 5 3 0 2 3 0 3

0 1 2 3 4 5

C: 1 0 2 2 0 1

1 2 3 4 5 6 7 8

B:
Executing Loop 2
1 2 3 4 5 6 7 8

A: 5 3 0 2 3 0 3

0 1 2 3 4 5

C: 2 0 2 2 0 1

1 2 3 4 5 6 7 8

B:
Executing Loop 2
1 2 3 4 5 6 7 8

A: 5 3 0 2 3 0 3

0 1 2 3 4 5

C: 2 0 2 3 0 1

1 2 3 4 5 6 7 8

B:
End of Loop 2
1 2 3 4 5 6 7 8

A: 5 3 0 2 3 0 3

0 1 2 3 4 5

C: 2 0 2 3 0 1

1 2 3 4 5 6 7 8

B:
Executing Loop 3
1 2 3 4 5 6 7 8

A: 5 3 0 2 3 0 3

0 1 2 3 4 5

C: 2 0 2 3 0 1
0 1 2 3 4 5

C: 2 2 2 3 0 1

1 2 3 4 5 6 7 8

B:
Executing Loop 3
1 2 3 4 5 6 7 8

A: 5 3 0 2 3 0 3

0 1 2 3 4 5

C: 2 2 2 3 0 1
0 1 2 3 4 5

C: 2 2 4 3 0 1

1 2 3 4 5 6 7 8

B:
Executing Loop 3
1 2 3 4 5 6 7 8

A: 5 3 0 2 3 0 3

0 1 2 3 4 5

C: 2 2 4 3 0 1
0 1 2 3 4 5

C: 2 2 4 7 0 1

1 2 3 4 5 6 7 8

B:
Executing Loop 3
1 2 3 4 5 6 7 8

A: 5 3 0 2 3 0 3

0 1 2 3 4 5

C: 2 2 4 7 0 1
0 1 2 3 4 5

C: 2 2 4 7 7 1

1 2 3 4 5 6 7 8

B:
Executing Loop 3
1 2 3 4 5 6 7 8

A: 5 3 0 2 3 0 3

0 1 2 3 4 5

C: 2 2 4 7 7 1
0 1 2 3 4 5

C: 2 2 4 7 7 8

1 2 3 4 5 6 7 8

B:
End of Loop 3
1 2 3 4 5 6 7 8

A: 5 3 0 2 3 0 3

0 1 2 3 4 5

C: 2 2 4 7 7 8

1 2 3 4 5 6 7 8

B:
Executing Loop 4
1 2 3 4 5 6 7 8

A: 5 3 0 2 3 0 3

0 1 2 3 4 5

C: 2 2 4 7 7 8

1 2 3 4 5 6 7 8

B:
Executing Loop 4
1 2 3 4 5 6 7 8

A: 5 3 0 2 3 0 3

J=8, then A[ j ]=A[8]=3


0 1 2 3 4 5 And B[ C[ A[j] ] ]
=B[ C[ 3 ] ]
C: 2 2 4 7 7 8 =B[ 7]
So B[ C[ A[j] ] ] ←A[ j ]
=B[7]←3

1 2 3 4 5 6 7 8

B:
Executing Loop 4
1 2 3 4 5 6 7 8

A: 5 3 0 2 3 0 3

J=8, then A[ j ]=A[8]=3


0 1 2 3 4 5 Then C[ A[j] ]
= C[ 3 ]
C: 2 2 4 6 7 8 =7
So C[ A[j] ] = C[ A[j] ] -1
=7-1=6

1 2 3 4 5 6 7 8

B: 3
Executing Loop 4
1 2 3 4 5 6 7 8

A: 5 3 0 2 3 0 3

0 1 2 3 4 5

C: 1 2 4 6 7 8

1 2 3 4 5 6 7 8

B: 0 3
Executing Loop 4
1 2 3 4 5 6 7 8

A: 5 3 0 2 3 0 3

0 1 2 3 4 5

C: 1 2 4 5 7 8

1 2 3 4 5 6 7 8

B: 0 3 3
Executing Loop 4
1 2 3 4 5 6 7 8

A: 5 3 0 3 0 3

0 1 2 3 4 5

C: 1 2 3 5 7 8

1 2 3 4 5 6 7 8

B: 0 2 3 3
Executing Loop 4
1 2 3 4 5 6 7 8

A: 5 3 0 3 0 3

0 1 2 3 4 5

C: 0 2 3 5 7 8

1 2 3 4 5 6 7 8

B: 0 0 2 3 3
Executing Loop 4
1 2 3 4 5 6 7 8

A: 5 3 0 3 0 3

0 1 2 3 4 5

C: 0 2 3 4 7 8

1 2 3 4 5 6 7 8

B: 0 0 2 3 3 3
Executing Loop 4
1 2 3 4 5 6 7 8

A: 2 5 3 0 3 0 3

0 1 2 3 4 5

C: 0 2 3 4 7 7

1 2 3 4 5 6 7 8

B: 0 0 2 3 3 3 5
End of Loop 4
1 2 3 4 5 6 7 8

A: 5 3 0 3 0 3

0 1 2 3 4 5

C: 0 2 2 4 7 7

1 2 3 4 5 6 7 8

B: 0 0 2 2 3 3 3 5

Sorted data in Array B


Time Complexity Analysis
1. Counting-Sort(A, B, k)
2. Let C[0…..k] be a new array
Loop 1 and 3
3. for i=0 to k [Loop 1] takes O(k) time
4. C[i]= 0;
5. for j=1 to A.length or n [Loop 2]
6. C[ A[j] ] = C[ A[j] ] + 1;
7. for i=1 to k [Loop 3]
8. C[i] = C[i] + C[i-1];
9. for j=n or A.length down to 1 [Loop 4]
10. B[ C[ A[j] ] ] = A[j]; Loop 2 and 4
takes O(n) time
11. C[ A[j] ] = C[ A[j] ] - 1;
Time Complexity Analysis
• So the counting sort takes a total time of: O(n + k)
• Counting sort is called stable sort.
– A sorting algorithm is stable when numbers with
the same values appear in the output array in the
same order as they do in the input array.

– Space complexity of counting is sort is: O(n+k)

You might also like