Skip to content

Commit 737fd91

Browse files
authored
Add files via upload
1 parent 1de5ab7 commit 737fd91

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Sorts/derpySort.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// This sorting function called derpy randomly removes and re-adds elements in order
2+
3+
function derpySort(arr) {
4+
5+
let sorted = [];
6+
let length = arr.length;
7+
8+
while (length > 0) {
9+
10+
// Get a random index from the array
11+
let randomIndex = Math.floor(Math.random() * length);
12+
13+
// Remove the element
14+
let element = arr.splice(randomIndex, 1)[0];
15+
16+
// Add it back into the array
17+
sorted.push(element); // Add the element to the sorted array
18+
19+
length--;
20+
21+
}
22+
23+
return sorted;
24+
25+
}
26+
27+
28+
let unsortedArray = [42, 17, 8, 99, 23, 54];
29+
30+
console.log("Original Array: " + unsortedArray);
31+
32+
let sortedArray = derpySort(unsortedArray);
33+
34+
console.log("Derpy Sorted Array: " + sortedArray);

0 commit comments

Comments
 (0)