-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathModifiedBubbleSort.java
More file actions
41 lines (38 loc) · 865 Bytes
/
ModifiedBubbleSort.java
File metadata and controls
41 lines (38 loc) · 865 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
33
34
35
36
37
38
39
40
41
import java.util.Scanner;
public class ModifiedBubbleSort{
public static void main(String ar[]){
int[] a= new int[5];
Scanner s1= new Scanner(System.in);
System.out.print("Enter number of elements:");
int n= s1.nextInt();
System.out.println("Enter Array elements:");
for(int i=0;i<n;i++)
a[i]=s1.nextInt();
for(int p=0;p<n-1;p++)
{
int flag=0;
for(int s=0;s<n-p-1;s++)
{
if(a[s] > a[s+1])
{
flag=1;
int temp;
temp=a[s];
a[s]=a[s+1];
a[s+1]=temp;
}
}
if(flag == 0)
{
System.out.println("Number of Comparision required= "+p);
System.out.println("Array after Sorting as follows :");
for(int i=0;i<n;i++)
System.out.print(a[i]+" ");
return;
}
}
System.out.println("Array after Sorting as follows :");
for(int i=0;i<n;i++)
System.out.print(a[i]+" ");
}
}