Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c23263e

Browse files
committed
Added Bubble Sort
1 parent 69d9ae8 commit c23263e

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

Sorting/Bubble Sort/BubbleSort.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
import java.util.Arrays;
3+
4+
public class BubbleSort {
5+
6+
public static int[] bubbleSort(int input[]) {
7+
boolean swapping = true;
8+
while (swapping) {
9+
swapping = false;
10+
for (int i = 0; i < input.length - 1; i++) {
11+
if (input[i] > input[i+1]){
12+
System.out.println(String.format("Swapping pair %d, %d", input[i], input[i+1]));
13+
Swap.swap(input, i,i+1);
14+
System.out.println(Arrays.toString(input));
15+
swapping = true;
16+
}
17+
}
18+
}
19+
return input;
20+
}
21+
22+
public static void main(String[] args) {
23+
int[] example = {13, 22, 50, 15, 20, 33, 33};
24+
BubbleSort b = new BubbleSort();
25+
b.bubbleSort(example);
26+
}
27+
}

Sorting/Bubble Sort/Swap.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public class Swap {
2+
public static void swap(int[] arr, int indexOne, int indexTwo) {
3+
int temp = arr[indexTwo];
4+
arr[indexTwo] = arr[indexOne];
5+
arr[indexOne] = temp;
6+
}
7+
8+
public static void main(String[] args) {
9+
10+
}
11+
}

Sorting/Bubble Sort/readme.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Bubble Sort
2+
Bubble sort is an introductory sorting algorithm that iterates through a list and compares pairings of adjacent elements.
3+
4+
## Introduction
5+
According to the sorting criteria, the algorithm swaps elements to shift elements towards the beginning or end of the list.
6+
7+
By default, a list is sorted if for any element e and position 1 through N:
8+
9+
e1 <= e2 <= e3 … eN, where N is the number of elements in the list.
10+
11+
We implement the algorithm with two loops.
12+
13+
The first loop iterates as long as the list is unsorted and we assume it’s unsorted to start.
14+
15+
Within this loop, another iteration moves through the list. For each pairing, the algorithm asks:
16+
17+
In comparison, is the first element larger than the second element?
18+
19+
If it is, we swap the position of the elements. The larger element is now at a greater index than the smaller element.
20+
21+
When a swap is made, we know the list is still unsorted. The outer loop will run again when the inner loop concludes.
22+
23+
The process repeats until the largest element makes its way to the last index of the list. The outer loop runs until no
24+
swaps are made within the inner loop.
25+
26+
## Working
27+
As mentioned, the bubble sort algorithm swaps elements if the element on the left is larger than the one on the right.
28+
29+
How does this algorithm swap these elements in practice?
30+
31+
Let’s say we have the two values stored at the following indices index_1 and index_2. How would we swap these two elements within the list?
32+
It is tempting to write code like:
33+
34+
list[index_1] = list[index_2]
35+
36+
list[index_2] = list[index_1]
37+
38+
However, if we do this, we lose the original value at index_1. The element gets replaced by the value at index_2. Both indices end up with the value at index_2.
39+
40+
Programming languages have different ways of avoiding this issue. In some languages, we create a temporary variable which holds one element during the swap.
41+
42+
Other languages provide multiple assignment which removes the need for a temporary variable.
43+
44+
## Algorithm Analysis
45+
Given a moderately unsorted data-set, bubble sort requires multiple passes through the input before producing a sorted list.
46+
47+
Each pass through the list will place the next largest value in its proper place.
48+
49+
We are performing n-1 comparisons for our inner loop. Then, we must go through the list n times in order to ensure that each item in our list has been placed in its proper order.
50+
51+
The n signifies the number of elements in the list. In a worst case scenario, the inner loop does n-1 comparisons for each n element in the list.
52+
53+
Therefore we calculate the algorithm’s efficiency as:
54+
55+
**O(n(n-1)) = O(n(n)) = O(n^2)**
56+
57+
When calculating the run-time efficiency of an algorithm, we drop the constant (-1), which simplifies our inner loop comparisons to n.
58+
59+
This is how we arrive at the algorithm’s runtime: **O(n^2)**.

Sorting/readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Sorting Algorithms
2+
3+
| | Best Case | Worst Case | Average Case | Space Complexity |
4+
|-------------|-----------|------------|--------------|------------------|
5+
| Bubble Sort | Ω(n) | O(n^2) | O(n^2) | O(1) |
6+
| Merge Sort | Ω(n log n)| O(n log n) | O(n log n) | O(n) |
7+
| Quicksort | Ω(n log n)| O(n log n) | O(n^2) | O(log n) |

0 commit comments

Comments
 (0)