-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirst.java
More file actions
48 lines (41 loc) · 1.01 KB
/
Copy pathFirst.java
File metadata and controls
48 lines (41 loc) · 1.01 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
public class First {
public static void printarray(int[] arr)
{
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+" ");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//bubble sort:ascending
//n-1 comparisions. tc=o(n^2) so not good tc
//with iteration the last element gets sorted , then 2nd last and so on
//for descending we take smallest element to last
int[] arr= {7,8,3,1,2};
//outer loop
for(int i=0;i<arr.length-1;i++)
{
//n-1 ,n-2,n-3 etc. times
//every iteration removes one last element
for(int j=0;j<arr.length-1-i;j++)
{ //swap: ascending
// if(arr[j]>arr[j+1])
// { //i is just a counter and index is defined by j
//
// int temp=arr[j];
// arr[j]=arr[j+1];
// arr[j+1]=temp;
// }
//swap: descending
if(arr[j]<arr[j+1])
{ //i is just a counter and index is defined by j
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printarray(arr);
}
}