bubble sort logic code example
Example 1: bubble sort in java
public static void bubbleSort(int arr[])
{
for (int i = 0; i < arr.length; i++)
{
for (int j = 0; j < (arr.length - 1 - i); j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
Example 2: bubble sort code
func Sort(arr []int) []int {
for i := 0; i < len(arr)-1; i++ {
for j := 0; j < len(arr)-i-1; j++ {
if arr[j] > arr[j+1] {
temp := arr[j]
arr[j] = arr[j+1]
arr[j+1] = temp
}
}
}
return arr
}