/Users/adamklas/Desktop/IB/Net Beans/Dossier for Ms. Boughey/src/dossier/pkgfor/ms/boughey/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 
 7 package dossier.pkgfor.ms.boughey;
 8 
 9 import java.util.ArrayList;
10 
11 /**
12  *
13  * @author adamklas
14  */
15 public class SortAndSearch {
16     
17     public void studentYearBubbleSort(ArrayList <Student> studentList){
18         //I assign the list of the Student Array List to be an integer "n"
19         int n = studentList.size();
20         //boolean "sorted" is set to be false to start 
21         boolean sorted = false;
22         //while the condition that "sorted" is not true, the loop below is repeated
23         while(!sorted){
24             //each time it loops, the n decreases by 1 making this more efficient
25             n--;
26             //sorted is set to be true
27             sorted = true;
28             //here I loop through all the Students being focused on
29             for(int i = 0; i < n ; i++){
30                 //if the 'lower' student has higher higher data the code below is carried out
31                 if(studentList.get(i).getStudentYear().compareTo(studentList.get(i+1).getStudentYear()) > 0){
32                     //here I make a temporary Student, and set it to be the 'lower' Student
33                     Student temp = studentList.get(i); 
34                     //Here I set the 'lower' student to be the 'higher student'
35                     studentList.set(i, studentList.get(i+1)); 
36                     //Here the 'higher' student is set to be the 'lower student'
37                     studentList.set(i+1, temp);
38                     //sorted is set to be true, to keep the loop going
39                     sorted = false;
40                     
41                     
42                 }
43             }
44         }
45         
46         
47     }
48     //All the sorting classes are the same, each just focuses on a different attribute
49     //refer to lines 18-47 for description
50     public void studentClassBubbleSort(ArrayList <Student> studentList){
51         int n = studentList.size();
52         boolean sorted = false;
53         while(!sorted){
54             n--;
55             sorted = true;
56             for(int i = 0; i < n ; i++){
57                 if(studentList.get(i).getStudentBlock().compareTo(studentList.get(i +1).getStudentBlock()) > 0){
58                     Student temp = studentList.get(i); //making a temporary carList object
59                     studentList.set(i, studentList.get(i+1)); //set first to be the second
60                     studentList.set(i+1, temp);//set the second to be the temp
61                     sorted = false;
62                     
63                     
64                 }
65             }
66         }
67     }
68     
69         
70         public void dateInputBubbleSort(ArrayList <Student> studentList){
71         int n = studentList.size();
72         boolean sorted = false;
73         while(!sorted){
74             n--;
75             sorted = true;
76             for(int i = 0; i < n ; i++){
77                 if(studentList.get(i).getDateInput().compareTo(studentList.get(i +1).getDateInput()) > 0){
78                     Student temp = studentList.get(i); //making a temporary carList object
79                     studentList.set(i, studentList.get(i+1)); //set first to be the second
80                     studentList.set(i+1, temp);//set the second to be the temp
81                     sorted = false;
82                     
83                     
84                 }
85             }
86         }
87         
88         
89     }
90         
91         
92         
93         
94     }
95     
96 
97