-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
31d74c8
commit c12f106
Showing
43 changed files
with
1,054 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// bubble sort! | ||
// | ||
// docs: https://en.wikipedia.org/wiki/Bubble_sort | ||
// | ||
// bubble sort steps through a list, comparing adjacent elements and swapping them if they | ||
// are in the wrong order. it passes through the list repeatedly until the list is sorted | ||
|
||
// bubble_sort is the top level function responsible for ... bubble sorting! | ||
func bubbleSort(inputList []string) (outputList []string) { | ||
// setup defaults | ||
outputList = inputList | ||
isSorted := false | ||
|
||
// continuously do sorting rounds as long as the list remains unsorted | ||
for isSorted == false { | ||
outputList, isSorted = doSortingRound(outputList) | ||
} | ||
|
||
return outputList | ||
} | ||
|
||
// doSortingRound does the "actual sorting" | ||
func doSortingRound(inputList []string) (outputList []string, isSorted bool) { | ||
isSorted = true | ||
|
||
for index, element := range inputList { | ||
if index == 0 { | ||
// we compare (index VS index - 1) so theres | ||
// nothing to compare when looking at the 0th index | ||
outputList = []string{element} | ||
} else if element < outputList[index-1] { | ||
// if this element is less than the previous element then swap their order | ||
visitedElements := outputList[:index-1] | ||
previousElement := outputList[index-1] | ||
// append a list of | ||
// - all the list elements we've visited already | ||
// - the current element | ||
// - the previous element | ||
// which has the effect of swapping the order of the current and previous | ||
// elements, while also keeping all of the visited elements in place | ||
outputList = append(visitedElements, element, previousElement) | ||
isSorted = false | ||
} else { | ||
// otherwise append | ||
outputList = append(outputList, element) | ||
} | ||
} | ||
|
||
return outputList, isSorted | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
func builtinSort(inputList []string) (outputList []string) { | ||
outputList = inputList | ||
sort.Strings(outputList) | ||
return outputList | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// insertion sort! | ||
// | ||
// docs: https://en.wikipedia.org/wiki/Insertion_sort | ||
// | ||
// Insertion sort steps through an input list, using it to grow an output list. | ||
// On every step, it sorts the current element into its proper location on the | ||
// output list.It continues until the input list is empty. | ||
|
||
func insertionSort(inputList []string) (outputList []string) { | ||
|
||
for idx, element := range inputList { | ||
outputList = append(outputList, element) | ||
outputList = sortElementAtIndex(outputList, element, idx) | ||
} | ||
|
||
return outputList | ||
} | ||
|
||
func sortElementAtIndex(inputList []string, element string, idx int) (outputList []string) { | ||
outputList = inputList | ||
targetIndex := idx | ||
|
||
for (targetIndex != 0) && (element < outputList[targetIndex-1]) { | ||
outputList = swapWithPrevious(outputList, targetIndex) | ||
targetIndex = targetIndex - 1 | ||
} | ||
|
||
return outputList | ||
} | ||
|
||
func swapWithPrevious(list []string, idx int) []string { | ||
list[idx], list[idx-1] = list[idx-1], list[idx] | ||
return list | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// merge sort! | ||
// | ||
// docs: https://leetcode.com/explore/learn/card/recursion-ii/470/divide-and-conquer/2868/ | ||
|
||
func MergeSort(inputList []string) (sortedList []string) { | ||
|
||
var doMergeInput [][]string | ||
for _, item := range inputList { | ||
doMergeInput = append(doMergeInput, []string{item}) | ||
} | ||
|
||
doMergeOutput, _ := mergeSortRound(doMergeInput, 0) | ||
sortedList = doMergeOutput[0] | ||
|
||
return sortedList | ||
} | ||
|
||
func mergeSortRound(inputList [][]string, inputRound int) (outputList [][]string, outputRound int) { | ||
|
||
// "round" is used for debugging | ||
outputRound = inputRound + 1 | ||
|
||
// merge each pair, ignoring potential odd items | ||
for i := 0; i < len(inputList)/2*2; i += 2 { | ||
outputList = append( | ||
outputList, | ||
merge(inputList[i], inputList[i+1]), | ||
) | ||
} | ||
|
||
// handle odd items | ||
if len(inputList)%2 == 1 { | ||
outputList = append(outputList, inputList[len(inputList)-1]) | ||
} | ||
|
||
// recurse! | ||
if len(outputList) != 1 { | ||
return mergeSortRound(outputList, outputRound) | ||
} | ||
|
||
return outputList, outputRound | ||
} | ||
|
||
func merge(left []string, right []string) (result []string) { | ||
leftIndex, rightIndex := 0, 0 | ||
|
||
for (leftIndex < len(left)) || (rightIndex < len(right)) { | ||
|
||
if leftIndex == len(left) { | ||
// boundary check on first list, append from second list | ||
result = append(result, right[rightIndex]) | ||
rightIndex++ | ||
} else if rightIndex == len(right) { | ||
// boundary check on second list, append from first list | ||
result = append(result, left[leftIndex]) | ||
leftIndex++ | ||
} else if left[leftIndex] < right[rightIndex] { | ||
// comparison check, append from first list | ||
result = append(result, left[leftIndex]) | ||
leftIndex++ | ||
} else { | ||
// implicit comparison check, append from second list | ||
result = append(result, right[rightIndex]) | ||
rightIndex++ | ||
} | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// selection sort! | ||
// | ||
// docs: https://en.wikipedia.org/wiki/Selection_sort | ||
// | ||
// Selection sort looks through every element an input list, and finds the | ||
// smallest element. That element is then appended to the end of an output list. | ||
// When it reaches the end of the input list, all of the output elements will | ||
// be sorted. | ||
|
||
func selectionSort(inputList []string) (sortedList []string) { | ||
unsortedList := inputList | ||
inputListLength := len(inputList) | ||
|
||
for i := 0; i < inputListLength; i++ { | ||
smallestIndex := findSmallestIndex(unsortedList) | ||
smallestElement := unsortedList[smallestIndex] | ||
sortedList = append(sortedList, smallestElement) | ||
unsortedList = removeIndex(unsortedList, smallestIndex) | ||
} | ||
|
||
return sortedList | ||
} | ||
|
||
func findSmallestIndex(inputList []string) (smallestIndex int) { | ||
for index, element := range inputList { | ||
if element < inputList[smallestIndex] { | ||
smallestIndex = index | ||
} | ||
} | ||
|
||
return smallestIndex | ||
} | ||
|
||
func removeIndex(inputList []string, index int) (outputList []string) { | ||
outputList = append(inputList[:index], inputList[index+1:]...) | ||
return outputList | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
function doSorting(inputList) { | ||
return bubbleSort(inputList); | ||
} | ||
|
||
function bubbleSort(inputList) { | ||
outputList = inputList; | ||
isSorted = false; | ||
|
||
while (isSorted === false) { | ||
outputList, (isSorted = doSortingRound(outputList)); | ||
} | ||
|
||
return outputList; | ||
} | ||
|
||
function doSortingRound(inputList) { | ||
outputList = []; | ||
isSorted = true; | ||
|
||
inputList.forEach((element, index) => { | ||
if (index === 0) { | ||
outputList.push(element); | ||
} else if (element < outputList[index - 1]) { | ||
previousElement = outputList[index - 1]; | ||
outputList.pop(); | ||
outputList.push(element); | ||
outputList.push(previousElement); | ||
isSorted = false; | ||
} else { | ||
outputList.push(element); | ||
} | ||
}); | ||
|
||
return outputList, isSorted; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function doSorting(inputList) { | ||
outputList = inputList; | ||
outputList.sort(); | ||
return outputList; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// insertion sort! | ||
// | ||
// docs: https://en.wikipedia.org/wiki/Insertion_sort | ||
// | ||
// Insertion sort steps through an input list, using it to grow an output list. | ||
// On every step, it sorts the current element into its proper location on the | ||
// output list.It continues until the input list is empty. | ||
|
||
function doSorting(inputList) { | ||
return insertionSort(inputList); | ||
} | ||
|
||
function insertionSort(inputList) { | ||
outputList = []; | ||
|
||
inputList.forEach((element, idx) => { | ||
outputList.push(element); | ||
outputList = sortElementAtIndex(outputList, element, idx); | ||
}); | ||
|
||
return outputList; | ||
} | ||
|
||
function sortElementAtIndex(inputList, element, idx) { | ||
target_index = idx; | ||
outputList = inputList; | ||
|
||
while (target_index != 0 && element < outputList[target_index - 1]) { | ||
swapWithPrevious(outputList, target_index); | ||
target_index -= 1; | ||
} | ||
|
||
return outputList; | ||
} | ||
|
||
function swapWithPrevious(list, idx) { | ||
tmp = list[idx - 1]; | ||
list[idx - 1] = list[idx]; | ||
list[idx] = tmp; | ||
return list; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// selection sort! | ||
// | ||
// docs: https://en.wikipedia.org/wiki/Selection_sort | ||
// | ||
// Selection sort looks through every element an input list, and finds the | ||
// smallest element. That element is then appended to the end of an output list. | ||
// When it reaches the end of the input list, all of the output elements will | ||
// be sorted. | ||
|
||
function doSorting(inputList) { | ||
return selectionSort(inputList); | ||
} | ||
|
||
function selectionSort(inputList) { | ||
sortedElements = []; | ||
unsortedElements = inputList; | ||
inputListLength = inputList.length; | ||
|
||
for (let index = 0; index < inputListLength; index++) { | ||
// console.log("--- for (let index = 0; index < inputList.length; index++) { ---"); | ||
// console.log("unsortedElements => " + unsortedElements); | ||
// console.log("sortedElements => " + sortedElements); | ||
|
||
smallestIndex = findSmallestIndex(unsortedElements); | ||
smallestElement = unsortedElements[smallestIndex]; | ||
sortedElements.push(smallestElement); | ||
unsortedElements.splice(smallestIndex, 1); | ||
|
||
// console.log("smallestIndex => " + smallestIndex); | ||
// console.log("smallestElement => " + smallestElement); | ||
// console.log("unsortedElements => " + unsortedElements); | ||
// console.log("sortedElements => " + sortedElements); | ||
} | ||
|
||
return sortedElements; | ||
} | ||
|
||
function findSmallestIndex(inputList) { | ||
smallestIndex = 0; | ||
|
||
inputList.forEach((element, index) => { | ||
if (element < inputList[smallestIndex]) { | ||
smallestIndex = index; | ||
} | ||
}); | ||
|
||
return smallestIndex; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# bubble sort! | ||
# | ||
# docs: https://en.wikipedia.org/wiki/Bubble_sort | ||
# | ||
# bubble sort steps through a list, comparing adjacent elements and swapping them if they | ||
# are in the wrong order. it passes through the list repeatedly until the list is sorted | ||
|
||
|
||
def do_sorting(input_list): | ||
return bubble_sort(input_list) | ||
|
||
|
||
# bubble_sort is the top level function responsible for ... bubble sorting! | ||
def bubble_sort(input_list: list[str]) -> list[str]: | ||
# set defaults | ||
output_list = input_list | ||
is_sorted = False | ||
|
||
# continuously do sorting rounds as long as the list remains unsorted | ||
while is_sorted is False: | ||
output_list, is_sorted = do_sorting_round(output_list) | ||
|
||
# mission accomplished! ✨ | ||
return output_list | ||
|
||
|
||
# do_sorting_round does the "actual sorting" | ||
def do_sorting_round(input_list: list[str]) -> (list[str], bool): | ||
# set defaults | ||
output_list = [] | ||
is_sorted = True | ||
|
||
for index, element in enumerate(input_list): | ||
# we compare (index VS index - 1) so there's | ||
# nothing to compare when looking at the 0th index | ||
if index == 0: | ||
output_list.append(element) | ||
continue | ||
|
||
# grab (index - 1) | ||
previous_element = output_list[index - 1] | ||
|
||
# if this element is less than the previous element then swap their order | ||
if element < previous_element: | ||
output_list.pop() | ||
output_list.append(element) | ||
output_list.append(previous_element) | ||
is_sorted = False | ||
# otherwise append | ||
else: | ||
output_list.append(element) | ||
|
||
return output_list, is_sorted | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
def do_sorting(input_list): | ||
return sorted(input_list) | ||
|
Oops, something went wrong.