ADVERTISEMENT
ADVERTISEMENT

Golang program to sort an integer array in descending order using bubble sort

Here, we are going to learn how to sort an integer array in descending order using bubble sort in Golang (Go Language)?
Submitted by Nidhi, on March 07, 2021

Problem Solution:

In this program, we will read an array element from the user then sort the array in descending order using bubble sort and then print the sorted array on the console screen.

Program/Source Code:

The source code to sort an integer array in descending order using bubble sort is given below. The given program is compiled and executed successfully.

// Golang program to sort an integer array in 
// descending order using bubble sort

package main
import "fmt"

func main() {
    var arr[5] int
    var temp int=0
    
    fmt.Printf("Enter array elements: \n")
    for i:=0;i<=4;i++{
        fmt.Printf("Elements: arr[%d]: ",i)
        fmt.Scanf("%d",&arr[i])
    }
    
    for i:=0; i<=4; i++{
        for j:=4;j>=i+1;j--{
            if(arr[j]>arr[j-1]){
                temp=arr[j]
                arr[j]=arr[j-1]
                arr[j-1]=temp
            }
        }
    }
    
    fmt.Printf("Array after sorting: \n")
    for i:=0;i<=4;i++{
        fmt.Printf("%d ",arr[i])
    }
}

Output:

Enter array elements:
Elements: arr[0]: 12
Elements: arr[1]: 3
Elements: arr[2]: 5
Elements: arr[3]: 1
Elements: arr[4]: 23
Array after sorting:
23 12 5 3 1

Explanation:

In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the fmt package that includes the files of package fmt then we can use a function related to the fmt package.

In the main() function, we created an array arr and also created a variable temp.

  fmt.Printf("Enter array elements: \n")
    for i:=0;i<=4;i++{
        fmt.Printf("Elements: arr[%d]: ",i)
        fmt.Scanf("%d",&arr[i])
    }

In the above code, we read array elements from the user.

for i:=0; i<=4; i++{
        for j:=4;j>=i+1;j--{
            if(arr[j]>arr[j-1]){
                temp=arr[j]
                arr[j]=arr[j-1]
                arr[j-1]=temp
            }
        }
    }

In the above code, we applied bubble sort to arrange array elements in descending order, Here In each phase of bubble sort smallest element get moved to the last After that we printed the sorted array on the console screen.

Golang Array Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.