/Users/johnr/Desktop/IA_14_-_Stage_P_Upload_all_2021-04-08/MyFirstGUIProject - Daniel/src/main/java/com/mycompany/myfirstguiproject/SortingAndSearching.java
  1 /*
  2  * To change this license header, choose License Headers in Project Properties.
  3  * To change this template file, choose Tools | Templates
  4  * and open the template in the editor.
  5  */
  6 package com.mycompany.myfirstguiproject;
  7 
  8 import java.util.ArrayList;
  9 
 10 /**
 11  *
 12  * @author 16939
 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--; //It is the n which will result in one less comparison happening each outer pass;
 21                //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
 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; //as in the second bubble sort, if swapping happens we'll want to continue, and so
 29                                     //with sorted re-set to false again, the while loop continues
 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--; //It is the n which will result in one less comparison happening each outer pass;
 40                //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
 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; //as in the second bubble sort, if swapping happens we'll want to continue, and so
 48                                     //with sorted re-set to false again, the while loop continues
 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--; //It is the n which will result in one less comparison happening each outer pass;
 59                //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
 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; //as in the second bubble sort, if swapping happens we'll want to continue, and so
 67                                     //with sorted re-set to false again, the while loop continues
 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--; //It is the n which will result in one less comparison happening each outer pass;
 78                //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
 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; //as in the second bubble sort, if swapping happens we'll want to continue, and so
 86                                     //with sorted re-set to false again, the while loop continues
 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--; //It is the n which will result in one less comparison happening each outer pass;
 96                //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
 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; //as in the second bubble sort, if swapping happens we'll want to continue, and so
104                                     //with sorted re-set to false again, the while loop continues
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--; //It is the n which will result in one less comparison happening each outer pass;
114                //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
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; //as in the second bubble sort, if swapping happens we'll want to continue, and so
122                                     //with sorted re-set to false again, the while loop continues
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--; //It is the n which will result in one less comparison happening each outer pass;
132                //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
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; //as in the second bubble sort, if swapping happens we'll want to continue, and so
140                                     //with sorted re-set to false again, the while loop continues
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--; //It is the n which will result in one less comparison happening each outer pass;
150                //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
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; //as in the second bubble sort, if swapping happens we'll want to continue, and so
158                                     //with sorted re-set to false again, the while loop continues
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){           // Keep on looking for the key until the low and the high cross 
168         int mid = (low + high) / 2; // each other - if that does happen, it means the key was not found.
169         if(students.get(mid).getName().compareToIgnoreCase(key) == 0)
170             return mid;           // This is what will happen if/when we find the key in the array.
171         else if(students.get(mid).getName().compareToIgnoreCase(key) < 0)
172             low = mid + 1;        // Since the arr[mid] value is less than the key, we can eliminate 
173         else                        // looking at the left side of the remaining elements
174             high = mid -1;        // i.e. the arr[mid] value is greater than what we are looking for, 
175     }                               // so we can eliminate looking at the right side of the remaining elements
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){           // Keep on looking for the key until the low and the high cross 
182         int mid = (low + high) / 2; // each other - if that does happen, it means the key was not found.
183         if(songs.get(mid).getSongName().compareToIgnoreCase(key) == 0)
184             return mid;           // This is what will happen if/when we find the key in the array.
185         else if(songs.get(mid).getSongName().compareToIgnoreCase(key) < 0)
186             low = mid + 1;        // Since the arr[mid] value is less than the key, we can eliminate 
187         else                        // looking at the left side of the remaining elements
188             high = mid -1;        // i.e. the arr[mid] value is greater than what we are looking for, 
189     }                               // so we can eliminate looking at the right side of the remaining elements
190     return -1;
191 }
192     
193 }
194