Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.

Similar presentations


Presentation on theme: "Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham."— Presentation transcript:

1 Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

2 Week 11 Topics 11.1.1 Sorting: selection, insertion and bubble sort 11.1.2 Profiling the Selection Sort Algorithm 11.1.3 Analyzing the Performance of the Selection Sort Algorithm 11.1.4 Searching 11.2.1 Binary Search

3 11.1.1 Sorting A sorting algorithm rearranges the elements of a collection so that they are stored in sorted order A collection can be sorted in ascending order (from low to high) or in descending order (from high to low) There are many algorithms for sorting, the most common introductory ones are the selection, insertion and bubble sorts

4 11.1.1 Sorting Cont. (selection) The selection sort works by selecting the smallest unsorted item remaining in the list, and then swapping it with the item in the next position to be filled, working from “left to right” A selection sort is a simple algorithm and is easy to implement A selection sort is inefficient for large lists

5 11.1.1 Sorting Cont. (selection) The selection sort is a comparison sort that works as follows: –Find the minimum value in the list –Swap it with the value in the first position –Sort the remainder of the list (excluding the first value) The next value to “the right” becomes the new beginning of the unsorted section of the list and the algorithm above is repeated for the unsorted section until every element in the list (except the last element) is compared (source: Wikipedia)

6 11.1.1 Sorting Cont. (selection) 11917512 11917512 59171112 Initial state Begin first pass Begin second pass Min value for pass 1 is: 5 Min value for pass 2 is: 9

7 11.1.1 Sorting Cont. (selection) 59111712 59111217 Begin third pass Begin fourth pass Collection is now selection sorted in ascending order. Min value for pass 4 is: 12 Min value for pass 3 is: 11 59171112 Final state

8 11.1.1 Sorting Cont. (selection) // SELECTION SORT public static int [ ] selSort(int arr[ ], int left, int right) { // left is beginning of unsorted section for (int i = left; i < right; ++i) { int min = i; for (int j = i; j < right; ++j) // find the min element if (arr[min] > arr[j]) min = j; // swap the minimum element int temp = arr[min]; arr[min] = arr[i]; arr[i] = temp; { return arr; }

9 11.1.1 Sorting Cont. (insertion) An insertion sort works by choosing the next unsorted element and finding where to insert it in the sorted section. When the location is found it then shifts elements to the right and drops the element into place. An insertion sort is a simple algorithm and is easy to implement An insertion sort is inefficient for large lists, but is somewhat more efficient than a selection or a bubble sort

10 11.1.1 Sorting Cont. (insertion) The insertion sort is a comparison sort that works as follows: –Start with the second element in the list as the target –Compare the target to the first element, if target is smaller in value, slide the first element to the “right” and put the value of the target into the first element The third element then becomes the target, and again you “work backwards”, comparing target to element two then one if necessary. At the point target is greater than element to the left, push previous larger elements to the right and put target value in that position. Continue in like with next element to the right until all elements are “targeted”.

11 11.1.1 Sorting Cont. (insertion) 11917512 11917512 91117512 Initial state Begin first pass Begin second pass 1st pass begins at pos 2 2nd pass begins at pos 3

12 11.1.1 Sorting Cont. (insertion) 91117512 59111217 Begin third pass Begin fourth pass Collection is now insertion sorted in ascending order. 4th pass begins at pos 5 3rd pass begins at pos 4 59111712 Final state

13 11.1.1 Sorting Cont. (insertion) // INSERTION SORT public static int [ ] insSort(int arr[ ], int left, int right) { for (int i = left+1; j < right; ++i) { int j, val = arr[i]; // locate position to place target element for ( j= i-1; j >= left && arr[j] > val; --j) arr[j+1] = arr[j]; // slide each element to the right arr[j+1] = val; // drop element into position } return arr; }

14 11.1.1 Sorting Cont. (bubble) The bubble sort (also called the exchange sort) works by repeatedly stepping through the list to be sorted, comparing two items at a time, swapping these two items if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which means the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top (i.e. head) of the list via the swaps. The bubble sort is a simple algorithm and is easy to implement, but is inefficient for large lists

15 11.1.1 Sorting Cont. (bubble) The bubble sort is a comparison sort that works as follows (source - Wikipedia): –Compare adjacent elements. If the first is greater than the second, swap them. –Do this for each pair of adjacent elements, starting with the first two and ending with the last two. At this point the last element should be the greatest. –Repeat the steps for all elements except the last one. –Keep repeating for one fewer element each time, until you have no more pairs to compare.

