SHARE
TWEET

Untitled

a guest Sep 19th, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5. )
  6.  
  7. func selection(data []int){
  8.     n := len(data)
  9.     //find min ele
  10.     for i:=0;i<n-1;i++{
  11.         minInd := i
  12.         for j:=i+1;j<n;j++{
  13.             if data[j]<data[minInd]{
  14.                 minInd = j         
  15.             }
  16.         }
  17.         data[i], data[minInd] = data[minInd], data[i]
  18.     }
  19.     fmt.Println(data)
  20. }
  21.  
  22.  
  23. func bubble(data []int){
  24.     n := len(data)
  25.     swapped := false
  26.     //compare two adjacent
  27.    
  28.     for i:=0;i<n-1;i++{
  29.         for j:=0;j<n-1;j++{
  30.             if(data[j]>data[j+1]){
  31.                 data[j],data[j+1]= data[j+1],data[j]
  32.                 swapped = true
  33.             }
  34.         }
  35.         if !swapped{
  36.             break;
  37.         }
  38.     }
  39.     fmt.Println(data)
  40. }
  41.  
  42. func insertion(data []int){
  43.     n := len(data)
  44.     for i:=1;i<n;i++{
  45.         for j:=i;j>0;j--{
  46.             if data[i]<data[j]{
  47.                 data[i],data[j] = data[j], data[i]
  48.             }
  49.         }
  50.    
  51.     }
  52.     fmt.Println(data)      
  53. }
  54.  
  55. func main() {
  56.     arr := make([]int,5)
  57.     arr[0]=5
  58.     arr[1]=9
  59.     arr[2]=3
  60.     arr[3]=7
  61.     arr[4]=2
  62.     selection(arr)
  63.     bubble(arr)
  64.     insertion(arr)
  65. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top