Bubble Sort in java

Bubble Sort in java
SOURCE : BubbleSort.java

package Huwaei;

public class BubbleSort {

public static void main(String[] args) {

int arr[] = { 45, 7, 8, -9, 72, 7 };

BubbleSort bb = new BubbleSort();
bb.bubble(arr);

}

private void bubble(int[] arr) {

int length = arr.length;

for (int i = 0; i < length; i++) {
for (int j = i + 1; j < length; j++) {
if (arr[i] > arr[j]) {

int temp;

temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;

}

}
}

print(arr);
}

private void print(int[] arr) {

for (int i = 0; i < arr.length; i++) {

System.out.println(arr[i]);
}

}

}


INPUT

int arr[] = { 45, 7, 8, -9, 72, 7 }




OUTPUT

{-9 7 7 8 45 72 }






Sandeep Kumar D

Hi, I have written and developed this post so that most of people will be benefited. I'm committed to provide easy and in-depth tutorials on various technologies.I hope it will help you a lot.

- Sandeep Kumar D

Follow Me @Google+




SHARE

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment