Presentation is loading. Please wait.

Presentation is loading. Please wait.

ArrayList is a class that implements the List interface. The List interface is a blueprint for its “implementor” classes. There is another implementor.

Similar presentations


Presentation on theme: "ArrayList is a class that implements the List interface. The List interface is a blueprint for its “implementor” classes. There is another implementor."— Presentation transcript:

1 ArrayList is a class that implements the List interface. The List interface is a blueprint for its “implementor” classes. There is another implementor of the List interface, called LinkedList, that you do not have to know about, but it helps to be aware of it. However, you do need to be aware of the List class. Often, you will see ArrayLists declared this way: List x = new ArrayList(); But why would we do this? This is called polymorphism, which we’ll get into in more detail on another day. But the point is that a later line of code might want to change x to be a LinkedList. If it had been declared as an ArrayList originally, this would be a problem.

2

3 Algorithm An algorithm is a step-by-step set of operations to be performed. Real-life example: a recipe Computer science example: determining the mode in an array

4 Sorting Sorting is the process of arranging the elements in an array in a particular order The sorting process is based on specific values – examples: sorting a list of test scores in ascending numeric order sorting a list of people alphabetically by last name There are many algorithms for sorting an array These algorithms vary in efficiency & speed

5 Big-O Notation The efficiency/performance or “time complexity” of a sorting algorithm is measured in “Big O” notation For example: if an algorithm has O(n^3) efficiency, that means that the maximum number of “touches” of the elements of the array necessary to sort it equals the cube of the number of elements in the array. So, for example, if an array holds 5 numbers, then in the worst-case scenario, it would take this particular sorting algorithm 125 different comparisons or moves of those numbers in order to sort it. If a sorting algorithm has efficiency O(n), this is called “linear time,” meaning that the algortithm’s efficiency is directly proportional to the size of the array.

6 You need to know the basics of these 4 sorting algorithms: 1)Selection Sort 2) Insertion Sort 3) Merge Sort 4) Quick Sort

7 Selection Sort 1.find the smallest element in the array 2.swap it with the first element 3.find the next-smallest element in the array 4.swap it with the second element 5.repeat until all values are in their proper places Efficiency: O(n^2) Inefficient for large lists

8 Example: original array: 3 9 6 1 2 smallest is 1: 1 9 6 3 2 smallest is 2: 1 2 6 3 9 smallest is 3: 1 2 3 6 9 smallest is 6: 1 2 3 6 9

9 Insertion Sort 1)pick an item and insert it into its proper place in a sorted sublist 2)repeat until all items have been inserted Efficiency: O(n) An example: original: 9 3 6 1 2 insert 3: 3 9 6 1 2 insert 6: 3 6 9 1 2 insert 1: 1 3 6 9 2 insert 2: 1 2 3 6 9

10 Merge Sort Divides a list in half, recursively sorts each half (one pair at a time), combines two pairs into a list of 4, etc, until merging the final two lists into one list. At the deepest level of recursion, one-element lists are reached The work of the sort comes in when the sorted sublists are merged together Efficiency: O(n log n) Very well-suited to large lists

11 Quick Sort Chooses a pivot value, then partitions the list into two sublists, (one list contains everything smaller than the pivot, the other contains everything larger), then recursively sorts each sublist Unlike a merge sort, a quick sort does most of its work when it divides the list It doesn’t need to combine sublists after the recursive steps; the list is already sorted at that point Also, unlike a merge sort, the 2 sublists do not have to be the same size One of the more popular sorting techniques Efficiency: O(n log n)

12 Review 1.Selection Sort: Swaps numbers in a list until the list is sorted 2.Insertion Sort: Sorts the first 2 #s in the list, then the first 3, then the first 4, etc… 3.Merge Sort: cuts list in half, sorts them, then combines the 2 lists 4.Quick Sort: cuts list in half using a pivot value; sorts each sublist, no need to combine the lists at the end since they are already sorted

13 Assignment (ItchyAndScratchy) Create an 11-by-11 board which is filled with dashes. Create a class, ItchyClass, containing one method, DisplayBoard. This method should receive the board as a parameter, display it, and return nothing. Randomly place an I and an S on the board. Allow the user to control the I (Itchy) by typing L, R, U, or D, for each of the 4 directions. This will move the I one space on the board. When Itchy catches Scratchy, the game is over. Display how many moves it took. Allow the option to play again. Optional challenge: Allow the user to choose a difficulty level, in which Scratchy (the computer) can move too.


Download ppt "ArrayList is a class that implements the List interface. The List interface is a blueprint for its “implementor” classes. There is another implementor."

Similar presentations


Ads by Google