-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Abo3aTef
authored
Dec 11, 2016
1 parent
e4e64cf
commit 24ba8d2
Showing
4 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
|
||
func countingSort(list:[Int]) -> [Int]? { | ||
if let max = list.maxElement(){ | ||
|
||
// 1. Counting | ||
var countingList = [Int](count: Int(max + 1), repeatedValue: 0) | ||
for n in list{ | ||
countingList[n] += 1 | ||
} | ||
|
||
// 2. Sum | ||
for i in 1..<countingList.count{ | ||
countingList[i] += countingList[i-1] | ||
} | ||
|
||
|
||
// 3. Substract & Index. | ||
|
||
var sortedList = [Int] (count: Int(list.count), repeatedValue: 0) | ||
|
||
for num in list{ | ||
countingList[num] -= 1 | ||
sortedList[countingList[num]] = num | ||
} | ||
print(sortedList) | ||
return sortedList | ||
} | ||
return nil | ||
} | ||
|
||
|
||
var list = [9,4,15,7,1,1,7,2,10,9,0,1] | ||
countingSort(list) | ||
|
||
var inputList1 = [9,8,5,6,4,2,2,1,1] | ||
countingSort(inputList1) | ||
|
||
var inputList2 = [1,4,1,2,7,5] | ||
countingSort(inputList2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='5.0' target-platform='ios' executeOnSourceChanges='false'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |
7 changes: 7 additions & 0 deletions
7
CountingSort.playground/playground.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file added
BIN
+7.81 KB
...nd/playground.xcworkspace/xcuserdata/ahmadatef.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.