16 11.1.1 Sorting Cont. (bubble) 11917512 11917512 91151217 Initial state Begin first pass Begin second pass 1st pass compare pattern: 2nd pass compare pattern:

17 11.1.1 Sorting Cont. (bubble) 59111217 59111217 Begin forth pass Final state Collection is now bubble sorted in ascending order. 4th pass compare pattern: Begin third pass 3rd pass compare pattern: 95111217

18 11.1.1 Sorting Cont. (bubble) // BUBBLE SORT public static void bubbleSort(int data[ ]) { boolean isSorted; int tempVar; int passes = 0; do { isSorted = true; for (int i = 1; i < data.length - passes; i++) { if (data[i] < data[i - 1]) { // swap logic tempVar = data[i]; data[i] = data[i - 1]; data[i - 1] = tempVar; isSorted = false; } } passes++; } while (!isSorted); // continue if a swap took place }

19 11.1.2 Profiling the Selection Sort Algorithm An important aspect of studying algorithms is to determine the complexity of a given algorithm Complexity in computer science is usually measured two ways: space complexity and time complexity Space complexity is a measure of the amount of memory an algorithm takes in its execution, while time complexity is a measure of the amount of time an algorithm takes in its execution

20 11.1.2 Profiling the Selection Sort Algorithm Cont. Often an inverse relationship exists: time can be reduced if space is increased, and vice-versa Determining the complexity of an algorithm can be done two ways: mathematical analysis (covered in the next section) and profiling Profiling is the process of gathering runtime statistics about programs, such as execution time, memory usage, and method call counts

21 11.1.2 Profiling the Selection Sort Algorithm Cont. ElementsBubbleSelectionInsertion 2,5603115 5,1201254731 20,4801,969875563 81,92031,39114,0479,125 Sort time in milliseconds for various array sizes The interesting characteristic of this data is that for each of the sorting algorithms, as the number of elements doubles, the sorting time roughly quadruples

22 11.1.3 Analyzing the Performance of the Selection Sort Algorithm To do a selection sort requires finding the smallest value (up to n visits) and then swapping the elements (two visits). The algorithm can be expressed as the equation: 1/2 n² + 5/2 n - 3 The above can be reduced to the fastest growing term n² Thus the selection sort using big-Oh notation is called an O(n²) algorithm. Doubling the data set means a fourfold increase in processing time.

23 11.1.4 Searching To find a value in an unordered collection you must use a linear search (also called a sequential search) A linear search examines all values in a list until it finds a match or reaches the end A linear search locates a value in a list in O(n) steps A linear search is a very inefficient way to find a value in a large list

24 11.2.1 Binary Search A binary search is a technique for finding a particular value in a sorted linear array, by ruling out half of the data at each step A binary search finds the median, makes a comparison to determine whether the desired value comes before or after it, and then searches the remaining half in the same manner A binary search is an example of a divide and conquer algorithm (source – Wikipedia)

25 11.2.1 Binary Search Cont. An example of binary search in action is a simple guessing game in which a player has to guess a positive integer selected by another player between 1 and N, using only questions answered with yes or no. Supposing N is 16 and the number 11 is selected, the game might proceed as follows. Is the number greater than 8? (Yes) Is the number greater than 12? (No) Is the number greater than 10? (Yes) Is the number greater than 11? (No) Therefore, the number must be 11. At each step, we choose a number right in the middle of the range of possible values for the number. For example, once we know the number is greater than 8, but less than or equal to 12, we know to choose a number in the middle of the range [9, 12] (either 10 or 11 will do). Source - Wikipedia

26 11.1.1 Sorting Cont. (selection) // BINARY SEARCH public static int bSearch(int[ ] array, int value, int left, int right) { // Continue while left and right haven't // become equal or crossed over each other while (left < right) { // locate middle index between left and right int mid = (left + right) / 2; if (array[mid] < value) left = mid + 1; // in right half else if (array[mid] > value) right = mid; // in left half else return mid; // found it } return -1; // not found }

27 Reference: Big Java 2 nd Edition by Cay Horstmann 11.1.1 Sorting: selection, insertion and bubble sort (section 19.1 in Big Java) 11.1.2 Profiling the Selection Sort Algorithm (section 19.2 in Big Java) 11.1.3 Analyzing the Performance of the Selection Sort Algorithm (section 19.3 in Big Java) 11.1.4 Searching (section 19.6 in Big Java) 11.2.1 Binary Search (section 19.7 in Big Java)


Download ppt "Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham."

Similar presentations


Ads by Google