Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
Create implement_selection_sort.go (#5613)
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavsilimkhan authored Apr 5, 2024
1 parent d4a76e9 commit 0da4ad6
Showing 1 changed file with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import "fmt"

// Function to perform selection sort
func selectionSort(arr []int) {
n := len(arr)

// Traverse through all array elements
for i := 0; i < n-1; i++ {
// Find the minimum element in unsorted array
minIndex := i
for j := i + 1; j < n; j++ {
if arr[j] < arr[minIndex] {
minIndex = j
}
}

// Swap the found minimum element with the first element
arr[i], arr[minIndex] = arr[minIndex], arr[i]
}
}

func main() {
// Test the selection sort function
arr := []int{64, 25, 12, 22, 11}
fmt.Println("Original array:", arr)
selectionSort(arr)
fmt.Println("Sorted array:", arr)
}

0 comments on commit 0da4ad6

Please sign in to comment.