Skip to content

Commit e04ccb7

Browse files
committed
counting sort
1 parent 2931a3e commit e04ccb7

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@
496496

497497
![](https://github.com/aish21/Algorithms-and-Data-Structures/blob/main/Resources/Animations/Merge-Sort.gif)
498498

499-
### [Quick Sort](/Practice%20Concepts/Sorting/merge-sort.py)
499+
### [Quick Sort](/Practice%20Concepts/Sorting/quick-sort.py)
500500
* Based on the Divide and Conquer strategy
501501
* Method:
502502
1. Select the pivot element: Different variations have different methods of picking the pivot elements. The most basic one picks the right-most element as the pivot element.
@@ -515,6 +515,20 @@
515515

516516
![](https://github.com/aish21/Algorithms-and-Data-Structures/blob/main/Resources/Animations/quicksort.gif)
517517

518+
### [Counting Sort](/Practice%20Concepts/Sorting/quick-sort.py)
519+
* Sorts the elements of an array by counting the number of occurances of each unique element in the array.
520+
* The count is stored in an auxiliary array and the sorting is done by mapping the count as an index of the auxiliary array.
521+
* Method:
522+
- Find out the maximum element
523+
- Initialize an array of length max+1 with all elements 0. This array is used for storing the count of the elements in the array.
524+
- Store the count of each element at their respective index in the 'count' array
525+
- Store cumulative sum of the elements of the count array. It helps in placing the elements into the correct index of the sorted array.
526+
- Find the index of each element of the original array in the count array. This gives the cumulative count.
527+
- After placing each element at its correct position, decrease its count by one
528+
* Time Complexity - Best, Average and Worst: O(n + k) - There are mainly four main loops
529+
* Space Complexity - O(max)
530+
531+
![](https://github.com/aish21/Algorithms-and-Data-Structures/blob/main/Resources/Animations/counting-sort.gif)
518532

519533
## Search Algorithms
520534
<p align="center">
77 KB
Loading

0 commit comments

Comments
 (0)