Skip to content

Commit 12a6b93

Browse files
authored
Merge pull request kodecocodes#934 from JMatharu/master
Updating Counting Sort for stability
2 parents e515919 + bf38b12 commit 12a6b93

File tree

6 files changed

+34
-6
lines changed

6 files changed

+34
-6
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

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
}

Counting Sort/Tests/Tests.xcodeproj/project.pbxproj

+3-2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
developmentRegion = English;
9898
hasScannedForEncodings = 0;
9999
knownRegions = (
100+
English,
100101
en,
101102
Base,
102103
);
@@ -226,7 +227,7 @@
226227
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
227228
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
228229
PRODUCT_NAME = "$(TARGET_NAME)";
229-
SWIFT_VERSION = 3.0;
230+
SWIFT_VERSION = 5.0;
230231
};
231232
name = Debug;
232233
};
@@ -238,7 +239,7 @@
238239
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
239240
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
240241
PRODUCT_NAME = "$(TARGET_NAME)";
241-
SWIFT_VERSION = 3.0;
242+
SWIFT_VERSION = 5.0;
242243
};
243244
name = Release;
244245
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

0 commit comments

Comments
 (0)