-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-sortFrames.java
66 lines (51 loc) · 1.36 KB
/
1-sortFrames.java
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
62
63
64
65
66
// Write a program to sort frames using appropriate sorting techniques.
import java.util.Scanner;
import java.util.Random;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
public class sortFrames{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("\nEnter the number of frames: ");
int n=sc.nextInt();
List<int[]> frame=new ArrayList<>();
for(int i=0;i<n;i++){
Random rand=new Random();
int seqNum=rand.nextInt(1000)+1;
System.out.printf("Enter data for frame %d: ",i+1);
int data=sc.nextInt();
frame.add(new int[]{seqNum,data});
}
System.out.println("\n\nBefore Sorting: ");
for(int[] i:frame){
System.out.printf("seqNum->%d, data->%d\n", i[0],i[1]);
}
frame=sort(frame);
System.out.println("\n\nAfter Sorting: ");
for(int[] i:frame){
System.out.printf("seqNum->%d, data->%d\n", i[0],i[1]);
}
System.out.println();
}
public static List<int[]> sort(List<int[]> frame){
Collections.sort(frame,(a,b)->Integer.compare(a[0],b[0]));
return frame;
}
}
/*
Output:
Enter the number of frames:
3
Enter data for frame 1: 12
Enter data for frame 2: 56
Enter data for frame 3: 89
Before Sorting:
seqNum->426, data->12
seqNum->636, data->56
seqNum->683, data->89
After Sorting:
seqNum->426, data->12
seqNum->636, data->56
seqNum->683, data->89
*/