File tree 6 files changed +95
-0
lines changed
6 files changed +95
-0
lines changed Original file line number Diff line number Diff line change @@ -75,6 +75,15 @@ The next two have a node (SLinkedListNode or DLinkedListNode) and the implementa
75
75
76
76
* Is prime
77
77
78
+ ## Sorting
79
+
80
+ ### Python
81
+
82
+ * Insertion O(n^2)
83
+ * Bubble O(n^2)
84
+ * Merge O(nlogn)
85
+ * Quick O(nlogn)
86
+
78
87
## Concepts
79
88
80
89
### Greedy
Original file line number Diff line number Diff line change @@ -29,3 +29,4 @@ Sourced: https://github.com/CoreyMSchafer/code_snippets/blob/master/Regular-Expr
29
29
30
30
\* Note captured group and be recombined with the ** #** followed by the group number
31
31
32
+ To test out regex use [ regex101.com] ( https://regex101.com/ )
Original file line number Diff line number Diff line change
1
+ """ Bubble sort O(n^2) """
2
+
3
+ def bubbleSort (arr ):
4
+ # bubbles the largest number up
5
+
6
+ # for all elements in array (skip first element)
7
+ for i in range (len (arr )):
8
+ for j in range (len (arr )- i - 1 ):
9
+ # swap elements if smaller
10
+ if arr [j ] > arr [j + 1 ]:
11
+ arr [j ], arr [j + 1 ] = arr [j + 1 ], arr [j ]
Original file line number Diff line number Diff line change
1
+ """ Insertion sort O(n^2) """
2
+
3
+ def insertionSort (arr ):
4
+ # for all elements in array (skip first element)
5
+ for i in range (1 , len (arr )):
6
+ # examining element
7
+ key = arr [i ]
8
+
9
+ # start from the one below the current and move down the array
10
+ j = i
11
+ while j > 0 and key < arr [j - 1 ]:
12
+ # shift one down
13
+ arr [j ] = arr [j - 1 ]
14
+ j -= 1
15
+ arr [j ] = key
Original file line number Diff line number Diff line change
1
+ """ Merge sort O(nlogn) """
2
+
3
+ def merge (left , right ):
4
+ i = j = 0
5
+ result = []
6
+
7
+ # merging left and right mantaining sorted order
8
+ while i < len (left ) and j < len (right ):
9
+ if left [i ] < right [j ]:
10
+ result .append (left [i ])
11
+ i += 1
12
+ else :
13
+ result .append (right [j ])
14
+ j += 1
15
+
16
+ # append the rest of the subarray into result
17
+ result += left [i :]
18
+ result += right [j :]
19
+ return result
20
+
21
+ def mergeSort (arr ):
22
+ if len (arr ) <= 1 :
23
+ return arr
24
+
25
+ mid = len (arr )// 2
26
+
27
+ # have a left and right hand side
28
+ # sort left and right sublist
29
+ left = mergeSort (arr [:mid ])
30
+ right = mergeSort (arr [mid :])
31
+
32
+ return merge (left , right )
Original file line number Diff line number Diff line change
1
+ """ Quick sort O(nlogn) """
2
+
3
+ # returns sorted array (not as space efficient as in array quick sort)
4
+ def quickSort (arr ):
5
+ # return if trivial cases
6
+ if len (arr ) <= 1 :
7
+ return arr
8
+
9
+ # pivot
10
+ pivot = arr [0 ]
11
+ i = 0
12
+ # for all elements in array
13
+ for j in range (len (arr ) - 1 ):
14
+ # swap to end if larger than pivot
15
+ if arr [j + 1 ] < pivot :
16
+ # swap
17
+ arr [j + 1 ], arr [i + 1 ] = arr [i + 1 ], arr [j + 1 ]
18
+ i += 1
19
+
20
+ # swap pivot
21
+ arr [0 ], arr [i ] = arr [i ], arr [0 ]
22
+ left = quickSort (arr [:i ])
23
+ right = quickSort (arr [i + 1 :])
24
+ # add pivot back and return the whole subarray
25
+ left .append (arr [i ])
26
+
27
+ return left + right
You can’t perform that action at this time.
0 commit comments