Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BubbleSort {
- public static void bubbleSort(int[] arr) {
- boolean stillSwappingElements = true;
- int j = 0;
- while (stillSwappingElements) {
- stillSwappingElements = false;
- j++;
- for (int i = 0; i < (arr.length - j); ++i) {
- if (arr[i] > arr[i+1]) {
- int temp = arr[i];
- arr[i] = arr[i+1];
- arr[i+1] = temp;
- stillSwappingElements = true;
- }
- }
- }
- }
- public static void main(String[] args) {
- int[] arr = new int[] { 44, 55, 12, 42, 94, 18 };
- bubbleSort(arr);
- for (int element : arr) {
- System.out.print(element + " ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement