-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
42 lines (38 loc) · 1.09 KB
/
BubbleSort.java
File metadata and controls
42 lines (38 loc) · 1.09 KB
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
/*
Input format
3 //number of elements in the Array
1 2 3 //elements in the array (space-separated)
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class BubbleSort {
public static void bubbleSort(int[] arr){
int count=0;
int tmp=0;
for(int i=0;i<arr.length;i++){
for(int j=i+1;j<arr.length;j++){
if(arr[i] > arr[j]){
arr[i] = arr[i]^arr[j];
arr[j] = arr[i]^arr[j];
arr[i] = arr[i]^arr[j];
count++;
}
}
}
System.out.println("Array is sorted in " + count + " swaps.");
System.out.println("First Element: " + arr[0]);
System.out.println("Last Element: " + arr[arr.length-1]);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int a[] = new int[n];
for(int a_i=0; a_i < n; a_i++){
a[a_i] = in.nextInt();
}
bubbleSort(a);
}
}