diff --git a/bubble.go b/bubble.go new file mode 100644 index 0000000..13ef82b --- /dev/null +++ b/bubble.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" +) + +func main(){ + arr := []int{64, 25, 12, 22, 11, 14, 1, 3, 2} + n := len(arr)-1 + + fmt.Println("Unsorted:",arr) + + for i := 0; i < n; i++ { + for j := 0; j < n-i; j++{ + if arr[j] > arr[j+1]{ + arr[j], arr[j+1] = arr[j+1], arr[j] + } + } + } + fmt.Println("Sorted:",arr) +} + +/* +Starting from the first index, compare the first and the second elements. +If the first element is greater than the second element, they are swapped. +Now, compare the second and the third elements. Swap them if they are not in order. +The above process goes on until the last element. +*/ \ No newline at end of file diff --git a/heap.go b/heap.go new file mode 100644 index 0000000..ed414bf --- /dev/null +++ b/heap.go @@ -0,0 +1,39 @@ +package main + +import( + "fmt" +) + +func main(){ + arr := []int{64, 25, 12, 22, 11, 14, 1, 3, 2} + n := len(arr) + fmt.Println("Unsorted:",arr) + sort(arr, n) + fmt.Println("Sorted:",arr) +} + +func heapify(arr []int, n, i int){ + largest := i + l := 2 * i + 1 + r := 2 * i + 2 + if l < n && arr[l] > arr[largest]{ + largest = l + } + if r < n && arr[r] > arr[largest]{ + largest = r + } + if largest != i { + arr[i], arr[largest] = arr[largest], arr[i] + heapify(arr, n, largest) + } +} + +func sort(arr []int, n int){ + for i := n / 2 - 1; i >= 0; i-- { + heapify(arr, n, i) + } + for i := n - 1; i > 0; i-- { + arr[0], arr[i] = arr[i], arr[0] + heapify(arr, i, 0) + } +} \ No newline at end of file diff --git a/insertion.go b/insertion.go new file mode 100644 index 0000000..c9076e4 --- /dev/null +++ b/insertion.go @@ -0,0 +1,33 @@ +package main + +import ( + "fmt" +) + +func main(){ + arr := []int{64, 25, 12, 22, 11, 14, 1, 3, 2} + n := len(arr) + + fmt.Println("Unsorted:",arr) + + for i := 1; i < n; i++{ + key := arr[i] + j := i - 1; + + for j >= 0 && arr[j] > key{ + arr[j + 1] = arr[j] + j = j - 1 + } + arr[j + 1] = key + } + fmt.Println("Sorted:",arr) +} + +/* +To sort an array of size n in ascending order: +1: Iterate from arr[1] to arr[n] over the array. +2: Compare the current element (key) to its predecessor. +3: If the key element is smaller than its predecessor, +compare it to the elements before. +Move the greater elements one position up to make space for the swapped element. +*/ \ No newline at end of file diff --git a/selection.go b/selection.go new file mode 100644 index 0000000..6650a59 --- /dev/null +++ b/selection.go @@ -0,0 +1,34 @@ +package main + +import ( + "fmt" +) + +func main(){ + arr := []int{64, 25, 12, 22, 11, 14, 1, 3, 2} + n := len(arr) + + fmt.Println("Unsorted:",arr) + + for i := 0; i < n; i++ { + var min = i + for j := i; j < n; j++ { + if arr[j] < arr[min] { + min = j + } + } + arr[i], arr[min] = arr[min], arr[i] + } + fmt.Println("Sorted:",arr) +} + +/* +selectionSort(array, size) + repeat (size - 1) times + set the first unsorted element as the minimum + for each of the unsorted elements + if element < currentMinimum + set element as new minimum + swap minimum with first unsorted position +end selectionSort +*/ \ No newline at end of file