Lecture 11 Searching and Sorting Richard Gesick.

Slides:



Advertisements
Similar presentations
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Advertisements

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search ; Reading p Selection Sort ; Reading p
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
Chapter 14: Sorting and searching. Chapter Goals To study several sorting and searching algorithms To appreciate that algorithms for the same task can.
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.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
Sort Algorithms.
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.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Searching algorithms with simple arrays
Prof. U V THETE Dept. of Computer Science YMA
UNIT - IV SORTING By B.Venkateswarlu Dept of CSE.
Sorting Chapter 14.
Searching and Sorting Arrays
Chapter 9: Sorting and Searching Arrays
Alternate Version of STARTING OUT WITH C++ 4th Edition
16 Searching and Sorting.
CS212: Data Structures and Algorithms
19 Searching and Sorting.
Sorting Mr. Jacobs.
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Searching & Sorting "There's nothing hidden 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.
Design and Analysis of Algorithms
Teach A level Computing: Algorithms and Data Structures
10.3 Bubble Sort Chapter 10 - Sorting.
Searching and Sorting Linear Search Binary Search ; Reading p
Linear and Binary Search
Algorithm design and Analysis
Objectives At the end of the class, students are expected to be able to do the following: Understand the purpose of sorting technique as operations on.
Introduction to Search Algorithms
Bubble, Selection & Insertion sort
CSc 110, Spring 2017 Lecture 39: searching.
Searching and Sorting Arrays
MSIS 655 Advanced Business Applications Programming
Standard Version of Starting Out with C++, 4th Edition
C++ Plus Data Structures
Searching and Sorting 1-D Arrays
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
24 Searching and Sorting.
14 SORTING AND SEARCHING CHAPTER
Principles of Computing – UFCFA3-30-1
Sorting "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.
Review of Searching and Sorting Algorithms
Algorithm Efficiency and Sorting
Searching and Sorting Arrays
Module 8 – Searching & Sorting Algorithms
Searching/Sorting/Searching
Chapter 19 Searching, Sorting and Big O
Principles of Computing – UFCFA3-30-1
Module 8 – Searching & Sorting Algorithms
Algorithm Efficiency and Sorting
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

Lecture 11 Searching and Sorting Richard Gesick

The focus Searching - examining the contents of the array to see if an element exists within the array Sorting - rearranging the elements of the array to be in a particular order

Linear Search Also called sequential search Examines all values in an array until it finds a match or reaches the end Number of visits for a linear search of an array of n elements - The average search visits n/2 elements - The maximum visits is n A linear search is an O(n) algorithm

Linear Search Algorithm steps = 0; foreach (int temp in G) { steps++; if (temp == find) return true; } return false;

Binary Search Locates a value in a sorted array by determining whether the value occurs in the first or second half, then repeating the search in one of the halves Number of visits to search an sorted array of size n? We visit one element (the middle element) then search either the left or right subarray Binary search is a O( log(n) ) algorithm

Binary Search Algorithm int low=0; int high= G.Length; int mid = 0; while (!found) { steps++; mid = (low + high) / 2; if (find == G[mid]) return true; else if (find < G[mid]) high = mid; else low = mid; if (low > high-1 || high < low +1) return false; }

Sorting There are many ways to order elements within a collection.  There is typically a trade-off between complexity of the algorithm and the performance of the algorithm. a simple approach to sorting realize that it is slower than other methods that are more complex to code yet more efficient.

Bubble Sort Repeatedly pair-wise examine elements and swap them if needed; this is called a "Bubble sort" since elements "bubble" to their correct positions. 

Bubble sort explained int [] A={8, 4, 2, 1}; The bubble sort finds the smallest value and then the next smallest etc. It does it by pair-wise comparisons and swaps. 8 and 4, 4 is smaller so it gets swapped, A = 4, 8, 2, 1 4 and 2, 2 is smaller so it gets swapped, A= 2, 8, 4, 1 2 and 1, 1 is smaller so it gets swapped, A= 1,8,4,2 The first pass has been completed so now we start on the same process with index 1 8 and 4, 4 is smaller, swap and A= 1, 4,8,2 4 and 2, 2 is smaller, swap and A= 1, 2, 8, 4 That completes that pass and now the first 2 values are in the correct position. Now the third pass looks at 8 and 4 4 is smaller, swap and A= 1, 2, 4, 8 We’re done. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Bubble Sort Algorithm for (int i = 0; i < A.Length - 1; i++) for (int j = i+1; j < A.Length; j++) { if (A[j] < A[i]) int temp = A[j]; A[j] = A[i]; A[i] = temp; } //notice the extra variable temp to make the swap process easy without accidently misassigning values

Selection Sort The selection sort algorithm repeatedly finds the smallest element in the unsorted tail region of the array and moves it to the front

Sorting an Array of Integers Array in original order 11 9 17 5 12 Find the smallest and swap it with the first element 5 9 17 11 12 Find the next smallest. It is already in the correct place 9 17 11 12 Find the next smallest and swap it with the first element of unsorted portion 5 9 11 17 12 Repeat 5 9 11 12 17 When the unsorted portion is of length 1, we are done

Selection Sort Algorithm int i = 0; int j = 0; for (i = 0; i < B.Length - 1; i++) { int minPos = i; for (j = i + 1; j < B.Length; j++) { if (B[j] < B[minPos]) minPos = j; } if (i != minPos && minPos < B.Length) { int temp = B[minPos]; B[minPos] = B[i]; B[i] = temp;

The bubble sort and selection sort This algorithms are slow when run on large data sets because of the large number of swaps that are done during the processing. Both of these algorithms and the insertion sort that is coming up next are considered to be Big-0 of n squared, O(n2). To keep things simple, consider the n to be the growth factor, so if a data set doubles in size, 2 would be 2, while if it tripled in size, n would be 3. O(n2) means that the time required grows by the square of the factor so if n was 2, the time would grow by about a factor of 4. If n was 3, by a factor of about 9.

Time Taken By Selection Sort on Various Size Arrays

Insertion Sort Consider what you would do to sort a set of cards. This approach is an insertion sort - taking a new item and place it correctly relative to the other items in the "finished" portion.  You continually maintain two sets - unexamined, unsorted cards and a set of examined/sorted cards.  For each card in the unexamined set, take it out of that set and place it correctly relative to the examined set.

Insertion Sort Algorithm public void insertionSort (int[] list) { for (int index = 1; index < list.Length; index++) int key = list [index]; int position = index; // Shift larger values to the right while (position > 0 && key < list[position-1]) list [position] = list [position - 1]; position--; } list [position] = key;

Insertion sort explained int [] A= { 8, 4, 2, 1}; the key = 4, index = 1 and position =1 4 < 8 and position > 0 so the while loops shifts 8 into the 4’s position. A= 8,8,2,1 then position-- so now position =0. The while loop stops. key (4) gets assigned to A[position] so A = 4,8,2,1 Now index goes to 2, key =2 and position =2 2 < 8 and position > 0 so the while loops shifts 8 into the 2’s position. A= 4,8,8,1 then position-- so now position =1. The while loop continues. 2< 4 so the while loops shifts 4 into the first 8’s position. A=4,4,8,1, position -- so now position=0, the while loop ends key (2) gets assigned to A[position] so A = 2,4,8,1 Then one more pass with 1, shifting 8,4,2 right 1 space each so then A= 1,2,4,8 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Sorting in a C# Program The Array and List classes contains sort methods. To use them, the data type you are sorting must implement the IComparble interface. List<string> myList= new List<string>(); . . . myList.Sort(); To sort an array of integers int [] nums= new int [50]; … Array.Sort(nums);