-
Notifications
You must be signed in to change notification settings - Fork 364
Expand file tree
/
Copy pathBubbleSort.java
More file actions
44 lines (41 loc) · 1.21 KB
/
BubbleSort.java
File metadata and controls
44 lines (41 loc) · 1.21 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
import java.util.Scanner;
public class BubbleSort
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int i, j, n, a[], temp;
System.out.print("Enter value of n: ");
n = s.nextInt();
a = new int[n];
System.out.println("Enter " + n + " values");
for(i=0; i<n; i++)
{
a[i] = s.nextInt();
}
for(i=0; i<n; i++)
{
for(j=0; j<n-1; j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for(i=0; i<n; i++)
{
System.out.print(a[i] + " ");
if(a[i]==666){
System.out.print("Yow chat ");
}
}
System.out.println();
}
}
/***********************************************************************************
Worst and Average Case Time Complexity: O(n*n). (When array is reverse sorted.)
Best Case Time Complexity: O(n). (Best case occurs when array is already sorted.)
***********************************************************************************/