COMP 171 Data Structures and Algorithms Tutorial 4 In-Class Exercises: Algorithm Design.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

College of Information Technology & Design
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
CSC 331: Algorithm Analysis Divide-and-Conquer Algorithms.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Lecture 2: Divide and Conquer algorithms Phan Thị Hà Dương
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
2. Getting started Hsu, Lih-Hsing. Computer Theory Lab. Chapter 2P Insertion sort Example: Sorting problem Input: A sequence of n numbers Output:
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Analysis of Algorithms CS 477/677 Randomizing Quicksort Instructor: George Bebis (Appendix C.2, Appendix C.3) (Chapter 5, Chapter 7)
Chapter 7 Sorting Part I. 7.1 Motivation list: a collection of records. keys: the fields used to distinguish among the records. One way to search for.
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
Chapter 1 – Basic Concepts
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.
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.
Sorting. Input: A sequence of n numbers a 1, …, a n Output: A reordering a 1 ’, …, a n ’, such that a 1 ’ < … < a n ’
Lecture 12 Oct 15 Recursion – more examples Chapter 8 problems and solutions Cell arrays, Chapter 10.
CS2420: Lecture 9 Vladimir Kulyukin Computer Science Department Utah State University.
Data Structures Review Session 1
Sorting and Heaps Eugene Weinstein New York University Computer Science Department Data Structures Fall 2008.
Sorting Algorithms Bubble Sort Merge Sort Quick Sort Randomized Quick Sort.
Searching Arrays Linear search Binary search small arrays
Proving correctness. Proof based on loop invariants  an assertion which is satisfied before each iteration of a loop  At termination the loop invariant.
Sorting Chapter 6 Chapter 6 –Insertion Sort 6.1 –Quicksort 6.2 Chapter 5 Chapter 5 –Mergesort 5.2 –Stable Sorts Divide & Conquer.
Algorithm Correctness A correct algorithm is one in which every valid input instance produces the correct output. The correctness must be proved mathematically.
Complexity of algorithms Algorithms can be classified by the amount of time they need to complete compared to their input size. There is a wide variety:
2IL50 Data Structures Fall 2015 Lecture 2: Analysis of Algorithms.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Jessie Zhao Course page: 1.
1 Designing algorithms There are many ways to design an algorithm. Insertion sort uses an incremental approach: having sorted the sub-array A[1…j - 1],
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.
C# PROGRAMMING Searching & Sorting. Objective/Essential Standard Essential Standard 3.00 Apply Advanced Properties of Arrays Indicator 3.03 Apply procedures.
Searching Dr. Jose Annunziato. Linear Search Linear search iterates over an array sequentially searching for a matching element int linearSearch(int haystack[],
1 Chapter 7: Sorting (Insertion Sort, Shellsort) CE 221 Data Structures and Algorithms Izmir University of Economics Text: Read Weiss, § 7.1 – 7.4.
COMP 171 Data Structures and Algorithms Tutorial 3 Merge Sort & Quick Sort.
Algorithms Merge Sort Solving Recurrences The Master Theorem.
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort (iterative, recursive?) * Bubble sort.
Data Structure Introduction.
Algorithms IS 320 Spring 2015 Sorting. 2 The Sorting Problem Input: –A sequence of n numbers a 1, a 2,..., a n Output: –A permutation (reordering) a 1.
QuickSort (Ch. 7) Like Merge-Sort, based on the three-step process of divide- and-conquer. Input: An array A[1…n] of comparable elements, the starting.
Sorting Data Structures and Algorithms (60-254). Sorting Sorting is one of the most well-studied problems in Computer Science The ultimate reference on.
Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University.
Data Structures and Algorithm Analysis Algorithm Analysis and Sorting
2IL50 Data Structures Spring 2016 Lecture 2: Analysis of Algorithms.
 Design and Analysis of Algorithms تصميم وتحليل الخوارزميات (311 عال) Chapter 2 Sorting (insertion Sort, Merge Sort)
Sorting Algorithms Merge Sort Quick Sort Hairong Zhao New Jersey Institute of Technology.
1 Sorting. 2 Sorting Data Items Consider a set of data items  Each item may have more than one field Example: a student record with name, roll no, CGPA,…
RECURSION Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
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.
Searching CS 110: Data Structures and Algorithms First Semester,
Lecture 6COMPSCI.220.FS.T Data Sorting Ordering relation: places each pair ,  of countable items in a fixed order denoted as (  ) or 
1 Chapter 7: Sorting (Insertion Sort, Shellsort) CE 221 Data Structures and Algorithms Izmir University of Economics Text: Read Weiss, § 7.1 – 7.4.
1 Overview Divide and Conquer Merge Sort Quick Sort.
Sorting. 2 The Sorting Problem Input: A sequence of n numbers a 1, a 2,..., a n Output: A permutation (reordering) a 1 ’, a 2 ’,..., a n ’ of the input.
Binary Search Manolis Koubarakis Data Structures and Programming Techniques 1.
Growth of Functions & Algorithms
Searching Given a collection and an element (key) to find… Output
Algorithm Design & Analysis
Proving correctness.
Divide and Conquer (Merge Sort)
Divide and Conquer (Merge Sort)
Divide & Conquer Algorithms
Algorithms.
Presentation transcript:

COMP 171 Data Structures and Algorithms Tutorial 4 In-Class Exercises: Algorithm Design

Exercises (p.37) Observe that the while loop of the Insertion- sort procedure uses a linear search to scan (backward) through the sorted subarray A[0..i-1]. Can we use a binary search instead to improve the overall worst-case running time of insertion sort to Θ (n ㏒ n)?

InsertionSort(A) for i ← 1 to size(A)-1 key ← A[i] j ← i – 1 while j ≧ 0 and A[j] > key A[j + 1] ← A[j] j ← j – 1 end while A[j + 1] ← key end for i end InsertionSort

BinarySearch(A, left, right, s_value) if left ≦ right then middle ←  (left + right) / 2  if A[middle] = s_value then return middle else if A[middle] > s_value then BinarySearch(A, left, middle-1, s_value) else BinarySearch(A, middle+1, right, s_value) end if else return –1; end if end BinarySearch

Exercises (p.37) Describe a Θ(n ㏒ n)-time algorithm that, given a set of S of n integers and another integer x, determines whether or not there exist two elements in S whose sum is exactly x.

Exercises 2-4 (p.39) Let A[1..n] be an array of n distinct numbers. If i A[j], then the pair (i, j) is called an inversion of A. –List the five inversions of the array {2, 3, 8, 6, 1} –What array with elements from the set {1, 2, …, n} has the most inversions? How many does it have?

–What is the relationship between the running time of insertion sort and the number of inversions in the input array? Justify your answer. –Given an algorithm that determines the number of inversions in any permutation on n elements in Θ(n ㏒ n) worst-case time. (Hint: Modify merge sort)

mergesort(A, left, right) if left < right then middle ←  (left + right) / 2  mergesort(A, left, middle) mergesort(A, middle+1, right) merge(A, left, middle+1, right) end if end mergesort

merge(A, p, q, r) n 1 ← q – p n 2 ← r – q + 1 create array L[0..n 1 ], R[0..n 2 ] for i ← 0 to n1-1 do L[i] ← A[p+i] for j ← 0 to n2-1 do R[j] ← A[q+j] L[n 1 ] ← R[n 2 ] ← ∞ I ← j ← 0 for k ← p to r if L[I] < R[j] then A[k] ← L[i] i ← i + 1 else A[k] ← R[j] j ← j + 1 end if end for k end merge