add files
This commit is contained in:
parent
a7b9c28e38
commit
c2b17fb968
28
bubble.go
Normal file
28
bubble.go
Normal file
@ -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.
|
||||
*/
|
39
heap.go
Normal file
39
heap.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
33
insertion.go
Normal file
33
insertion.go
Normal file
@ -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.
|
||||
*/
|
34
selection.go
Normal file
34
selection.go
Normal file
@ -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
|
||||
*/
|
Loading…
Reference in New Issue
Block a user