Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science Searching & Sorting.

Similar presentations


Presentation on theme: "Computer Science Searching & Sorting."— Presentation transcript:

1 Computer Science Searching & Sorting

2 Searching and Sorting As you know computers are fantastic machines for completing repetitive tasks They are really good at Searching - looking for things Sorting –organizing things Many techniques searching and sorting techniques have developed as computers have evolved We are going to look at just a few of these techniques Since these techniques all require a series of steps they are called algorithms

3 Types of Searches & Sorts
Sequential or Linear Searches Binary Searches Sorts Insertion Sorts Selection Sorts Bubble Sorts Shell Sorts

4 Sequential Search Looking for something in a systematic way Algorithm:
Start at first item Is it the item you are looking for? No Go to the next item Repeat until item is found

5 Example 1 – 10.1 pg 382 Carter The method performs a sequential search on an array of String values for the value called item. If the search is successful, the method returns the index in the array of item, if not it returns -1. public int seqSearch(String[] list, String item) { int location=-1 for(int i = 0; i < list.length; i++) if(list[i].equals(item)) location=i; return location; }

6 Binary Search The Binary search is also known as the divide and conquer search This requires the items to be sorting in some kind of order for example from smallest to largest Algorithm: Sort items in order Find the middle point Ignore half that does not contain item Find middle point of the half Repeat until item is found Note: Look at Example 1 pg 386 Carter

7 Binary Search list low item middle item high item list
Is middle item what we are looking for? If not is it more or less than the target item? (Assume lower) list low middle high item item item and so forth…

8 Example 2 – 10.2 pg 388 Carter This examples uses a binary search for the value item in an array list of double value It will only keep search as long as bottom ≤ top public static int bsearch(double[] list, double item){ int location = -1; int bottom = 0; int top = list.length - 1; int mid; while( bottom == -1 && !found){ mid = (bottom+top)/2); if(list[mid] == item){ found=true; location=mid; } else if(list[mid]<item) bottom = mid + 1; else top = mid - 1; return result;

9 Types of Sorts Insertion Sort Selection Sort Bubble Sort Shellsort

10 Selection Sort On 1st pass, locate the smallest array element and swap it with the first array element On 2nd pass, locate the second smallest element and swap it with the second element of the array On the 3rd pass, locate the next smallest element and swap it with the third element of the array and so forth.... If the array contains N elements, it will be completely sorted after at most N–1 passes.

11 Insert & Selection Sort
Both use the concept of swapping Insertion Sort - Algorithm Rearrange items from smallest to largest by comparing the first two items and swapping if the first item is greater than the second item Repeat the process for the next value Selection Sort - Algorithm Search through the list and find the smallest element swap the smallest element with the first element repeat starting at second element and find the second smallest element

12 Selection Sort Example
Sort an array 5 random numbers ranging from 1-10. Example : Number are:   first iteration, 2 is already the lowest number and it is in first spot – do nothing Next lowest number is 3 exchange with 8 4 is the lowest number – do nothing 6 is the lowest number after that - done

13 Selection Sort Example
public static void selectionSort(int[] list){ int min; int temp; for(int i = 0; i < list.length - 1; i++){ min = i; for(int j = i + 1; j < list.length; j++) if(list[j] < list[min]) min = j; temp = list[i]; list[i] = list[min]; list[min] = temp; }

14 Bubble Sort Bubble Sort is very similar to Selection Sort
The key difference is that it compares neighbours, not the smallest value. Algorithm Compare adjacent pairs of data. If the pairs are not in order, swap them. Continue until all data is processed.

15 Bubble Sort Example Sort an array 5 random numbers ranging from 1-10
Example : Numbers are:   First Pass is 2 > 8 = No is 8 > 4 = Yes - move is 8 > 6 = Yes - move is 8> 3 = Yes - done Second Pass is 2 > 4 = No - leave  is 4 > 6 = No - leave is 6 > 3 = Yes - move is 6 > 8 = No - done Third Pass is 2 > 4 = No – leave is 4 > 3 = Yes - move is 4 > 6 = No – leave is 6 > 8 = No - done

16 ShellSort Created by Donald Shell in 1959
Wanted to stop moving data small distances (in the case of insertion sort and bubble sort) and stop making swaps that are not helpful (in the case of selection sort) Started with sub arrays created by looking at data that is far apart and then reduce the gap size

17 Shell Sort Example Gap of five –swap every 5 numbers lowest to highest
Sort 46, 5 18 Swap lowest to highest Sort 2, 17 No swap already in order Sort 83, 31 Swap Sort 41, 64 No swap Sort 102,49 Swap – done for gap 5 Gap remaining 2 Sort 5,31, 49, 17, 64,46 Gap 2 Sort 2,41, 18, 83, 102 Gap 1 Use insertion sort

18 Quick Sort Invented by C.A.R. (Tony) Hoare
A divide and conquer approach that uses recursion If the list has 0 or 1 elements it is sorted otherwise, pick any element p in the list. This is called the pivot value Partition the list minus the pivot into two sub lists according to values less than or greater than the pivot. (equal values go to either) return the quicksort of the first list followed by the quicksort of the second list

19 Merge Sort Algorithm If a list has 1 element or 0 elements it is sorted If a list has more than 2 split into 2 separate lists Perform this algorithm on each of those smaller lists Take the 2 sorted lists and merge them together One of the oldest algorithm developed Jon von Neumann

20 Merge Sort CS 307 Fundamentals of Computer Science
Sorting and Searching


Download ppt "Computer Science Searching & Sorting."

Similar presentations


Ads by Google