Computer Science Searching & Sorting.

Slides:



Advertisements
Similar presentations
Topic 14 Searching and Simple Sorts "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The.
Advertisements

Garfield AP Computer Science
Topic 24 sorting and searching arrays "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be."
CPS120: Introduction to Computer Science Searching and Sorting.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Data Structures and Algorithms
Topic 17 Fast Sorting "The bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical.
CS 307 Fundamentals of Computer ScienceSorting and Searching 1 Topic 11 Sorting and Searching "There's nothing in your head the sorting hat can't see.
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
CHAPTER 11 Sorting.
1 Lecture 23:Applications of Arrays Introduction to Computer Science Spring 2006.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
(c) , University of Washington
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
CSE 373: Data Structures and Algorithms Lecture 6: Sorting 1.
1 Sorting and Searching "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting.
Adapted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
CS 221 – Computer Science II Sorting and Searching 1 "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Sorting and Searching. Selection Sort  “Search-and-Swap” algorithm 1) Find the smallest element in the array and exchange it with a[0], the first element.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 9 Sorting 1. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Chapter 9 sorting. Insertion Sort I The list is assumed to be broken into a sorted portion and an unsorted portion The list is assumed to be broken into.
CSE 143 Lecture 16 Sorting reading: 13.1, slides created by Marty Stepp
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Sorting Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2010.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Building Java Programs Chapter 13 Sorting reading: 13.3, 13.4.
Searching and Sorting Searching algorithms with simple arrays
Sort Algorithm.
Lecture 25: Searching and Sorting
Searching and Sorting Arrays
Quicksort "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Fast Sorting "The bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical problems."
slides created by Marty Stepp and Hélène Martin
Adapted from slides by Marty Stepp and Stuart Reges
Topic 14 Searching and Simple Sorts
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
slides adapted from Marty Stepp and Hélène Martin
Building Java Programs
CSC215 Lecture Algorithms.
Adapted from slides by Marty Stepp and Stuart Reges
slides created by Marty Stepp
Sub-Quadratic Sorting Algorithms
slides created by Marty Stepp
Topic 14 Searching and Simple Sorts
Topic 17 Faster Sorting "The bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical.
Topic 24 sorting and searching arrays
slides created by Marty Stepp
CSE 143 Sorting reading: 13.3, 13.4.
CSE 373 Data Structures and Algorithms
slides created by Marty Stepp and Hélène Martin
Searching and Sorting Arrays
CSE 373 Sorting 2: Selection, Insertion, Shell Sort
Stacks, Queues, ListNodes
Presentation transcript:

Computer Science Searching & Sorting

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

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

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

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; }

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

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…

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;

Types of Sorts Insertion Sort Selection Sort Bubble Sort Shellsort

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.

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

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

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; }

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.

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

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

Shell Sort Example Gap of five –swap every 5 numbers lowest to highest 46 2 83 41 102 5 17 31 64 49 18 5 2 83 41 102 18 17 31 64 49 46 5 2 83 41 102 17 18 31 64 49 46 5 2 31 41 102 18 17 83 64 49 46 5 2 31 41 49 18 17 83 64 102 46 5 2 31 41 49 18 17 83 64 102 46 5 2 17 41 31 18 46 83 49 102 64 5 2 17 18 31 41 46 83 49 102 64 2 5 17 18 31 41 46 49 64 83 102 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

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

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

Merge Sort CS 307 Fundamentals of Computer Science Sorting and Searching