DEV Community

vinay
vinay

Posted on • Edited on

sorting golang

#go
package main

import (
    "fmt"
    "math"
)

func LinearSarch(a []int, key int) string {
    point := 0
    for point < len(a) {
        if key == a[point] {
            return fmt.Sprintf("found the index is %d", point)
        }
        point += 1
    }
    return "Not found Index"

}

func BinarySarch(a []int, key int, Left int, Right int) string {
    if Left <= Right {
        midPoint := int(math.Floor(float64(Left+Right) / 2))
        if key == a[midPoint] {
            return fmt.Sprintf("found the index is %d", midPoint)
        } else if key < a[midPoint] {
            return BinarySarch(a, key, Left, midPoint-1)
        } else {
            return BinarySarch(a, key, midPoint+1, Right)
        }
    }
    return "Not found"

}

func selection(a []int) {
    for i := 0; i < len(a)-1; i++ {
        min := i
        for j := i + 1; j < len(a); j++ {
            if a[j] < a[min] {
                min = j
            }
        }
        temp := a[i]
        a[i], a[min] = a[min], temp
    }

}

func insertion(a []int) {
    for i := 1; i < len(a); i++ {
        temp := a[i]
        j := i - 1
        for j >= 0 && a[j] > temp {
            a[j+1] = a[j]
            j--
        }
        a[j+1] = temp

    }

}

func bubblesort(a []int) {
    for i := 0; i < len(a)-1; i++ {
        for j := 0; j < len(a)-1-i; j++ {
            if a[j] > a[j+1] {
                temp := a[j]
                a[j] = a[j+1]
                a[j+1] = temp
            }
        }

    }

}

func sellsort(arr []int, n int) {
    for gap := n / 2; gap > 0; gap = gap / 2 {
        for j := gap; j < n; j++ {
            for i := j - gap; i >= 0; i -= gap {
                if arr[i+gap] > arr[i] {
                    break
                } else {
                    temp := arr[i]
                    arr[i] = arr[i+gap]
                    arr[i+gap] = temp
                }

            }

        }
    }

}
func partition(arr []int, lb int, ub int) int {
    pivot := arr[lb]
    start := lb
    end := ub
    for start < end {
        if arr[start] <= pivot {
            start += 1
        }
        for arr[end] > pivot {
            end -= 1
        }
        if start < end {
            temp := arr[start]
            arr[start] = arr[end]
            arr[end] = temp
         }
    }
    temp := arr[lb]
    arr[lb] = arr[end]
    arr[end] = temp
    return end
}

func quikSort(arr []int, lb int, ub int) {
    if lb < ub {
        p := partition(arr, lb, ub)
        quikSort(arr, lb, p-1)
        quikSort(arr, p+1, ub)
    }
}

func main() {
    a := []int{10, 20, 30, 10, 5, 3, 2}
    bubblesort(a)
    sellsort(a, len(a))
    insertion(a)
    selection(a)
    fmt.Println(a)
    b := BinarySarch(a, 30, 0, len(a)-1)
    q := LinearSarch(a, 30)
    fmt.Println(b, q)

}
Enter fullscreen mode Exit fullscreen mode

ACI image

ACI.dev: The Only MCP Server Your AI Agents Need

ACI.dev’s open-source tool-use platform and Unified MCP Server turns 600+ functions into two simple MCP tools on one server—search and execute. Comes with multi-tenant auth and natural-language permission scopes. 100% open-source under Apache 2.0.

Star our GitHub!

Latest comments (2)

Collapse
 
parmcoder profile image
Possawat Sanorkam

Do parallel merge sort!

Collapse
 
vinaygo profile image
vinay

kk

Postmark Image

20% off for developers who'd rather build features than debug email

Stop wrestling with email delivery and get back to the code you love. Postmark handles the complexities of email infrastructure so you can ship your product faster.

Start free

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay