Search and Sort 1-Dimensional Arrays
We can represent a collection of data items using an array. Java arrays have the following characteristics:
- Once created, the size of an array is fixed. We can access the size of the array using the array’s
length
attribute. - The data items represented by the array are indexed consecutively from
0
tolength-1
. - We can access each data item or more frequently array element using its index.
- The time to access each data item is always equal or almost equal.
We can either pass arrays to Java methods or have a Java method to return an array. The following are several exercises about designing and using Java methods with 1-dimensional arrays. These exercises are about search and sort 1-dimensional arrays.
Exercise 1. Linear Search
Write the definition of method named linearSearch
. The method receives two arguments, an integer array argument and an integer key. It returns the smallest index of the array element equal to the key, or -1 if there is no match or the array is null.
The header of the method is as follows,
public static int linearSearch(int[] numList, int key)
Exercise 2. Reverse Linear Search
Write the definition of method named reverseLinearSearch
. The method receives two arguments, an integer array argument and an integer key. It returns the largest index of the array element equal to the key, or -1 if there is no match or the array is null.
The header of the method is as follows,
public static int reverseLinearSearch(int[] numList, int key)
Exercise 3. Binary Search
Write the definition of the method named binarySearch
. The method receives two arguments, an integer array argument and an integer key. It returns the index of an array element equal to the key, or - insertion point - 1
if there is no match, or -1
the array is null.
The header of the method is as follows,
public static int binarySearch(int[] numList, int key)
Exercise 4. Selection Sort
This exercise is about implementing the Selection Sort algorithm. We divide this into three sub-exercises.
Exercise 4.1. Find Smallest
Write the definition of the method named findSmallest
. The method takes two parameters, integer array numList
and
integer beginIndex
. It finds the smallest element in the array from beginIndex
to the end of the array, and returns
the index of the smallest. It is assumed that numList
is not null.
The header of the method is as follows,
public static int findSmallest(int[] numList, int beginIndex)
Exercise 4.2. Swap Elements
Write the definition of the method named swap
. The method takes two parameters, an int
array, two int
parameters serving as array indices, and exchanges the values of the two array elements indexed by the two indices. The method does not return anything to the caller.
The header of the method is as follows:
public static void swap(int[] numList, int i, int j)
Exercise 4.3. Selection Sort
Write the definition of the method named selectionSort
that implements the selection sort algorithm.
The header of the method is as follows:
public static void selectionSort(int[] numList)
In the implementation, the two methods defined in the above, findSmallest
and swap
must be used. The method sort numList
in ascending order.