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.

Slides:



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

Bubble Sort Algorithm It is so named because numbers (or letters) which are in the wrong place “bubble-up” to their correct positions (like fizzy lemonade)
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."
CS203 Programming with Data Structures Sorting California State University, Los Angeles.
SORTING. Selection Sort (Basic) 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignore the sorted.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the.
June Searching CE : Fundamental Programming Techniques 16 Searching1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
CS 1400 March 30, 2007 Chapter 8 Searching and Sorting.
Searching Arrays Linear search Binary search small arrays
1 Two-Dimensional Arrays. 2 Can be visualized as consisting m rows, each of n columns Syntax: datatype arrayname [row] [ column] ; Example: int val[3]
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Chapter 13 Lists. List  List  A variable-length, linear collection of homogeneous components  Example  StudentRec Students[100];  A list of 100 students.
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.
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.
Data Structures Using C++1 Search Algorithms Sequential Search (Linear Search) Binary Search.
Lecture 12. Searching Algorithms and its analysis 1.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Computer Science Searching & Sorting.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
1 Searching and Sorting Linear Search Binary Search.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
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. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
C# PROGRAMMING Searching & Sorting. Objective/Essential Standard Essential Standard 3.00 Apply Advanced Properties of Arrays Indicator 3.03 Apply procedures.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
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.
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.
1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.
STARTING OUT WITH STARTING OUT WITH Class 3 Honors.
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.
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Searching When we maintain a collection of data,
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.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Data Structures Arrays and Lists Part 2 More List Operations.
ARRAYS Lecture void reverse (int x[], int size) { int i; for (i=0; i< (size/2); i++) temp = x[size-i-1] ; x[size-1-1] = x[i] ; x[i] = temp;
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
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.
APPLICATIONS OF RECURSION Copyright © 2006 Pearson Addison-Wesley. All rights reserved
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Searching algorithms with simple arrays
Searching and Sorting Algorithms
Lecture 14 Searching and Sorting Richard Gesick.
Sorting Algorithms.
Search Algorithms Sequential Search (Linear Search) Binary Search
Applications of Recursion
Linear and Binary Search
CSc 110, Spring 2017 Lecture 39: searching.
Introduction to Programming
searching Concept: Linear search Binary search
Lecture 11 Searching and Sorting Richard Gesick.
Search,Sort,Recursion.
Topic 24 sorting and searching arrays
Search,Sort,Recursion.
Sorting.
Presentation transcript:

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 the front

Selection Sort Animations sort/ sort/ ecturenotes/malamb/SortingDemos/Selecti onSortDemo.html ecturenotes/malamb/SortingDemos/Selecti onSortDemo.html cs162/javaProgs/sort/SelectSort.html cs162/javaProgs/sort/SelectSort.html

Selection Sort Example Code int currentMinIndex = 0; for (int front = 0; front < intArray.length; front++) { currentMinIndex = front; for (int i = front; i < intArray.length; i++) { if (intArray[i] < intArray[currentMinIndex]) { currentMinIndex = i; } int tmp = intArray[front]; intArray[front] = intArray[currentMinIndex]; intArray[currentMinIndex] = tmp; }

Bubble Sort 1. Start at the bottom / end of the array 2. Compare the two elements and swap them if the smaller number is on the bottom 3. Move up one 4. Repeat steps 1 through 3 until smallest number has "floated" to the top 5. Start at the bottom again and repeat the above steps for the next smallest number. This time, the front / top of the list can be ignored because the smallest number is guaranteed to be there.

Bubble Sort Animations rt/ rt/ cs162/javaProgs/sort/BubbleSort.html cs162/javaProgs/sort/BubbleSort.html

Bubble Sort Example Code int front = 0; int pos = 0; for (front = 0; front < intArray.length; front++) { for (pos = intArray.length-1; pos > front; pos--) { if (intArray[pos] < intArray[pos-1]) { int tmp = intArray[pos]; intArray[pos] = intArray[pos-1]; intArray[pos-1] = tmp; }

Linear Search 1. Start from the beginning of the list 2. Check if current element matches key 3. Move to next element 4. Repeat steps 2-3 until key is found or end of list is reached

Linear Search Example Code int key = 0;//key may be any value for(int i = 0; i < intArray.length; i++) { if (key == intArray[i]) return i; } return -1;

Binary Search 1. Assume sorted list 2. Go to the middle point 3. If the middle element matches the key, then the search is over 4. If key is less than middle element, go to the left (down), else go to the right (up) 5. Repeat steps 2-4 until key is found or when the left and right bounds pass each other

Binary Search Example Code int key = 0;//key may be any value int first = 0; int last = intArray.length-1;; int mid = 0; boolean found = false; while( (!found) && (first <= last) ) { mid = (first + last) / 2; if(key == intArray[mid]) found = true; if(key < intArray[mid]) last = mid - 1; if(key > intArray[mid]) first = mid + 1; }