Last active
June 18, 2018 10:15
-
-
Save jamesgeorge007/33e058e5862605e7e0998d4b1db8249f to your computer and use it in GitHub Desktop.
Go lang implementation of the basic Array operations like inserting, deleting and sorting using the Bubble sorting algorithm.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main(){ | |
var A[25] int | |
var n, pos, el, temp, mid, first, last int | |
fmt.Println("Number of elements <=25:") | |
fmt.Scan(&n) | |
fmt.Println("Enter the elements:") | |
for i:=0;i<n;i++ { | |
fmt.Scan(&A[i]) | |
} | |
fmt.Println("Printing out the elements:") | |
for i:=0;i<n;i++ { | |
fmt.Print(A[i],"\t") | |
} | |
fmt.Println("\nEnter the position of the element to be deleted:") | |
fmt.Scan(&pos) | |
if pos<=n{ | |
for i:=pos-1;i<n-1;i++{ | |
A[i] = A[i+1] | |
} | |
fmt.Println("After deletion the array is: ") | |
for i:=0;i<n-1;i++{ | |
fmt.Print(A[i], ", ") | |
} | |
}else{ | |
fmt.Println("Deletion not possible!") | |
} | |
fmt.Println("\nEnter the element to be inserted:") | |
fmt.Scan(&el) | |
fmt.Println("Position:") | |
fmt.Scan(&pos) | |
if pos>n{ | |
fmt.Println("Insertion not possible!") | |
}else{ | |
for i:=n-1;i>=pos;i--{ | |
A[i] = A[i-1] | |
} | |
A[pos-1] = el | |
fmt.Println("After inserting ",el," the array is: ",) | |
for i:=0;i<n;i++ { | |
fmt.Print(A[i],"\t") | |
} | |
} | |
fmt.Println("\n") | |
for i:=1;i<=10;i++{ | |
fmt.Print("*") | |
} | |
fmt.Print("\tBubble Sort\t") | |
for i:=1;i<=10;i++{ | |
fmt.Print("*") | |
} | |
for i:=0;i<n;i++{ | |
for j:=0;j<n-i;j++{ | |
if A[j]>A[j+1]{ | |
temp = A[j] | |
A[j] = A[j+1] | |
A[j+1] = temp | |
} | |
} | |
} | |
fmt.Println("\n\nSorted array is :") | |
for i:=1;i<n+1;i++{ | |
fmt.Print(A[i], ", ") | |
} | |
fmt.Println() | |
for i:=1;i<=10;i++{ | |
fmt.Print("#") | |
} | |
fmt.Print("\tSelection Sort\t") | |
for i:=1;i<=10;i++{ | |
fmt.Print("#") | |
} | |
fmt.Println() | |
for i:=0;i<n;i++{ | |
for j:=i+1;j<n;j++{ | |
if A[i]>A[j]{ | |
temp = A[j] | |
A[i] = A[j] | |
A[j] = temp | |
} | |
} | |
} | |
fmt.Println("The sorted array is:") | |
for i:=1;i<=n;i++{ | |
fmt.Print(" ", A[i]) | |
} | |
fmt.Println("\n") | |
for i:=1;i<=10;i++{ | |
fmt.Print("\\") | |
} | |
fmt.Print("\tBinary Search\t") | |
for i:=1;i<=10;i++{ | |
fmt.Print("\\") | |
} | |
fmt.Print("\nEnter the item to be searched: ") | |
fmt.Scan(&el) | |
first= 0 | |
last= n-1 | |
mid=(first+last)/2 | |
for first<=last{ | |
if A[mid] == el{ | |
fmt.Println("Element found at the position ", mid) | |
break | |
}else if(el > A[mid]){ | |
first=mid+1 | |
}else{ | |
last=mid-1 | |
} | |
mid=(first+last)/2 | |
} | |
if(first>last){ | |
fmt.Print("\nElement not within the array!") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment