/Users/20390/Desktop/jacob IA/src/newgui/sortAndSearch.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 newgui;
  7 
  8 /**
  9  *
 10  * @author 20390
 11  */
 12 public class sortAndSearch {
 13 
 14     public void sortByAgeAscending(Student[] students) {
 15         int n = students.length;
 16         boolean sorted = false;
 17         while (!sorted) {
 18             n--;
 19             sorted = true;
 20             for (int i = 0; i < n; i++) {
 21                 if (students[i].getAge() > students[i + 1].getAge()) {
 22                     Student temp = students[i];
 23                     students[i] = students[i + 1];
 24                     students[i + 1] = temp;
 25                     sorted = false;
 26                 }
 27             }
 28         }
 29 
 30     }
 31 
 32     public void sortByAgeDescending(Student[] students) {
 33         int n = students.length;
 34         boolean sorted = false;
 35         while (!sorted) {
 36             n--;
 37             sorted = true;
 38             for (int i = 0; i < n; i++) {
 39                 if (students[i].getAge() < students[i + 1].getAge()) {
 40                     Student temp = students[i];
 41                     students[i] = students[i + 1];
 42                     students[i + 1] = temp;
 43                     sorted = false;
 44                 }
 45             }
 46         }
 47     }
 48     
 49     public void sortByGradeAscending(Student[] students) {
 50         int n = students.length;
 51         boolean sorted = false;
 52         while (!sorted) {
 53             n--;
 54             sorted = true;
 55             for (int i = 0; i < n; i++) {
 56                 if (students[i].getGrade() > students[i + 1].getGrade()) {
 57                     Student temp = students[i];
 58                     students[i] = students[i + 1];
 59                     students[i + 1] = temp;
 60                     sorted = false;
 61                 }
 62             }
 63         }
 64 
 65     }
 66     
 67     public void sortByGradeDescending(Student[] students) {
 68         int n = students.length;
 69         boolean sorted = false;
 70         while (!sorted) {
 71             n--;
 72             sorted = true;
 73             for (int i = 0; i < n; i++) {
 74                 if (students[i].getGrade() < students[i + 1].getGrade()) {
 75                     Student temp = students[i];
 76                     students[i] = students[i + 1];
 77                     students[i + 1] = temp;
 78                     sorted = false;
 79                 }
 80             }
 81         }
 82     }
 83 
 84     public void sortByNameAtoZ(Student[] students) {
 85         int n = students.length;
 86         boolean sorted = false;
 87         while (!sorted) {
 88             n--;
 89             sorted = true;
 90             for (int i = 0; i < n; i++) {
 91                 if (students[i].getName().compareToIgnoreCase(students[i + 1].getName()) > 0) {
 92                     Student temp = students[i];
 93                     students[i] = students[i + 1];
 94                     students[i + 1] = temp;
 95                     sorted = false;
 96                 }
 97             }
 98         }
 99     }
100 
101     public void sortByNameZtoA(Student[] students) {
102         int n = students.length;
103         
104         boolean sorted = false;
105         while (!sorted) {
106             n--;
107             sorted = true;
108             for (int i = 0; i < n; i++) {
109                 if (students[i].getName().compareToIgnoreCase(students[i + 1].getName()) < 0) {
110                     Student temp = students[i];
111                     students[i] = students[i + 1];
112                     students[i + 1] = temp;
113                     sorted = false;
114                 }
115             }
116         }
117     }
118     
119     public void smartestBubbleSort(int[] intArray) {
120      int n = intArray.length;
121      boolean sorted = false;
122      while (!sorted) {
123           n--; //It is the n which will result in one less comparison happening each outer pass;
124                //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
125           sorted = true;  
126           for (int i=0; i < n; i++) {
127                if (intArray[i] > intArray[i+1]) {
128                     int temp = intArray[i];  
129                     intArray[i] = intArray[i+1];  
130                     intArray[i+1] = temp;
131                     sorted = false; //as in the second bubble sort, if swapping happens we'll want to continue, and so
132                                     //with sorted re-set to false again, the while loop continues
133                }
134           }
135      }
136   }
137     
138     
139     
140     
141     
142     
143     
144     
145 
146     public void sortByEmailAtoZ(Student[] students) {
147         int n = students.length;
148         boolean sorted = false;
149         while (!sorted) {
150             n--;
151             sorted = true;
152             for (int i = 0; i < n; i++) {
153                 if (students[i].getEmail().compareToIgnoreCase(students[i + 1].getEmail()) > 0) {
154                     Student temp = students[i];
155                     students[i] = students[i + 1];
156                     students[i + 1] = temp;
157                     sorted = false;
158                 }
159             }
160         }
161     }
162 
163     public void sortByEmailZtoA(Student[] students) {
164         int n = students.length;
165         boolean sorted = false;
166         while (!sorted) {
167             n--;
168             sorted = true;
169             for (int i = 0; i < n; i++) {
170                 if (students[i].getEmail().compareToIgnoreCase(students[i + 1].getEmail()) < 0) {
171                     Student temp = students[i];
172                     students[i] = students[i + 1];
173                     students[i + 1] = temp;
174                     sorted = false;
175                 }
176             }
177         }
178     }
179 
180     public int sequentialSearch(Student arr[], String country) {
181         for (int i = 0; i < arr.length; i++) {
182             if (arr[i].getName().equals(country)) {
183                 return i;
184             }
185         }
186         return -1;
187     }
188 }
189