/Users/johnr/Desktop/IA_14_-_Stage_P_Upload_all_2021-04-08/MyFirstGUIProject - Daniel/src/main/java/com/mycompany/myfirstguiproject/SortingAndSearching.java |
1
2
3
4
5
6 package com.mycompany.myfirstguiproject;
7
8 import java.util.ArrayList;
9
10
11
12 @author
13
14 public class SortingAndSearching {
15
16 public void sortByStudentNameAZ(ArrayList<Student> students){
17 int n = students.size();
18 boolean sorted = false;
19 while (!sorted) {
20 n--;
21
22 sorted = true;
23 for (int i=0; i < n; i++) {
24 if (students.get(i).getName().compareToIgnoreCase(students.get(i+1).getName()) > 0) {
25 Student temp = students.get(i);
26 students.set(i, students.get(i+1));
27 students.set(i+1, temp);
28 sorted = false;
29
30 }
31 }
32 }
33 }
34
35 public void sortByStudentEmailAZ(ArrayList<Student> students){
36 int n = students.size();
37 boolean sorted = false;
38 while (!sorted) {
39 n--;
40
41 sorted = true;
42 for (int i=0; i < n; i++) {
43 if (students.get(i).getEmail().compareToIgnoreCase(students.get(i+1).getEmail()) > 0) {
44 Student temp = students.get(i);
45 students.set(i, students.get(i+1));
46 students.set(i+1, temp);
47 sorted = false;
48
49 }
50 }
51 }
52 }
53
54 public void sortByStudentLocationAZ(ArrayList<Student> students){
55 int n = students.size();
56 boolean sorted = false;
57 while (!sorted) {
58 n--;
59
60 sorted = true;
61 for (int i=0; i < n; i++) {
62 if (students.get(i).getLocation().compareToIgnoreCase(students.get(i+1).getLocation()) > 0) {
63 Student temp = students.get(i);
64 students.set(i, students.get(i+1));
65 students.set(i+1, temp);
66 sorted = false;
67
68 }
69 }
70 }
71 }
72
73 public void sortByStudentTimeZoneAZ(ArrayList<Student> students){
74 int n = students.size();
75 boolean sorted = false;
76 while (!sorted) {
77 n--;
78
79 sorted = true;
80 for (int i=0; i < n; i++) {
81 if (students.get(i).getTimeZone().compareToIgnoreCase(students.get(i+1).getTimeZone()) < 0) {
82 Student temp = students.get(i);
83 students.set(i, students.get(i+1));
84 students.set(i+1, temp);
85 sorted = false;
86
87 }
88 }
89 }
90 }
91 public void sortBySongNameAZ(ArrayList<Song> songs){
92 int n = songs.size();
93 boolean sorted = false;
94 while (!sorted) {
95 n--;
96
97 sorted = true;
98 for (int i=0; i < n; i++) {
99 if (songs.get(i).getSongName().compareToIgnoreCase(songs.get(i+1).getSongName()) > 0) {
100 Song temp = songs.get(i);
101 songs.set(i, songs.get(i+1));
102 songs.set(i+1, temp);
103 sorted = false;
104
105 }
106 }
107 }
108 }
109 public void sortByMovieNameAZ(ArrayList<Song> songs){
110 int n = songs.size();
111 boolean sorted = false;
112 while (!sorted) {
113 n--;
114
115 sorted = true;
116 for (int i=0; i < n; i++) {
117 if (songs.get(i).getMovieName().compareToIgnoreCase(songs.get(i+1).getMovieName()) > 0) {
118 Song temp = songs.get(i);
119 songs.set(i, songs.get(i+1));
120 songs.set(i+1, temp);
121 sorted = false;
122
123 }
124 }
125 }
126 }
127 public void sortByBPMAscending(ArrayList<Song> songs){
128 int n = songs.size();
129 boolean sorted = false;
130 while (!sorted) {
131 n--;
132
133 sorted = true;
134 for (int i=0; i < n; i++) {
135 if((songs.get(i).getSongBPM() - songs.get(i+1).getSongBPM()) > 0) {
136 Song temp = songs.get(i);
137 songs.set(i, songs.get(i+1));
138 songs.set(i+1, temp);
139 sorted = false;
140
141 }
142 }
143 }
144 }
145 public void sortByBPMDescending(ArrayList<Song> songs){
146 int n = songs.size();
147 boolean sorted = false;
148 while (!sorted) {
149 n--;
150
151 sorted = true;
152 for (int i=0; i < n; i++) {
153 if((songs.get(i).getSongBPM() - songs.get(i+1).getSongBPM()) < 0) {
154 Song temp = songs.get(i);
155 songs.set(i, songs.get(i+1));
156 songs.set(i+1, temp);
157 sorted = false;
158
159 }
160 }
161 }
162 }
163
164 public int binarySearchStudentName(ArrayList<Student> students, String key){
165 int low = 0;
166 int high = students.size() -1;
167 while(low <= high){
168 int mid = (low + high) / 2;
169 if(students.get(mid).getName().compareToIgnoreCase(key) == 0)
170 return mid;
171 else if(students.get(mid).getName().compareToIgnoreCase(key) < 0)
172 low = mid + 1;
173 else
174 high = mid -1;
175 }
176 return -1;
177 }
178 public int binarySearchSongName(ArrayList<Song> songs, String key){
179 int low = 0;
180 int high = songs.size() -1;
181 while(low <= high){
182 int mid = (low + high) / 2;
183 if(songs.get(mid).getSongName().compareToIgnoreCase(key) == 0)
184 return mid;
185 else if(songs.get(mid).getSongName().compareToIgnoreCase(key) < 0)
186 low = mid + 1;
187 else
188 high = mid -1;
189 }
190 return -1;
191 }
192
193 }
194