Skip to content

Commit c9ad078

Browse files
sorting method
1 parent b9f21b6 commit c9ad078

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import java.util.Scanner;
2+
public class BubbleSort {
3+
int n, i, j, temp;
4+
int arr[] = new int[50];
5+
int Array[] = new int[arr.length] ;
6+
Scanner scan = new Scanner(System.in);
7+
8+
BubbleSort(){
9+
System.out.print("Enter the size of Array : ");
10+
n = scan.nextInt();
11+
System.out.print("Enter Array Elements : "+" ");
12+
for(i=0; i<n; i++)
13+
{
14+
arr[i] = scan.nextInt();
15+
}
16+
17+
for(i=0;i<arr.length;i++){
18+
Array[i]=arr[i];
19+
}
20+
}
21+
22+
void bubbleSort(){
23+
for(i=0;i<n-1;i++){
24+
for(j=0;j<n-i-1;j++){
25+
if(Array[j]>Array[j+1]){
26+
temp=Array[j];
27+
Array[j]=Array[j+1];
28+
Array[j+1]=temp;
29+
}
30+
}
31+
}
32+
}
33+
34+
void printArray(){
35+
System.out.print("New Array is :\n");
36+
for(i=0; i<n; i++)
37+
{
38+
System.out.print(Array[i]+ " "+" ");
39+
}
40+
}
41+
}
42+
43+
44+
45+
class Main{
46+
public static void main(String args[]){
47+
BubbleSort obj = new BubbleSort();
48+
obj.bubbleSort();
49+
obj.printArray();
50+
}
51+
}

0 commit comments

Comments
 (0)