Bubble Sort Time Complexity
What is the time complexity of the bubbleSort method in the code above?
public class BubbleSort {
public static void bubbleSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if(arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
public static void main(String[] args) {
int[] values = {5, 3, 8, 4, 2};
bubbleSort(values);
for(int num : values) {
System.out.print(num + " ");
}
}
}
A
O(n)
B
O(n^2)
C
O(log n)
D
O(1)
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE