-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubbleSort.java
47 lines (34 loc) · 996 Bytes
/
BubbleSort.java
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of array: " );
int size = sc.nextInt();
int arr[] = new int[size];
for(int i=0 ; i<size; i++){
arr[i]= sc.nextInt();
}
System.out.println("The given array is: ");
for(int i=0;i<size;i++){
System.out.println(arr[i] + " ");
}
System.out.println();
sortedarray(arr);
for(int i=0;i<size;i++){
System.out.println(arr[i] + " ");
}
sc.close();
}
public static void sortedarray(int[] arr){
int size= arr.length;
for(int i=size-1;i<=0;i--){
for(int j =0; j<=i-1; j++){
if(arr[j]>arr[j+1]){
int temp = arr[j+1];
arr[j+1] = arr[j];
arr[j] = temp;
}
}
}
}
}