-
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
24ba8d2
commit b74aea8
Showing
4 changed files
with
66 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,55 @@ | ||
|
||
func fastQuicksort(a: [Int]) -> [Int] { | ||
guard a.count > 1 else {return a} | ||
|
||
let pivot = a[0] | ||
let less = a.filter{$0 < pivot} | ||
let equal = a.filter{$0 == pivot} | ||
let greater = a.filter{$0 > pivot} | ||
let result = fastQuicksort(less) + equal + fastQuicksort(greater) | ||
return result | ||
} | ||
|
||
let list1 = [ 5,8,1,3,7,9,2 ] | ||
print (fastQuicksort(list1)) | ||
|
||
|
||
|
||
|
||
|
||
|
||
func partition(inout list : [Int], low: Int, high : Int) -> Int { | ||
let pivot = list[high] | ||
var j = low | ||
var i = j - 1 | ||
while j < high { | ||
if list[j] <= pivot{ | ||
i += 1 | ||
(list[i], list[j]) = (list[j], list[i]) | ||
} | ||
j += 1 | ||
} | ||
(list[i+1], list[high]) = (list[high], list[i+1]) | ||
return i+1 | ||
} | ||
|
||
func quikcSort(inout list : [Int] , low : Int , high : Int) { | ||
|
||
if low < high { | ||
let pIndex = partition(&list, low: low, high: high) | ||
quikcSort(&list, low: low, high: pIndex-1) | ||
quikcSort(&list, low: pIndex + 1, high: high) | ||
} | ||
} | ||
|
||
var list = [7,3,15,10,0,8,2,4] | ||
quikcSort(&list, low: 0, high: list.count-1) | ||
print(list) | ||
|
||
var list2 = [ 10, 0, 3, 9, 2, 14, 26, 27, 1, 5, 8, -1, 8 ] | ||
quikcSort(&list2, low: 0, high: list2.count-1) | ||
print(list2) | ||
|
||
var list3 = [1,3,9,8,2,7,5] | ||
quikcSort(&list3, low: 0, high: list3.count-1) | ||
print(list3) |
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
QuickSort_Final.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
+9.23 KB
...nd/playground.xcworkspace/xcuserdata/ahmadatef.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.