Sort The Array Using Bubble Sort

 package sorting;


import java.util.Arrays;

import java.util.stream.Collectors;


public class BubbleSort {


public static void main(String[] args) {


int arr[] = { 1, 3, 4, 6, 5, 9, 2, 8, 7 };

int n = arr.length - 1;

for (int i = 0; i < n; i++) {

int flag = 0;

for (int j = 0; j < n - i; j++) {

if (arr[j] > arr[j + 1]) {

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

flag = 1;

}

}

if (flag == 0) {

break;

}  

}


System.out.println(Arrays.stream(arr).boxed().distinct().collect(Collectors.toList()));

}

}


Comments

Popular posts from this blog

3 Lines of Code And You Can Download Your Favourite Youtube Video.

Leetcode Problem(Easy) Roman to Integer

Leetcode : (Find Minimum in Rotated Sorted Array)