Logout

# Sorting and Searching Arrays of Objects - Booleans Too

This can be a royal pain, so here are some hints:

Some key parts and point for implementing the boolean:

1. A way to get in the boolean information: So here, just have the user type in yes or no, rather than the admittedly better way of having a check box, for example.

2. A way to output both pieces of associated search result; do it just in one text box.

```370 else if (searchComboBox.getSelectedItem().equals("Genre")) {
371             s.selectionSortByGenre(movieArray);
372             int result = s.binarySearchOfGenre(movieArray, searchInputTF.getText());
373             if (result == -1) {
375
376             }
377             searchResultTF.setText(movieArray[result].getMovieName() + " --" + movieArray[result].getHaveSeenIt());
378
379         } ```

3. An approach to doing the binary search of a boolean:

I'll just give you the code, but please do try to understand why this works by reading the comments. And when doig so, just think of how the data will be ordered as follows:

0000000000000001111111111

And so think about what happens in the above case when we land on the first mid, which will be a 0. If we're looking for a 0, then we've found one; otherwise, we would need to look in the right hand half of the array, and so make one past our mid the new low. See below.

``` 97 public int binarySearchOfHaveSeenIt(Movie2222 arr[], String key) {
98
99         boolean haveSeenIt = false;
100         if(key.equalsIgnoreCase("yes")){
101             haveSeenIt = true;
102         }
103
104         int low = 0;
105         int high = arr.length - 1;
106         while (low <= high) {
107             int mid = (low + high) / 2;
108             if (arr[mid].getHaveSeenIt() == haveSeenIt) {//So if looking for false, return false, if looking for true, return true.
109                 return mid;
110             } else if (arr[mid].getHaveSeenIt() == false) {//This means we must be looking for true, but we landed on a false, so:
111                 low = mid + 1;
112             } else {//This means we must be looking for false, but landed on a true, so:
113                 high = mid - 1;
114             }
115         }
116         return -1;
117     }
```

For this homework assignment, you will make your own GUI Sort & Search and upload it. Naturally, there will be a lot of copying and pasting, and that's both normal and fine. But I will make it so that you will have to do a few things differnently, so be careful after you paste to make all the necessary changes.