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.

Slides:



Advertisements
Similar presentations
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Advertisements

Garfield AP Computer Science
HST 952 Computing for Biomedical Scientists Lecture 9.
Sorting Algorithms and Average Case Time Complexity
System Programming in C Lecture 4. Lecture Summary Operations with Arrays: –Searching the array –Sorting the array.
CS203 Programming with Data Structures Sorting California State University, Los Angeles.
C++ Plus Data Structures
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Computer Programming Sorting and Sorting Algorithms 1.
Algorithm Efficiency and Sorting
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search ; Reading p Selection Sort ; Reading p
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
Simple Sort Algorithms Selection Sort Bubble Sort Insertion Sort.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Copyright © 2013 by John Wiley & Sons. All rights reserved. SORTING AND SEARCHING CHAPTER Slides by Rick Giles 14.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 9 Searching Arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Searching. The process used to find the location of a target among a list of objects Searching an array finds the index of first element in an array containing.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
CSE 373 Data Structures and Algorithms
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
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.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
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.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
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.
CS 162 Intro to Programming II Sorting Introduction & Selection Sort 1.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
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
Sorting Chapter 14.
Searching and Sorting Arrays
Lecture 14 Searching and Sorting Richard Gesick.
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.
Linear and Binary Search
Introduction to Search Algorithms
Bubble, Selection & Insertion sort
CSc 110, Spring 2017 Lecture 39: searching.
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
MSIS 655 Advanced Business Applications Programming
Searching and Sorting 1-D Arrays
Review of Searching and Sorting Algorithms
Searching and Sorting Arrays
Module 8 – Searching & Sorting Algorithms
Module 8 – Searching & Sorting Algorithms
Module 8 – Searching & Sorting Algorithms
Sorting Algorithms.
Presentation transcript:

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 Sorting - rearranging the elements of the array to be in a particular order

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 Algorithm for (int i = 0; i < A.Length - 1; i++) for (int j = i; j < A.Length; j++) { if (A[j] < A[i]) { int temp = A[j]; A[j] = A[i]; A[i] = temp; }

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 Find the smallest and swap it with the first element Find the next smallest. It is already in the correct place

Sorting an Array of Integers (2) Find the next smallest and swap it with the first element of unsorted portion Repeat When the unsorted portion is of length 1, we are done This algorithm is slow when run on large data sets

Time Taken By Selection Sort on Various Size Arrays

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

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 List inOrder = new List (); foreach (int temp in C) { bool added = false; for (int i = 0; i < inOrder.Count; i++) { if (temp < inOrder[i]) { inOrder.Insert(i, temp); added = true; break; } if (!added) inOrder.Add(temp); }

Sorting in a C# Program The List class contains sort methods To sort an array of integers convert to a List and call the sort method List E=D.ToList (); E.Sort();

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