32
loading...
This website collects cookies to deliver better user experience
bubbleSort(array)
for i <- 1 to indexOfLastUnsortedElement-1
if leftElement > rightElement
swap leftElement and rightElement
end bubbleSort
package algorithms.sorting;
import java.util.Arrays;
public class BubbleSort {
public static void main(String[] args) {
int[] nums = {-2, 45, 0, 11,-9};
BubbleSort bs = new BubbleSort();
int swaps = bs.sort(nums);
System.out.println(Arrays.toString(nums));
System.out.println("Total swaps done : " + swaps);
}
public int sort(int[] arr) {
int swaps = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 1; j < arr.length - i; j++) {
if (arr[j] < arr[j - 1]) {
int temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
swaps++;
}
}
}
return swaps;
}
}
def bubble_sort(array):
swaps = 0
for i in range(len(array)):
for j in range(1, len(array) - i):
if array[j] < array[j - 1]:
temp = array[j]
array[j] = array[j-1]
array[j-1] = temp
swaps += 1
return swaps
if __name__ == '__main__':
nums = [-2, 45, 0, 11, -9]
total_swaps = bubble_sort(nums)
print(nums)
print(f"Total swaps done : {total_swaps}")
[-9, -2, 0, 11, 45]
Total swaps done : 6
swapped
or isSwapped
which is set to true if there occurs swapping of elements. Otherwise, it is set false. swapped
or isSwapped
will be false. This means elements are already sorted and there is no need to perform further iterations.bubbleSort(array)
swapped <- false
for i <- 1 to indexOfLastUnsortedElement-1
if leftElement > rightElement
swap leftElement and rightElement
swapped <- true
end bubbleSort
package algorithms.sorting;
import java.util.Arrays;
public class BubbleSort {
public static void main(String[] args) {
int[] nums = {-2, 45, 0, 11,-9};
BubbleSort bs = new BubbleSort();
int swaps = bs.sort(nums);
System.out.println(Arrays.toString(nums));
System.out.println("Total swaps done : " + swaps);
}
public int sort(int[] arr) {
boolean isSwapped;
int swaps = 0;
for (int i = 0; i < arr.length; i++) {
isSwapped = false;
for (int j = 1; j < arr.length - i; j++) {
if (arr[j] < arr[j - 1]) {
int temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
isSwapped = true;
swaps++;
}
}
if (!isSwapped) break;
}
return swaps;
}
}
def bubble_sort(array):
swaps = 0
for i in range(len(array)):
is_swapped = False
for j in range(1, len(array) - i):
if array[j] < array[j - 1]:
temp = array[j]
array[j] = array[j-1]
array[j-1] = temp
is_swapped = True
swaps += 1
if not is_swapped:
break
return swaps
if __name__ == '__main__':
nums = [-2, 45, 0, 11, -9]
total_swaps = bubble_sort(nums)
print(nums)
print(f"Total swaps done : {total_swaps}")
[-9, -2, 0, 11, 45]
Total swaps done : 6
Time Complexity | |
---|---|
Best | O(n) |
Worst | O(n^2) |
Average | O(n^2) |
Space Complexity | O(1) |
Stability | Yes |
Cycle | Number of Comparisons |
---|---|
1st | (n-1) |
2nd | (n-2) |
3rd | (n-3) |
....... | ...... |
last | 1 |
(n-1) + (n-2) + (n-3) +.....+ 1 = n(n-1)/2
O(n^2)
If we want to sort in ascending order and the array is in descending order then the worst case occurs.O(n)
If the array is already sorted, then there is no need for sorting.O(n^2)
It occurs when the elements of the array are in jumbled order (neither ascending nor descending).O(1)
because an extra variable is used for swapping.O(2)
.