/Users/17763/Desktop/IA_Final_Submission_00889-0018/Product/IA_Netbeans_Project/src/ia/hokeun/SortAndSearchPlayers.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.hokeun;
 7 
 8 /**
 9  *
10  * @author 17763
11  */
12 public class SortAndSearchPlayers {
13     public int sequentialSearchForPlayerName(Player arr[], String player){
14         for(int i = 0; i < arr.length; i++){
15             if(arr[i].getName().equals(player)){
16                 return i;
17             }
18         }
19         return -1;
20     }
21     
22     public void sortByPlayerNames(Player[] players) {
23      int n = players.length;
24      boolean sorted = false;
25      while (!sorted) {
26           n--; //It is the n which will result in one less comparison happening each outer pass;
27                //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
28           sorted = true;  
29           for (int i=0; i < n; i++) {
30                if (players[i].getName().compareTo(players[i+1].getName()) > 0){
31                     Player temp = players[i];  
32                     players[i] = players[i+1];  
33                     players[i+1] = temp;
34                     sorted = false; //as in the second bubble sort, if swapping happens we'll want to continue, and so
35                                     //with sorted re-set to false again, the while loop continues
36                }    
37             }
38         }
39     }
40     
41     public void sortBySport(Player[] players) {
42      int n = players.length;
43      boolean sorted = false;
44      while (!sorted) {
45           n--; //It is the n which will result in one less comparison happening each outer pass;
46                //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
47           sorted = true;  
48           for (int i=0; i < n; i++) {
49                if (players[i].getSport().compareTo(players[i+1].getSport()) > 0){
50                     Player temp = players[i];  
51                     players[i] = players[i+1];  
52                     players[i+1] = temp;
53                     sorted = false; //as in the second bubble sort, if swapping happens we'll want to continue, and so
54                                     //with sorted re-set to false again, the while loop continues
55                }    
56             }
57         }
58     }
59     
60     public void sortBySeason(Player[] players) {
61      int n = players.length;
62      boolean sorted = false;
63      while (!sorted) {
64           n--; //It is the n which will result in one less comparison happening each outer pass;
65                //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
66           sorted = true;  
67           for (int i=0; i < n; i++) {
68                if (players[i].getSeason().compareTo(players[i+1].getSeason()) > 0){
69                     Player temp = players[i];  
70                     players[i] = players[i+1];  
71                     players[i+1] = temp;
72                     sorted = false; //as in the second bubble sort, if swapping happens we'll want to continue, and so
73                                     //with sorted re-set to false again, the while loop continues
74                }    
75             }
76         }
77     }
78     
79     public void sortByPlayingLevel(Player[] players) {
80      int n = players.length;
81      boolean sorted = false;
82      while (!sorted) {
83           n--; //It is the n which will result in one less comparison happening each outer pass;
84                //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
85           sorted = true;  
86           for (int i=0; i < n; i++) {
87                if (players[i].getPlayingLevel().compareTo(players[i+1].getPlayingLevel()) > 0){
88                     Player temp = players[i];  
89                     players[i] = players[i+1];  
90                     players[i+1] = temp;
91                     sorted = false; //as in the second bubble sort, if swapping happens we'll want to continue, and so
92                                     //with sorted re-set to false again, the while loop continues
93                }    
94             }
95         }
96     }
97 }
98