Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Bubble Sort #6792

Merged
merged 1 commit into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions code/languages/Java/README_bubble-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Bubble sort is an algorithm used to sort an array or a list in a more optimized fashion.
At worst, bubble sort is seen as a 0(n^2) runtime sort algorithm.

Algorithm used:
1) Check and see if the positions next to each other in the array/list are
out of order.
2) If they are, swap them. If not, move on.
3) Do this through the entire list, if no swaps are made, end the algorithm.
4) If changes have been made, repeat.
98 changes: 98 additions & 0 deletions code/languages/Java/bubble-sort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Building Bubble Sort for both strings and integer arrays
class bubble_sort {
public static void bubble_sort_int(int[] arr1) {
int i, j;
boolean areSwapped;

// Loops through to sort the array
for (i = 0; i < arr1.length - 1; i++) {
areSwapped = false;
for (j = 0; j < arr1.length - i - 1; j++) {
if (arr1[j] > arr1[j+1]) {

// swaps the two numbers if they are out of order
int temp = arr1[j];
arr1[j] = arr1[j+1];
arr1[j+1] = temp;
areSwapped = true;
}
}

// ends the algorithm if it is in sorted order
if (areSwapped == false) {
break;
}
}
}

public static void bubble_sort_string(String[] arr1) {
int i, j;
boolean areSwapped;

// Loops through to sort the array
for (i = 0; i < arr1.length; i++) {
areSwapped = false;
for (j = 0; j < arr1.length - i - 1; j++) {

// case doesn't matter for sorting, so we don't want an error
String atJ = arr1[j].toLowerCase();
String atJ1 = arr1[j+1].toLowerCase();
if (atJ.compareTo(atJ1) > 0) {

// swaps the two strings if they are out of order
String temp = arr1[j];
arr1[j] = arr1[j+1];
arr1[j+1] = temp;
areSwapped = true;
}
}

// ends the algorithm if it is in sorted order
if (areSwapped == false) {
break;
}
}
}

public static void intTester() {
// Tests the integer array sort function
int[] check = {3, 1, 4, 2, 43, 7, 9, 13, 0, 2, -1};
int i;
System.out.println("Before: ");
for (i = 0; i < check.length; i++) {
System.out.print(check[i] + " ");
}

bubble_sort_int(check);

System.out.println("\nAfter: ");
for (i = 0; i < check.length; i++) {
System.out.print(check[i] + " ");
}
}

public static void stringTester() {
// Tests the string array sort function
String[] check = {"Hi", "Bye", "Byex", "Alex", "Ale", "My", "Friend", "q", "Z"};
System.out.println("Before: ");
int i;
for (i = 0; i < check.length; i++) {
System.out.print(check[i] + " ");
}

bubble_sort_string(check);

System.out.println("\nAfter: ");
for (i = 0; i < check.length; i++) {
System.out.print(check[i] + " ");
}
}

public static void main(String[] args) {
// Runs the functions
intTester();
System.out.println();
stringTester();
System.out.println();
}
}