what is Bubble Sort algorithm
Bubble Sort Algorithm
Definition
one way of sorting algorithms by using brute force.
check every possible ways to sort the list.
Algorithm steps
- compare two numbers
- smaller number will be left and bigger number will be in right side.
- compare over and over again until the end of the list
- Now, biggest number will stay in most right side. This number is sorted
- Start over from first number except sorted place.
- Repeat 1~5 until all of them sorted.



Avg. time complexity
Worst time complexity
space complexity
stability
yes
Java code
public static int[] bubbleSort(int[] input) {
for (int i = 0; i < input.length - 1; ++i) {
for (int j = 0; j < input.length - i - 1; ++j) {
if (input[j] > input[j + 1]) {
int temp = input[j];
input[j] = input[j + 1];
input[j + 1] = temp;
}
}
}
return input;
}
How to calculate
numbers of loop
The most visited place of the list
(the first place)
The least visited place of the list
(the last place)
Avg. visited numbers of the list
(The most visited place of the list+The least visited place of the list)/2
Polynomial Time
numbers of loop*Avg. visited numbers of the list
Time Complexity
0 개의 댓글:
댓글 쓰기