/Users/johnr/Dropbox/johnrayworth.info/largeFilesOutsideJSR/__IB-Other/Other/IA-Solutions-2020/Nont/Product/Nont - IA/src/nont/ia/Sorts.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 nont.ia;
 7 
 8 /**
 9  *
10  * @author 15837
11  */
12 public class Sorts {
13     public void sortByArtistName(Song[] songsArray, int counter) {
14         int n = counter;
15         boolean sorted = false;
16         while (!sorted) {
17             n--; //It is the n which will result in one less comparison happening each outer pass;
18                //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
19             sorted = true;  
20             for (int i=0; i < n; i++) {
21                 if (songsArray[i].getArtistName().compareTo(songsArray[i+1].getArtistName()) > 0) {
22                     Song temp = songsArray[i];  
23                     songsArray[i] = songsArray[i+1];  
24                     songsArray[i+1] = temp;
25                     sorted = false; //as in the second bubble sort, if swapping happens we'll want to continue, and so
26                                     //with sorted re-set to false again, the while loop continues
27                     System.out.println("Inside sort by artist");
28                 }
29             }
30         }
31     }
32     
33     public void sortBySongName(Song[] songs, int counter) {
34         int n = counter;
35         boolean sorted = false;
36          while (!sorted) {
37             n--; //It is the n which will result in one less comparison happening each outer pass;
38                //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
39             sorted = true;  
40             for (int i=0; i < n; i++) {
41                 if (songs[i].getSongName().compareTo(songs[i+1].getSongName()) > 0) {
42                     Song temp = songs[i];  
43                     songs[i] = songs[i+1];  
44                     songs[i+1] = temp;
45                     sorted = false; //as in the second bubble sort, if swapping happens we'll want to continue, and so
46                                     //with sorted re-set to false again, the while loop continues
47                 }
48             }
49         }
50     }
51     
52     public void sortByAlbumName(Song[] songs, int counter) {
53         int n = counter;
54         boolean sorted = false;
55         while (!sorted) {
56             n--; //It is the n which will result in one less comparison happening each outer pass;
57                //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
58             sorted = true;  
59             for (int i=0; i < n; i++) {
60                 if (songs[i].getAlbumName().compareTo(songs[i+1].getAlbumName()) > 0) {
61                     Song temp = songs[i];  
62                     songs[i] = songs[i+1];  
63                     songs[i+1] = temp;
64                     sorted = false; //as in the second bubble sort, if swapping happens we'll want to continue, and so
65                                     //with sorted re-set to false again, the while loop continues
66                 }
67             }
68         }
69     }
70     
71     public void sortByGenreName(Song[] songs, int counter) {
72         int n = counter;
73         boolean sorted = false;
74         while (!sorted) {
75             n--; //It is the n which will result in one less comparison happening each outer pass;
76                //whereas, with the first bubble sort we could use the 'pass' variable used for the for loop.
77             sorted = true;  
78             for (int i=0; i < n; i++) {
79                 if (songs[i].getGenreName().compareTo(songs[i+1].getGenreName()) > 0) {
80                     Song temp = songs[i];  
81                     songs[i] = songs[i+1];  
82                     songs[i+1] = temp;
83                     sorted = false; //as in the second bubble sort, if swapping happens we'll want to continue, and so
84                                     //with sorted re-set to false again, the while loop continues
85                 }
86             }
87         }
88     }
89 }
90