Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Abo3aTef authored Dec 11, 2016
1 parent e4e64cf commit 24ba8d2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
39 changes: 39 additions & 0 deletions CountingSort.playground/Contents.swift
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)
4 changes: 4 additions & 0 deletions CountingSort.playground/contents.xcplayground
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>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.

0 comments on commit 24ba8d2

Please sign in to comment.