In the past few weeks, we focused on learning basic algorithms in Java as well as sub-programme (method or function) and arrays.
Four key algorithms
In Java, we have four important algorithms that help us to search and sort in different approaches based on different situations:
- Sequential search
- Binary Search
- Bubble Sort
- Selection Sort
Sequential search
- Sequential search can be known as linear search. We first get in touched with sequential search in the “battle ship” activity. Linear search is just simply examining through all elements in sequence and comparing them with the targeted element.
- This algorithm is usually used for file reading
- Create an array with names inside
- Receive user’s input
- Assign variable i and found with value 0 and -1.
- The program will continue looping if value of i is smaller than array’s length and found is equals to -1.
- If user’s input is matched with one element in the array, value of found will be changed to i
- After the loop ended, using if statement, the program will decide what to output
For further explanation, we created a sequential sort program.



Binary Search:
This can only be used when elements are already sorted. This algorithm helps us to increase efficiency by halving searching range after every comparison.
Steps are shown below:
- Designated variables with value of first, middle, and last index of the array
- Compare search number with element located at position of the middle
- If middle is bigger, move last to middle index – 1
- If smaller, move first to middle index + 1
- After the loop is ended, if found still equals to -1, the program will output that user’s input is not found in the array; otherwise, it will output user’s input is found in the array index found.



Sorting an array:
In order to increase the efficiency and avoid using sequential search for large arrays, we have bubble sort and selection sort. With these two algorithms, we are able to sort elements of an array in ascending or descending order, enabling usages of binary search.
Bubble sort:
- This algorithm compares elements in pairs and swaps them if these two elements are in wrong order.
- It will continue looping until everything is in the right order



We also created a trace table for Bubble sort:

Selection Sort
- Divide the array into two portions: sorted and unsorted parts
- Variables for index of first unsorted element and the smallest (or largest) element
- Only sort elements within unsorted range will be compared and swapped based on specific conditions
- For every single loop, the value of index of smallest element will be assigned to variable “smallest”.


To compare selection sort and bubble sort:
| Selection sort | Bubble sort |
| Fast and efficient | Slow and inefficient |
| Item selection | Item swapping |
One-dimensional Array
In Java, array can be used as a container that contains sequence of variables. Array contains variables of the same data type, which are called elements. Index is used to represent each element’s position (start counting from 0).
Declaration and Initialization of Arrays
We need two steps to create an array:
Declare a reference (specific address of the block) to the array:
dataType arrayName[];
Instantiate an array:
arrayName = new datatype[size];
Two-Dimensional Array (HL)
Two-dimensional arrays are arrays of arrays. We have arrays as elements in the array.
- DataType[] [] arrayName = new DataType [] [];
- First []: row
- Second []: store column data in a row
As practice, we did some exercises on 2D array.


METHODS
- Methods:
- It has header and body parts (containing declarations and statements). For the header part, it tells us the visibility to objects of other class (whether private or public), whether the method returns a result (having specific type or void), method name, and whether the method takes parameters.
- Sub-program contains instructions in sequence that perform a specific task, which is predefined before we write the main program.
- We have other names for different computer programming languages: sub-procedures, function, routine, method, modules…
- Parameters: They are another sort of variables inside bracket of methods’ headers, which receive values from outside of the method.
- Arguments: values of expressions passed to a method, corresponding with parameters of the method.
We have three types of method:
- Accessor: being used in order to return current values of object data in fields. In this case, they return String value of title and author to the main program.
- Mutator: “Set” method that can change values of object data
- Utility: other sorts of methods that accomplish a variety of tasks.
Benefits of Methods:
1.Reducing programme duplication
2.Maintainability
3.Easy for team to cooperate: People can work on different parts of the program and combine them together
4.Code reusability: When we are writing sub-programs, they can still be used in future projects.
We also did some exercises on OOP and methods:


This part of Java starts to get interesting because we can add more function to our code and can have more user friendly interface. But still, I am not very familiar with OOP concepts and methods, and that’s something I need to improve in the future.
