/Users/16484/Desktop/IA_Final_Submission_0003070061/Product/IA_Netbeans_Project/src/ia/prototype/shubha/n/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 ia.prototype.shubha.n;
 7 
 8 import java.util.ArrayList;
 9 
10 /**
11  *
12  * @author 16484
13  */
14 public class SortAndSearch {
15     public void sortByName(ArrayList<Reminder> reminder){
16        int n = reminder.size();
17        boolean sorted = false;
18        while(!sorted){
19            n--;
20            sorted = true;
21            for(int i = 0; i < n; i++){
22                if(reminder.get(i).getName().compareTo(reminder.get(i+1).getName()) > 0){
23                    Reminder temp = reminder.get(i);
24                    reminder.set(i, reminder.get(i+1));
25                    reminder.set(i+1, temp);
26                    sorted = false;
27                }
28            }
29        }
30     }
31     
32     public void sortByDate(ArrayList<Reminder> reminder){
33         
34     }
35     
36     public void sortByCategory(ArrayList<Reminder> reminder){
37         int n = reminder.size();
38         boolean sorted = false;
39         while (!sorted) {
40             n--; //It is the n which will result in one less comparison happening each outer pass;
41                //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
42             sorted = true;  
43             for (int i=0; i < n; i++) {
44                 if (reminder.get(i).getCategory().compareTo(reminder.get(i+1).getCategory()) > 0) {
45                     Reminder temp = reminder.get(i);
46                     reminder.set(i, reminder.get(i+1));
47                     reminder.set(i+1, temp);
48                     sorted = false; //as in the second bubble sort, if swapping happens we'll want to continue, and so
49                                     //with sorted re-set to false again, the while loop continues
50                }
51           }
52      }
53     }
54     
55     public int sequentialSearch(ArrayList<Reminder> arr, String key){
56         for(int i = 0; i < arr.size(); i++){
57             if(arr.get(i).getName().equals(key)){
58                 return i;
59             }
60         }
61         return -1;
62     }
63 }
64