-
Notifications
You must be signed in to change notification settings - Fork 1
/
SortingAlgorithms.scala
320 lines (274 loc) · 8.49 KB
/
SortingAlgorithms.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package examples
import scala.util.Random
/**
* Some examples which probably involves writing nested
* loops with mutable state in imperative style of programming.
*
* Illustrating functional way of writing such code.
* Both programs probably have the same asymptotic
* complexity
*
*/
object SortingAlgorithms {
/**
* Insertion sort algorithm(https://en.wikipedia.org/wiki/Insertion_sort)
* typically has nested loops with mutable state in imperative style of program
*
* Steps of an insertion sort:
* 3 7 4 9 5 2 6 1
* 3 7 4 9 5 2 6 1
* 3 7 4 9 5 2 6 1
* 3 4 7 9 5 2 6 1
* 3 4 7 9 5 2 6 1
* 3 4 5 7 9 2 6 1
* 2 3 4 5 7 9 6 1
* 2 3 4 5 6 7 9 1
* 1 2 3 4 5 6 7 9
*
* @param input
* @return
*/
def insertionSort(input: List[Int]): List[Int] = {
input.foldLeft(List[Int]())( (acc, element) => {
val (firstHalf, secondHalf) = acc.span(_ < element)
//inserting the element at the right place
firstHalf ::: element :: secondHalf
})
}
/**
*
* Quick sort algorithm (https://en.wikipedia.org/wiki/Quicksort)
*
* 1. Pick an element, called a pivot, from the array.
* 2. all elements with values less than the pivot come before the pivot;
* all elements with values greater than the pivot come after it (equal
* values can go either way)
* 3. Recursively apply the above steps to the sub-array of elements
* with smaller values and separately to the sub-array of elements
* with greater values.
*
* The implementation below is not tail recursive. So there is a possibility
* of StackOverflowError.
*
* @param input
* @return
*/
def quickSort(input: List[Int]): List[Int] = {
/**
* This method divides the given list into three sublists
* using a random pivot.
* (less than pivot, equal to pivot, greater than pivot)
*
* @param list
* @return
*/
def pivot(list: List[Int]): (List[Int], List[Int], List[Int]) = {
val randomPivot = list(new Random().nextInt(input.length))
list.foldLeft(List[Int](), List[Int](), List[Int]())( (acc, element) => {
val (lessThanPivot, equalToPivot, greaterThanPivot) = acc
element match {
case x if x < randomPivot => (x :: lessThanPivot, equalToPivot, greaterThanPivot)
case x if x > randomPivot => (lessThanPivot, equalToPivot, x :: greaterThanPivot)
case x @ _ => (lessThanPivot, x ::equalToPivot, greaterThanPivot)
}
})
}
input match {
case Nil => Nil
case oneElementList @ List(x) => oneElementList
case head :: tail => {
val (lessThanPivot, equalToPivot, greaterThanPivot) = pivot(input)
//step 2 & 3
quickSort(lessThanPivot) :::
equalToPivot :::
quickSort(greaterThanPivot)
}
}
}
/**
* Bubble sort algorithm(https://en.wikipedia.org/wiki/Bubble_sort)
*
* First Pass:
* ( 5 1 4 2 8 ) \to ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
* ( 1 5 4 2 8 ) \to ( 1 4 5 2 8 ), Swap since 5 > 4
* ( 1 4 5 2 8 ) \to ( 1 4 2 5 8 ), Swap since 5 > 2
* ( 1 4 2 5 8 ) \to ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.
*
* Second Pass:
* ( 1 4 2 5 8 ) \to ( 1 4 2 5 8 )
* ( 1 4 2 5 8 ) \to ( 1 2 4 5 8 ), Swap since 4 > 2
* ( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
* ( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
*
* Now, the array is already sorted, but the algorithm does not know if it is completed.
* The algorithm needs one whole pass without any swap to know it is sorted.
*
* Third Pass:
* ( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
* ( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
* ( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
* ( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
*
* This algorithm can be further improved by stopping when no swapping has been
* performed during a bubble call.
*
* @param input
* @return
*/
def bubbleSort(input: List[Int]): List[Int] = {
/**
*
* For input of ( 5 1 4 2 8 )
* returns (8, List(1 4 2 5)) -> First pass from the example above.
*
* @param remaining
* @return a tuple with the maxElement in the given list and the rest of the list
*/
def bubble(remaining: List[Int]): (Int, List[Int]) = {
remaining match {
case first :: second :: tail => {
if (first >= second) {
val (maxElement, untraversed) = bubble(first :: tail)
(maxElement, second :: untraversed)
}
else {
val (maxElement, untraversed) = bubble(second :: tail)
(maxElement, first :: untraversed)
}
}
case maxElement :: Nil => (maxElement, Nil)
}
}
/**
* For a given unsorted list, accumulates results in resultList
* by calling bubble each time and adding the maxElement to the
* resultList with each call.
*
* @param unsorted
* @param resultList
* @return
*/
def sort(unsorted: List[Int], resultList: List[Int]): List[Int] = {
unsorted match {
case Nil => Nil
case oneElementList @ List(x) => oneElementList
case _ :: _ => {
val (maxElement, tail) = bubble(unsorted)
tail match {
case head :: Nil => head :: maxElement :: resultList
case _ :: _ => sort(tail, maxElement :: resultList)
}
}
}
}
sort(input, List[Int]())
}
/**
* Merge sort algorithm(https://en.wikipedia.org/wiki/Merge_sort)
*
* Start : 3--4--2--1--7--5--8--9--0--6
* Select runs : 3--4 2 1--7 5--8--9 0--6
* Merge : 2--3--4 1--5--7--8--9 0--6
* Merge : 1--2--3--4--5--7--8--9 0--6
* Merge : 0--1--2--3--4--5--6--7--8--9
*
* @param input
* @return
*/
def mergeSort(input: List[Int]): List[Int] = {
/**
* Merges two sorted lists
*
* @param list1
* @param list2
* @return
*/
def merge(list1: List[Int], list2: List[Int]): List[Int] = {
(list1, list2) match {
case (head :: Nil, Nil) => list1
case (Nil, head :: Nil) => list2
case (head1 :: tail1, Nil) => list1
case (Nil, head1 :: tail1) => list2
case (head1 :: tail1, head2 :: tail2) => {
if (head1 < head2) {
head1 :: merge(tail1, list2)
} else {
head2 :: merge(list1, tail2)
}
}
}
}
/**
* Performs merge sort but dividing the input list into n sublists
* and repeatedly merging them to produce sorted sublists until it
* has one final sorted list.
*
* @param list
* @return
*/
def sort(list: List[Int]): List[Int] = {
val midPoint = if (list.size % 2 == 0)
list.size / 2
else
list.size / 2 + 1
list match {
case Nil => Nil
case head :: Nil => list
case head :: tail => {
val (firstPart, secondPart) = list.splitAt(midPoint)
merge( sort(firstPart), sort(secondPart) )
}
}
}
sort(input)
}
/**
*
* Selection sort algorithm (https://en.wikipedia.org/wiki/Selection_sort)
*
* 64 25 12 22 11 // this is the initial, starting state of the array
* 11 64 25 12 22 // sorted sublist = {11}
* 11 12 64 25 22 // sorted sublist = {11, 12}
* 11 12 22 64 25 // sorted sublist = {11, 12, 22}
* 11 12 22 25 64 // sorted sublist = {11, 12, 22, 25}
* 11 12 22 25 64 // sorted sublist = {11, 12, 22, 25, 64}
*
* @param input
* @return
*/
def selectionSort(input: List[Int]): List[Int] = {
/**
* Returns a tuple with the minimum element and rest of
* the list.
*
* @param currentMin
* @param remaining
* @return
*/
def findMin(currentMin: Int, remaining: List[Int]): (Int, List[Int]) = {
remaining match {
case Nil => (currentMin, Nil)
case head :: tail => {
if (currentMin > head) {
val (newMin, unsorted) = findMin(head, tail)
(newMin, currentMin :: unsorted)
} else {
val (newMin, unsorted) = findMin(currentMin, tail)
(newMin, head :: unsorted)
}
}
}
}
def sort(list: List[Int]): List[Int] = {
list match {
case Nil => Nil
case head :: Nil => list
case head :: tail => {
val (min, unsorted) = findMin(head, tail)
min :: sort(unsorted)
}
}
}
sort(input)
}
}