-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathSolution.go
38 lines (32 loc) · 858 Bytes
/
Solution.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"fmt"
)
func main() {
var n int
// take the inputs
fmt.Scanf("%d", &n)
arr := make([]int, n)
for i:=0; i<n; i++ {
fmt.Scanf("%d",&arr[i])
}
totalSwaps := 0
for i:=0; i<n; i++ {
// Track number of elements swapped during a single array traversal
numberOfSwaps := 0
for j:=0; j<n-1; j++ {
if arr[j] > arr[j+1] {
arr[j], arr[j+1] = arr[j+1], arr[j]
numberOfSwaps += 1
totalSwaps += 1
}
}
// If no elements were swapped during a traversal, array is sorted
if (numberOfSwaps == 0) {
break;
}
}
fmt.Printf("Array is sorted in %d swaps.\n", totalSwaps)
fmt.Printf("First Element: %d\n",arr[0])
fmt.Printf("Last Element: %d",arr[n-1])
}