Lab 4: ArrayLists and Lists
Goal: To become familiar with the strengths and weaknesses of different collection types (ArrayLists and Lists) by implementing them and trying three basic collection operations, sorting, searching, and inserting. In order to see the performance differences of these algorithms, a large collection of Cities will be used.
Part 3 - Sorting
Next, we want to be able to sort our data so we can easily locate information within the Collection. There are two types of sorts we want to implement:
InsertionSort
|
In an insertion sort, the data that we are sorting gets processed one piece at a time. We start from one end of the array/list, and insert that value. When we only have one value, the insertion does not change anything. After this, we proceed onward and insert the next value. Into our previously sorted values. We proceed onward until we have inserted all of the data, and the entire list is sorted. |
MergeSort
|
In a merge sort, we use a divide and conquer approach to solving our problem. We split the data into two equal parts, and then sort each of these halves in the same manner. Once we have divided all of the data into single items, we can begin the actual merging of the sorted pieces. The data is paired off and merged together. This continues with larger and larger pairs until we are back up to the base split we made in the data. Once those two sections have been merged, the data is sorted. |
Implement these two sorting strategies for both of the Collection types. If you have any questions on them, ask the lab instructor.
Merge Sort
|
Insertion Sort
|
Part 4 - Searching
Searching is also an important feature, and its speed can normally be improved if you have a sorted collection. We want to implement sorting for both ArrayLists and Lists.
Add a search function to the AAlgorithms class, can you think of any way the search can be optimized because the List is sorted, because the ArrayList is sorted?