Problem Solving #6: Search & Sort ICS201-071. 2 Outline Review of Key Topics Review of Key Topics Problem 1: Recursive Binary Search … Problem 1: Recursive.

Slides:



Advertisements
Similar presentations
Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
Advertisements

Chapter 10: Applications of Arrays and Strings J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second.
CHAPTER 10 ARRAYS II Applications and Extensions.
Searching and Sorting Algorithms Based on D. S
Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.
Searching Slowly And Fastly. public static int find(int x, int[] a, int n) { for (int loc = 0; loc < n; loc++) if (a[loc] == x) return loc;//found return.
Data Structures & Algorithms CHAPTER 3 Sorting Ms. Manal Al-Asmari.
1 Sorting II: Bubble Sort and Selection Sort CSC326 Information Structure Spring 2009.
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 10 Applications of Arrays.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
1 Lecture 23:Applications of Arrays Introduction to Computer Science Spring 2006.
Sorting Algorithms: Selection, Insertion and Bubble.
Complexity Analysis (Part III ) Analysis of Some Sorting Algorithms. Analysis of Recursive Algorithms.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006.
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
Part 2. Searching Arrays Looking for a specific element in an array E.g., whether a certain score (85) is in a list of scores Linear search Binary search.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Building Java Programs Chapter 13 Searching reading: 13.3.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Data Structures and Algorithms.
Analyzing Complexity of Lists OperationSorted Array Sorted Linked List Unsorted Array Unsorted Linked List Search( L, x ) O(logn) O( n ) O( n ) Insert(
Applications of Arrays (Searching and Sorting) and Strings
Lecture 12. Searching Algorithms and its analysis 1.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Computer Science Searching & Sorting.
1 Linear and Binary Search Instructor: Mainak Chaudhuri
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
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.
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.
Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion.
CSC 211 Data Structures Lecture 13
Chapter 14: Searching and Sorting
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
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 Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index.
Sorting Algorithms: Selection, Insertion and Bubble.
Data Structures Using C++1 Chapter 10 Sorting Algorithms.
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.
Java Programming: Program Design Including Data Structures1 Searching and Sorting  Learn the various search algorithms  Sequential and binary search.
Chapter 10: Class Vector and String, and Enumeration Types Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
Data Structures Using Java1 Chapter 9 Sorting Algorithms.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Lecture Contents  searching  Sequential search algorithm.  Binary search algorithm. 2.
Selection Sort main( ) { int a[ ] = { 17, 6, 13,12, 2 } ; int i, j, t ; for ( i = 0 ; i
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
1 CS 132 Spring 2008 Chapter 10 Sorting Algorithms.
BINARY SEARCH CS16: Introduction to Data Structures & Algorithms Thursday February 12,
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.
Using recursion for Searching and Sorting
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Chapter 18: Searching and Sorting Algorithms
Ch 14: Search and Sorting Yonglei Tao.
Data Structures Using C++
Chapter 7 Single-Dimensional Arrays
Sorting Algorithms: Selection, Insertion and Bubble
Adapted from Pearson Education, Inc.
CSC215 Lecture Algorithms.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Module 8 – Searching & Sorting Algorithms
Module 8 – Searching & Sorting Algorithms
Sorting Algorithms.
Presentation transcript:

Problem Solving #6: Search & Sort ICS

2 Outline Review of Key Topics Review of Key Topics Problem 1: Recursive Binary Search … Problem 1: Recursive Binary Search … Problem 2: Tracing …. Problem 2: Tracing …. Problem 3: Element Insertion … Problem 3: Element Insertion … Problem 4: Sorted Array Reverse … Problem 4: Sorted Array Reverse … Problem 5: Partition Method … Problem 5: Partition Method …

Form Groups of 3 Students and Work on the Following Problem

4 Review of Main Topics Search – –Sequential (linear) search – –Binary search Sort – –Selection sort – –Insertion sort – –Bubble sort – –Merge sort – –Quick sort

5 Problem 1: public static int binarySearch(int[] list, int listLength, int key) { int first = 0, last = listLength - 1; int mid; boolean found = false; while (first <= last && !found) { mid = (first + last) / 2; if (list[mid] == key) found = true; else if(list[mid] > key) last = mid - 1; else first = mid + 1; } if (found) return mid; else return –1; } //end binarySearch Recall

6 Problem 1 … a) Implement the binary search using recursion b) Write a recursive equation for its running time c) Find a closed form solution for the recursive equation in part (b)

7 Solution 1 int recBS(int[] a, int target, int lo, int hi){ 2 int mid = -1; 3 if(lo <= hi) { 4 mid = (hi + lo) / 2; 5 if(target < a[mid]) 6 mid = recBinary(a, target, lo, mid - 1); 7 else if(target > a[mid]) 8 mid = recBinary(a, target, mid + 1, hi); 9 } 10 return mid; 11 }

8 Solution … T(n) = T(n/2) + 1 T(n) = T(n/2) + 1 T(n) = {T(n/2 2 )+1}+1 T(n) = {T(n/2 2 )+1}+1 = T(n/2 2 )+2 = T(n/2 2 )+2 = {T(n/2 3 )+1}+2 = {T(n/2 3 )+1}+2 = T(n/2 3 )+3 = T(n/2 3 )+3 …… …… = T(n/2 k )+k = T(n/2 k )+k = T(1)+k when n/2 k =1  k=log 2 n = T(1)+k when n/2 k =1  k=log 2 n Since T(1) = 1 then T(n) = O(log 2 n)

9 Problem 2 public static void insertionSort(int[] list, int listLength) { int firstOutOfOrder, location; int temp; for (firstOutOfOrder = 1; firstOutOfOrder < listLength; firstOutOfOrder++) if (list[firstOutOfOrder] < list[firstOutOfOrder - 1]) { temp = list[firstOutOfOrder]; location = firstOutOfOrder; do { list[location] = list[location - 1]; location--; } while(location > 0 && list[location - 1] > temp); list[location] = temp; } } //end insertionSort public static void selectionSort(int[] list, int listLength) { int index; int smallestIndex; int minIndex; int temp; for (index = 0; index < listLength – 1; index++) { smallestIndex = index; for (minIndex = index + 1; minIndex < listLength; minIndex++) if (list[minIndex] < list[smallestIndex]) smallestIndex = minIndex; temp = list[smallestIndex]; list[smallestIndex] = list[index]; list[index] = temp; } Recall

10 Problem 2 … Given the following array Given the following array A = {4, 8, 3, 5, 1, 7} Trace each of the following sorting algorithms and show the array content after each iteration Trace each of the following sorting algorithms and show the array content after each iteration –Insertion sort –Selection sort

11 Problem 3 Insert a given item in its right place in a sorted array such that the array remains sorted Insert a given item in its right place in a sorted array such that the array remains sorted What is the running time? What is the running time? Can you think of another alternative? Can you think of another alternative?

12 Problem 4 Reverse the content of an ordered array Reverse the content of an ordered array –Using a temporary array –In the same array What is its running time? What is its running time?

13 Problem 5 Given the following array Given the following array A = {4, 8, 3, 5, 1, 7} Trace the partition method in quick sort. Trace the partition method in quick sort. Derive its running time if the array has length n. Derive its running time if the array has length n. private int partition(int from, int to) { int pivot = a[from]; int i = from - 1; int j = to + 1; while (i pivot) j--; if (i < j) swap(i, j); } return j; } Recall