Skip to content

Commit bf3f3d5

Browse files
committed
Updating Counting sort for stability
1 parent 9aa3608 commit bf3f3d5

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

Counting Sort/CountingSort.playground/Contents.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,17 @@ func countingSort(array: [Int]) throws -> [Int] {
2929

3030
// Step 3
3131
// Place the element in the final array as per the number of elements before it
32+
// Loop through the array in reverse to keep the stability of the new array
33+
// (i.e. 7, is at index 3 and 6, thus in sortedArray the position of 7 at index 3 should be before 7 at index 6
3234
var sortedArray = [Int](repeating: 0, count: array.count)
33-
for element in array {
35+
for index in stride(from: array.count - 1, through: 0, by: -1) {
36+
let element = array[index]
3437
countArray[element] -= 1
3538
sortedArray[countArray[element]] = element
3639
}
40+
3741
return sortedArray
3842
}
3943

4044
try countingSort(array: [10, 9, 8, 7, 1, 2, 7, 3])
45+

Counting Sort/CountingSort.swift

+4-1
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ func countingSort(_ array: [Int])-> [Int] {
2929

3030
// Step 3
3131
// Place the element in the final array as per the number of elements before it
32+
// Loop through the array in reverse to keep the stability of the new sorted array
33+
// (For Example: 7 is at index 3 and 6, thus in sortedArray the position of 7 at index 3 should be before 7 at index 6
3234
var sortedArray = [Int](repeating: 0, count: array.count)
33-
for element in array {
35+
for index in stride(from: array.count - 1, through: 0, by: -1) {
36+
let element = array[index]
3437
countArray[element] -= 1
3538
sortedArray[countArray[element]] = element
3639
}

Counting Sort/README.markdown

+5-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ The code for step 2 is:
5050

5151
### Step 3:
5252

53-
This is the last step in the algorithm. Each element in the original array is placed at the position defined by the output of step 2. For example, the number 10 would be placed at an index of 7 in the output array. Also, as you place the elements you need to reduce the count by 1 as those many elements are reduced from the array.
53+
This is the last step in the algorithm. Each element in the original array is placed at the position defined by the output of step 2. For example, the number 10 would be placed at an index of 7 in the output array. Also, as you place the elements you need to reduce the count by 1 as those many elements are reduced from the array.
54+
We also have to loop through the array in reverse to keep the stability of the new sorted array.
55+
For Example: 7 is at index 3 and 6, thus in sortedArray the position of 7 at index 3 should be before 7 at index 6.
5456

5557
The final output would be:
5658

@@ -63,7 +65,8 @@ Here is the code for this final step:
6365

6466
```swift
6567
var sortedArray = [Int](repeating: 0, count: array.count)
66-
for element in array {
68+
for index in stride(from: array.count - 1, through: 0, by: -1) {
69+
let element = array[index]
6770
countArray[element] -= 1
6871
sortedArray[countArray[element]] = element
6972
}

0 commit comments

Comments
 (0)