forked from Haresh1204/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
29 lines (26 loc) · 860 Bytes
/
BubbleSort.java
File metadata and controls
29 lines (26 loc) · 860 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.TrX;
//Program of BubbleSort #Day2Of100DaysOfCoding
import java.util.Arrays;
public class BubbleSort {
public static void main(String[] args) {
int [] arr1 = {67,54,89,11,56};
System.out.println("Before Sorting");
System.out.println(Arrays.toString(arr1));
bubbleSort(arr1);
System.out.println("After Sorting");
System.out.println(Arrays.toString(arr1));
}
public static void bubbleSort(int [] arr){
int temp = 0;
for(int i=0; i < arr.length; i++){
for(int j=1; j < (arr.length-i); j++){
if(arr[j-1] > arr[j]){
//swap elements
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}
}