Skip to content

Commit f98863e

Browse files
committed
added sorting repo with bubble- and quicksort implementations
1 parent 7db9ba3 commit f98863e

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

Sorting Algorithms/SortingRepo.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
class SortingRepo {
2+
3+
constructor () {
4+
const arr: Array<number> = [0, 8, 1, 6, 9];
5+
const res = this.bubbleSort(arr);
6+
console.log(res);
7+
console.log('Successfully instantiated a SortingRepo.');
8+
}
9+
10+
/**
11+
* Implementation of the bubble sort algorithm.
12+
* This one uses a slow + fast runner paradigm which compares every element with every
13+
* other element in the array.
14+
*
15+
* @param arr: The array you want to sort.
16+
* @returns The sorted array, ascending.
17+
*/
18+
public bubbleSort (arr: Array<number>): Array<number> {
19+
for (let slowRunner = 0; slowRunner < arr.length; slowRunner++) {
20+
for (let fastRunner = 0; fastRunner < arr.length; fastRunner++) {
21+
const itemToCompare = arr[slowRunner];
22+
const oneOfTheOtherItems = arr[fastRunner];
23+
24+
if (itemToCompare < oneOfTheOtherItems) {
25+
this.swap(arr, slowRunner, fastRunner);
26+
}
27+
}
28+
}
29+
30+
return arr;
31+
}
32+
33+
/**
34+
* Improved implementation of the bubble sort algorithm. This version checks
35+
* if a swap has been made during an interation. If not the algorithm is finished
36+
* since the array is sorted at that point.
37+
*
38+
* @param arr: The array you want to sort.
39+
* @returns The sorted array, ascending.
40+
*/
41+
public bubbleSortImproved (arr: Array<number>): Array<number> {
42+
let unsortedArrayLength = arr.length - 1;
43+
44+
do {
45+
var swapped = false;
46+
47+
for (let i = 0; i < arr.length; i++) {
48+
if (arr[i] > arr[i + 1]) {
49+
this.swap(arr, i, i + 1);
50+
swapped = true;
51+
}
52+
}
53+
54+
unsortedArrayLength -= 1;
55+
} while (swapped);
56+
57+
return arr;
58+
}
59+
60+
/**
61+
* Implementation of the quick sort algorithm.
62+
* Recursive algorithm that uses the partition function to place a pivot element after
63+
* all elements that are less than the pivot and in front of all elements that are larger
64+
* than the pivot element.
65+
* The last element is being picked as the pivot element.
66+
*
67+
* @param arr: The array you want to sort
68+
* @param start: The first index of the to-sort-part of the array.
69+
* @param end: The ending index of the to-sort-part of the array.
70+
* @returns The sorted array, ascending.
71+
*/
72+
public quickSort(arr: Array<number>, start: number, end: number): Array<number> {
73+
if (start > end) return;
74+
75+
let pivotIndex: number = this.partition(arr, start, end);
76+
this.quickSort(arr, start, pivotIndex - 1);
77+
this.quickSort(arr, pivotIndex + 1, end);
78+
79+
return arr;
80+
}
81+
82+
private partition(arr: Array<number>, start: number, end: number): number {
83+
let pivotIndex = start;
84+
const pivotValue = arr[end];
85+
86+
for (let i = start; i < end; i++) {
87+
if (arr[i] < pivotValue) {
88+
this.swap(arr, i, pivotIndex);
89+
pivotIndex += 1;
90+
}
91+
}
92+
93+
this.swap(arr, pivotIndex, end);
94+
95+
return pivotIndex;
96+
}
97+
98+
// Helper functions
99+
private swap (arr: Array<number>, a: number, b: number): Array<number> {
100+
if (a === b) return arr;
101+
102+
const tmp: any = arr[a];
103+
arr[a] = arr[b];
104+
arr[b] = tmp;
105+
106+
return arr;
107+
}
108+
109+
}
110+
111+
export default new SortingRepo();

0 commit comments

Comments
 (0)