-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
61 lines (48 loc) · 1.54 KB
/
BubbleSort.java
File metadata and controls
61 lines (48 loc) · 1.54 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.Scanner;
public class BubbleSort {
private static Scanner sc= new Scanner(System.in);
public static int[] getelements(int capacity){
int[] array = new int[capacity];
System.out.println("\nEnter "+capacity+" elements - \n");
for(int i=0;i<capacity;i++){
array[i]=sc.nextInt();
}
return array;
}
public static int[] sortelements(int[] array,int n){
int[] sortedarr = new int[n];
for(int i=0;i<n;i++)
sortedarr[i]=array[i];
boolean flag=true;
int temp;
while(flag){
flag=false;
for(int i=0;i<n-1;i++){
if(sortedarr[i]<sortedarr[i+1]){
temp=sortedarr[i];
sortedarr[i]=sortedarr[i+1];
sortedarr[i+1]=temp;
flag=true;
}
}
}
return sortedarr;
}
public static void printarray(int[] arr){
System.out.println("\n");
for(int i=0;i<arr.length;i++)
System.out.print(arr[i]+" ");
System.out.println("\n");
}
public static void main(String[] args) {
System.out.println("\nEnter the total number of elements in the array - ");
int n = sc.nextInt();
int[] arr = new int[n];
arr=getelements(n);
System.out.println("\nInput Array - \n");
printarray(arr);
int[] sortedarr=sortelements(arr,n);
System.out.println("\nSorted Array - \n");
printarray(sortedarr);
}
}