-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubblesort.java
More file actions
32 lines (30 loc) · 983 Bytes
/
Copy pathbubblesort.java
File metadata and controls
32 lines (30 loc) · 983 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
30
31
32
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] x = new int[n];
for (int i=0; i<n; i++){
x[i]=scan.nextInt();
}
boolean flag = true;
int swaps = 0;
while(flag){
flag=false;
for(int i=0; i<x.length-1; i++){
if(x[i]>x[i+1]){
int tmp = x[i];
x[i]=x[i+1];
x[i+1] = tmp;
flag = true;
swaps++;
}
}
}
System.out.println("Array is sorted in "+swaps+" swaps.");
System.out.println("First Element: "+x[0]);
System.out.println("Last Element: "+x[x.length-1]);
}
}