Skip to content

Select algorithm enhancements & adding quick sort #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 80 additions & 29 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,75 @@ package main

import (
"fmt"
"strings"
"time"

"github.com/gookit/color"
"github.com/manifoldco/promptui"
)

const (
BubbleSort int = 1
SelectionSort int = 2
InsertionSort int = 3
GnomeSort int = 4
CocktailShakerSort int = 5
CombSort int = 6
OddEvenSort int = 7
QuickSort int = 8
)

type options struct {
Name string
Uuid int
}

func main() {

clearConsole()

arr := generateRandomArray(10)
fmt.Println("Initial array:", arr)

algorithms := []options{
{Name: "Bubble Sort", Uuid: 1},
{Name: "Selection Sort", Uuid: 2},
{Name: "Insertion Sort", Uuid: 3},
{Name: "Gnome Sort", Uuid: 4},
{Name: "Cocktail Shaker Sort", Uuid: 5},
{Name: "Comb Sort", Uuid: 6},
{Name: "Odd-Even Sort", Uuid: 7},
{Name: "Quick Sort", Uuid: 8},
}

templates := &promptui.SelectTemplates{
Label: "{{ . }}?",
Active: "\U00002728 {{ .Name | cyan }}",
Inactive: " {{ .Name | cyan }}",
Selected: "\U00002728 {{ .Name | red | cyan }}",
Details: `
--------- Algorithm ----------
{{ "Name:" | faint }} {{ .Name }}`,
}

searcher := func(input string, index int) bool {
algorithm := algorithms[index]
name := strings.Replace(strings.ToLower(algorithm.Name), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)

return strings.Contains(name, input)
}

algorithmPrompt := promptui.Select{
Label: "Select Sorting Algorithm",
Items: []string{
"Bubble Sort",
"Selection Sort",
"Insertion Sort",
"Gnome Sort",
"Cocktail Shaker Sort",
"Comb Sort",
"Odd-Even Sort",
},
Label: "Select a sorting algorithm!",
Items: algorithms,
Templates: templates,
Size: 4,
Searcher: searcher,
}

_, algorithm, err := algorithmPrompt.Run()
i, _, err := algorithmPrompt.Run()

if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
Expand All @@ -46,7 +91,7 @@ func main() {

switch action {
case "Description":
printAlgorithmDescription(algorithm)
printAlgorithmDescription(algorithms[i].Uuid)
case "Run":
speedPrompt := promptui.Select{
Label: "Select Visualization Speed",
Expand Down Expand Up @@ -75,78 +120,84 @@ func main() {

clearConsole()

runAlgorithm(algorithm, arr, delay)
runAlgorithm(algorithms[i].Uuid, arr, delay)
default:
fmt.Println("Invalid choice")
return
}
}

func printAlgorithmDescription(algorithm string) {
func printAlgorithmDescription(algorithm int) {
switch algorithm {
case "Bubble Sort":
case BubbleSort:
color.Bold.Println(color.Yellow.Sprint("Bubble Sort"))
fmt.Println()
fmt.Println(color.Cyan.Sprint("Bubble Sort is a simple comparison-based sorting algorithm. It works by repeatedly stepping through the list to be sorted,"))
fmt.Println(color.Cyan.Sprint("compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list"))
fmt.Println(color.Cyan.Sprint("is sorted. The algorithm gets its name from the way smaller elements 'bubble' to the top of the list."))
case "Selection Sort":
case SelectionSort:
color.Bold.Println(color.Yellow.Sprint("Selection Sort"))
fmt.Println()
fmt.Println(color.Cyan.Sprint("Selection Sort is an in-place comparison-based sorting algorithm. It works by dividing the input list into two parts:"))
fmt.Println(color.Cyan.Sprint("the sorted part and the unsorted part. The algorithm repeatedly selects the minimum element from the unsorted part"))
fmt.Println(color.Cyan.Sprint("and moves it to the end of the sorted part. This process continues until the entire list is sorted."))
case "Insertion Sort":
case InsertionSort:
color.Bold.Println(color.Yellow.Sprint("Insertion Sort"))
fmt.Println()
fmt.Println(color.Cyan.Sprint("Insertion Sort is an in-place comparison-based sorting algorithm. It works by dividing the input list into two parts:"))
fmt.Println(color.Cyan.Sprint("the sorted part and the unsorted part. The algorithm repeatedly picks an element from the unsorted part and inserts"))
fmt.Println(color.Cyan.Sprint("it into the correct position in the sorted part. This process continues until the entire list is sorted."))
case "Gnome Sort":
case GnomeSort:
color.Bold.Println(color.Yellow.Sprint("Gnome Sort"))
fmt.Println()
fmt.Println(color.Cyan.Sprint("Gnome Sort is an in-place comparison-based sorting algorithm. It works by repeatedly comparing adjacent elements."))
fmt.Println(color.Cyan.Sprint("If the two elements are in the wrong order, it swaps them and moves one step backward. If the two elements"))
fmt.Println(color.Cyan.Sprint("are in the correct order, it moves one step forward. This process continues until the entire list is sorted."))
case "Cocktail Shaker Sort":
case CocktailShakerSort:
color.Bold.Println(color.Yellow.Sprint("Cocktail Shaker Sort"))
fmt.Println()
fmt.Println(color.Cyan.Sprint("Cocktail Shaker Sort, also known as Bidirectional Bubble Sort, is a variation of Bubble Sort. It works by repeatedly"))
fmt.Println(color.Cyan.Sprint("sorting the list in both directions, first from the beginning to the end (like Bubble Sort), and then from the end"))
fmt.Println(color.Cyan.Sprint("to the beginning. The algorithm stops when the list becomes sorted."))
case "Comb Sort":
case CombSort:
color.Bold.Println(color.Yellow.Sprint("Comb Sort"))
fmt.Println()
fmt.Println(color.Cyan.Sprint("Comb Sort is an in-place comparison-based sorting algorithm. It works by dividing the input list into a series of"))
fmt.Println(color.Cyan.Sprint("gaps and repeatedly sorting the list with a specific shrink factor. The shrink factor reduces the gaps until it becomes 1."))
fmt.Println(color.Cyan.Sprint("At this point, the algorithm behaves similar to Bubble Sort. Comb Sort is an improvement over Bubble Sort for large lists."))
case "Odd-Even Sort":
case OddEvenSort:
color.Bold.Println(color.Yellow.Sprint("Odd-Even Sort"))
fmt.Println()
fmt.Println(color.Cyan.Sprint("Odd-Even Sort is an in-place comparison-based sorting algorithm. It works by repeatedly comparing and swapping"))
fmt.Println(color.Cyan.Sprint("adjacent elements at even and odd indices. The process continues until the list is sorted. Odd-Even Sort is known"))
fmt.Println(color.Cyan.Sprint("for its simplicity but is not very efficient for large lists."))
case QuickSort:
color.Bold.Println(color.Yellow.Sprint("Quick Sort"))
fmt.Println()
fmt.Println(color.Cyan.Sprint("The quick sort algorithm falls under the divide and conquer class of algorithms, where we break (divide) a problem into smaller chunks that are much simpler to solve (conquer). In this case, an unsorted array is broken into sub-arrays that are partially sorted, until all elements in the list are in the right position, by which time our unsorted list will have become sorted."))
default:
fmt.Println("Invalid selection")
}
}

func runAlgorithm(algorithm string, arr []int, delay time.Duration) {
func runAlgorithm(algorithm int, arr []int, delay time.Duration) {
switch algorithm {
case "Bubble Sort":
case BubbleSort:
bubbleSortVisualizer(arr, delay)
case "Selection Sort":
case SelectionSort:
selectionSortVisualizer(arr, delay)
case "Insertion Sort":
case InsertionSort:
insertionSortVisualizer(arr, delay)
case "Gnome Sort":
case GnomeSort:
gnomeSortVisualizer(arr, delay)
case "Cocktail Shaker Sort":
case CocktailShakerSort:
cocktailShakerSortVisualizer(arr, delay)
case "Comb Sort":
case CombSort:
combSortVisualizer(arr, delay)
case "Odd-Even Sort":
case OddEvenSort:
oddEvenSortVisualizer(arr, delay)
case QuickSort:
quickSort(arr, delay)
default:
fmt.Println("Invalid selection")
}
Expand Down
37 changes: 37 additions & 0 deletions quick_sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"time"
)

func partition(arr []int, low, high int, delay time.Duration) ([]int, int) {
pivot := arr[high]
i := low
for j := low; j < high; j++ {
visualizeIteration(White, arr, j, j+1, delay)
if arr[j] < pivot {
visualizeIteration(LightYellow, arr, j, j+1, delay)
arr[i], arr[j] = arr[j], arr[i]
i++
visualizeIteration(LightBlue, arr, j, j+1, delay)
}
}
arr[i], arr[high] = arr[high], arr[i]
return arr, i
}

func processQuickSort(arr []int, low, high int, delay time.Duration) []int {
low = 0
high = len(arr) - 1
if low < high {
var p int
arr, p = partition(arr, low, high, delay)
arr = processQuickSort(arr, low, p-1, delay)
arr = processQuickSort(arr, p+1, high, delay)
}
return arr
}

func quickSort(arr []int, delay time.Duration) []int {
return processQuickSort(arr, 0, len(arr)-1, delay)
}