## Monday, 25 September 2017

### Java Program for Bubble Sort in Ascending and Descending order

Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements bubble to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort.

Time Complexity:--

Best case performance    O(n)
Worst case performance  -  O(n^2)

Bubble Sort program for sorting in ascending order,

```package com.pr;
public class BubbleSort {

public static void main(String[] args) {

BubbleSort sort = new BubbleSort();
sort.bubbleSort(new int[]{1,10,4,3,8,2,15});
}

public void bubbleSort(int[] arr) {
int length = arr.length;

System.out.println("Array before bubble sort is: ");
for (int i = 0; i< arr.length; i++) {
System.out.print(arr[i]+" ");
}

for (int i = 0; i<arr.length; i++) {
for (int j = 1; j< (length-i); j++) {
if (arr[j-1] > arr[j]) {
int temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
System.out.println("");
if (i== 0) {
System.out.println("Sorting step by step:");
}
for (int k = 0; k< arr.length; k++) {
System.out.print(arr[k]+" ");
}
}

System.out.println("");
System.out.println("Array after bubble sort is: ");
for (int i = 0; i< arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
}```
` `

Output:-
Array before bubble sort is:
1 10 4 3 8 2 15
Sorting step by step:
1 4 3 8 2 10 15
1 3 4 2 8 10 15
1 3 2 4 8 10 15
1 2 3 4 8 10 15
1 2 3 4 8 10 15
1 2 3 4 8 10 15
1 2 3 4 8 10 15
Array after bubble sort is:
1 2 3 4 8 10 15

Bubble Sort program for sorting in descending order,

For descending order, change the above code at line no.15
`arr[j-1] > arr[j] to arr[j-1] < arr[j],`

```package com.pr;
public class BubbleSort {

public static void main(String[] args) {

BubbleSort sort = new BubbleSort();
sort.bubbleSort(new int[]{1,10,4,3,8,2,15});
}

public void bubbleSort(int[] arr) {
int length = arr.length;

System.out.println("Array before bubble sort is: ");
for (int i = 0; i< arr.length; i++) {
System.out.print(arr[i]+" ");
}

for (int i = 0; i<arr.length; i++) {
for (int j = 1; j< (length-i); j++) {
if (arr[j-1] < arr[j]) {
int temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
System.out.println("");
if (i== 0) {
System.out.println("Sorting step by step:");
}
for (int k = 0; k< arr.length; k++) {
System.out.print(arr[k]+" ");
}
}

System.out.println("");
System.out.println("Array after bubble sort is: ");
for (int i = 0; i< arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
}```
` `
Output: -
Array before bubble sort is:
1 10 4 3 8 2 15
Sorting step by step:
10 4 3 8 2 15 1
10 4 8 3 15 2 1
10 8 4 15 3 2 1
10 8 15 4 3 2 1
10 15 8 4 3 2 1
15 10 8 4 3 2 1
15 10 8 4 3 2 1
Array after bubble sort is:
15 10 8 4 3 2 1

#### 1 comment:

1. Hi,
Thanks for sharing the info about JAVA Plz keep sharing on...
Thank you...