Skip to content

Commit a10aa73

Browse files
Quick sort homework. All but accesscode-2-2#4
1 parent 5c30bc7 commit a10aa73

File tree

1 file changed

+122
-6
lines changed

1 file changed

+122
-6
lines changed

HWFrom1-30-16(QuickSort+Lists+Queues).playground/Contents.swift

+122-6
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,130 @@
33
//Answer the questions in the homework below
44
//https://docs.google.com/document/d/1KlK3PmRMybmHS5db_AP11uHXIcbIeM5vHuuAZOV3xME/edit#
55

6-
//1)
6+
//1)Without looking at the Big O Cheatsheet, write down the average time and space complexity for bubble sort, insertion sort, selection sort, mergesort, and quicksort.
77

8-
//2)
8+
// Bubble Sort = O(n^2) ... Because you have to iterate through n elements n times.
99

10-
//3)
10+
// Insertion Sort = O(n^2) ... Because, in the worst case, aside from looping through the unsorted array to grab the element to be compared, if the number being inserted is less than every element in the sorted list you will have to make a comparison with every element already sorted.
1111

12-
//4)
12+
// Selection Sort = O(n^2) ... Because after looping through the whole array to grab the minimum element and looping through the array minus 1 each time until everything is sorted, the run time is O(n^2)/2 which means the time complexity is still O(n^2) because when it comes to Big Oh complexitiy, we disregard the constant.
1313

14-
//5)
14+
// Merge Sort = O(n log n) ... Because it divides the input in half recursively and has to make n comparisons each time.
15+
// Quick Sort = O(n log n) ... Because you have to do n-1 comparisons on each iteration and log n becuase you have to divide the elements log n times
16+
17+
//2) What is the advantage of partitioning quicksort in place?
18+
// No need to create another array and deal with merging them.
19+
20+
//3) Without looking, implement quicksort. * I looked. Couldn't do it and it was taking too long.*
21+
22+
func quickSort(inout arr: [Int]) {
23+
quickSort(&arr, firstIdx: 0, lastIdx: arr.count-1)
24+
}
25+
26+
func quickSort(inout arr: [Int], firstIdx: Int, lastIdx: Int) {
27+
// base case
28+
if firstIdx - lastIdx >= 1 { return }
29+
30+
// partition
31+
let splitPoint = partition(&arr, firstIdx: firstIdx, lastIdx: lastIdx)
32+
33+
// quickSort on leftHalf
34+
quickSort(&arr, firstIdx: firstIdx, lastIdx: splitPoint - 1)
35+
36+
// quickSort on rightHalf
37+
quickSort(&arr, firstIdx: splitPoint + 1, lastIdx: lastIdx)
38+
}
39+
40+
func partition(inout arr: [Int], firstIdx: Int, lastIdx: Int) -> Int {
41+
// set pivotValue to firstElement
42+
let pivotValue = arr[firstIdx]
43+
// set leftMark
44+
var leftMark = firstIdx + 1
45+
// set rightMark
46+
var rightMark = lastIdx
47+
48+
/*
49+
as leftMark and rightMark close in on each other,
50+
swap the items that are greater than the pivot value
51+
on the left side with the items that are less than the pivot
52+
value on the right side. Stop when rightMark crosses leftMark
53+
*/
54+
while leftMark <= rightMark {
55+
while arr[leftMark] < pivotValue {
56+
leftMark += 1
57+
}
58+
while arr[rightMark] > pivotValue {
59+
rightMark -= 1
60+
}
61+
62+
if leftMark < rightMark {
63+
swap(&arr[leftMark], &arr[rightMark])
64+
}
65+
}
66+
// set the correct value at the splitPoint
67+
if firstIdx != rightMark {
68+
swap(&arr[firstIdx], &arr[rightMark])
69+
}
70+
return rightMark // return the splitPoint
71+
}
72+
73+
var arrayForQuickSort = [22, 15, 38, 93, 95, 0, 34, 58, 72, 59]
74+
75+
76+
quickSort(&arrayForQuickSort)
77+
78+
//4) Write a function to generate an array of random numbers bounded between 1..<10,000 of size 10,000.
79+
80+
// Int(arc4random_uniform(UInt32(10000)))
81+
82+
// Compare the time it takes to run mergesort, quicksort, and quicksort with the median.
83+
// https://gist.github.com/gummibeatz/8ff29bcec54d7e3ef683
84+
85+
86+
87+
//5) Describe the algorithmic difference between mergesort and quicksort. Where does the sorting happen? As the recursive calls are being pushed onto the stack or as they are being popped off?
88+
// For quicksort, the sorting happens as the recursive calls are being pushed onto the stack as opposed to mergesort which is sorted "on the way up" or as the stack is being popped off.
89+
90+
//6) Given an array of strings containing “[“,”]”,”{“,”}”,”(“,”)”. Output whether or not the parentheses are balanced.
91+
//Good examples: () [] () ([]()[])
92+
//Bad examples: ( ( ] ([)]
93+
94+
func samePair(element1: String?, element2: String) -> Bool {
95+
96+
if element1 == "{" && element2 == "}" {
97+
return true
98+
} else if element1 == "[" && element2 == "]" {
99+
return true
100+
} else if element1 == "(" && element2 == ")" {
101+
return true
102+
} else {
103+
return false
104+
}
105+
106+
}
107+
108+
func isBalanced(paren: [String]) -> Bool {
109+
110+
guard paren.count < 2 || paren.isEmpty || paren.count % 2 == 0 else {
111+
return false
112+
}
113+
114+
var emptyArray = [String]()
115+
let openingSet = Set(["(", "[", "{"])
116+
117+
for i in 0..<paren.count {
118+
if openingSet.contains(paren[i]) {
119+
emptyArray.append(paren[i])
120+
} else if emptyArray.isEmpty || !samePair(emptyArray.last, element2: paren[i]) {
121+
return false
122+
} else {
123+
emptyArray.removeLast()
124+
}
125+
}
126+
127+
return emptyArray.isEmpty
128+
}
129+
130+
var array = ["[","]","{","}","(",")"]
131+
isBalanced(array)
15132

16-
//6)

0 commit comments

Comments
 (0)