Created
October 14, 2022 17:12
-
-
Save AnAverageBeing/f2387fa7c70c86dfecfc738ae5d7c710 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Arrays.Sorting; | |
public class BubbleSort { | |
/* | |
* This method takes a int array as input | |
* Then sorts the array using BUBBLE SORT algorithm | |
* Then returns the sorted array | |
*/ | |
public static int[] sort(int[] arr) { | |
int n = arr.length; | |
for (int i = 0; i < n; i++) { | |
for (int j = 0; j < n - 1; j++) { | |
if (arr[j] > arr[j + 1]) { | |
int temp = arr[j]; | |
arr[j] = arr[j + 1]; | |
arr[j + 1] = temp; | |
} | |
} | |
} | |
return arr; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bubble Sort
Input: arr[] = {5, 1, 4, 2, 8}
First Pass:
Second Pass: