Created
May 6, 2020 04:13
-
-
Save cedricconol/c598b7fd67f3ee0a17894c5ae8b1425b to your computer and use it in GitHub Desktop.
optimized swap in bubble sort
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
func pass(list []int, ascending bool) int { | |
var N int = len(list) | |
var swapCount int | |
for i := 0; i < N-1; i++ { | |
var firstElement int = list[i] | |
var secondElement int = list[i+1] | |
if ascending == true { | |
if firstElement > secondElement { | |
swap(list, i) | |
swapCount++ | |
} | |
} else { | |
if firstElement < secondElement { | |
swap(list, i) | |
swapCount++ | |
} | |
} | |
} | |
return swapCount | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment