Lecture 2 MAS 714 Hartmut Klauck

Slides:



Advertisements
Similar presentations
Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
Advertisements

DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Spring 2015 Lecture 5: QuickSort & Selection
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
CMPS1371 Introduction to Computing for Engineers SORTING.
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.
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 ’
Simple Sorting Algorithms
CS421 - Course Information Website Syllabus Schedule The Book:
Lecture 2: Divide and Conquer I: Merge-Sort and Master Theorem Shang-Hua Teng.
TDDB56 DALGOPT-D DALG-C Lecture 8 – Sorting (part I) Jan Maluszynski - HT Sorting: –Intro: aspects of sorting, different strategies –Insertion.
TTIT33 Algorithms and Optimization – Dalg Lecture 2 HT TTIT33 Algorithms and optimization Lecture 2 Algorithms Sorting [GT] 3.1.2, 11 [LD] ,
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Algorithm Efficiency and Sorting
CS Main Questions Given that the computer is the Great Symbol Manipulator, there are three main questions in the field of computer science: What kinds.
Analysis of Algorithms CS 477/677
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
Lecture 2 We have given O(n 3 ), O(n 2 ), O(nlogn) algorithms for the max sub-range problem. This time, a linear time algorithm! The idea is as follows:
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:
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Analysis of Algorithms CS 477/677
Getting Started Introduction to Algorithms Jeff Chastine.
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort (iterative, recursive?) * Bubble sort.
September 17, 2001 Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
1 Sorting (Bubble Sort, Insertion Sort, Selection Sort)
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
Sorting Fundamental Data Structures and Algorithms Aleks Nanevski February 17, 2004.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Young CS 331 D&A of Algo. Topic: Divide and Conquer1 Divide-and-Conquer General idea: Divide a problem into subprograms of the same kind; solve subprograms.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
CSC317 1 So far so good, but can we do better? Yes, cheaper by halves... orkbook/cheaperbyhalf.html.
A Introduction to Computing II Lecture 7: Sorting 1 Fall Session 2000.
Linear Sorting. Comparison based sorting Any sorting algorithm which is based on comparing the input elements has a lower bound of Proof, since there.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
SORTING AND ASYMPTOTIC COMPLEXITY Lecture 13 CS2110 – Fall 2009.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 4.
Lecture 2 Algorithm Analysis
Analysis of Algorithms CS 477/677
Growth of Functions & Algorithms
CS 3343: Analysis of Algorithms
Simple Sorting Algorithms
Divide and Conquer.
CS 3343: Analysis of Algorithms
Algorithm design and Analysis
Data Structures Review Session
8/04/2009 Many thanks to David Sun for some of the included slides!
Topic: Divide and Conquer
Sorting … and Insertion Sort.
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
Simple Sorting Methods: Bubble, Selection, Insertion, Shell
Divide and Conquer Algorithms Part I
Ch. 2: Getting Started.
Topic: Divide and Conquer
The Selection Problem.
Algorithms.
Discrete Mathematics CS 2610
Algorithms and Data Structures Lecture II
Presentation transcript:

Lecture 2 MAS 714 Hartmut Klauck Theory of Computing Lecture 2 MAS 714 Hartmut Klauck

O, , £ Let f,g be two monotone increasing functions that sends N to R+ f=O(g) if 9 n0,c 8 n>n0: f(n)<c g(n) Example: f(n)=n, g(n)=1000n+100) g(n)=O(f(n)) Set c=1001 and n0=100 Example: f(n)=n log n, g(n)=n2

O, , £ Let f,g be two monotone increasing functions that send N to R+ f = (g) iff g=O(f) Definition by Knuth f = £(g) iff [ f=O(g) and g=O(f) ] o, !: asymptotically smaller/larger f=o(g) iff lim f(n)/g(n)=0 E.g., n=o(n2) But 2n2 + 100 n=£(n2)

Sorting Computers spend a lot of time sorting! Assume we have a list of numbers x1,…,xn from a universe U For simplicity assume the xi are distinct The goal is to compute a permutation ¼ such that x ¼(1)< x¼(2) <  < x¼(n) Think of a deck of cards

InsertionSort An intuitive sorting algorithm The input is provided in A[1…n] Code: for i = 2 to n, for (k = i; k > 1 and A[k] < A[k-1]; k--) swap A[k,k-1] → invariant: A[1..i] is sorted end Clear: O(n2) comparisons and O(n2) swaps Algorithm works in place, i.e., uses linear space

Correctness By induction Base case: n=2: We have one conditional swap operation, hence the output is sorted Induction Hypothesis: After iteration i the elements A[1]…A[i] are sorted Induction Step: Consider Step i+1. A[1]…A[i] are sorted already. Inner loop starts from A[i+1] and moves it to the correct position. After this A[1]…A[i+1] are sorted.

Best Case In the worst case Insertion Sort takes time (n2) If the input sequence is already sorted the algorithms takes time O(n) The same is true if the input is almost sorted important in practice Algorithm is simple and fast for small n

Worst Case On some inputs InsertionSort takes (n2) steps Proof: consider a sequence that is decreasing, e.g., n,n-1,n-2,…,2,1 Each element is moved from position i to position 1 Hence the running time is at least i=1,…,n i = (n2)

Can we do better? Attempt 1: Searching for the position to insert the next element is inefficient, employ binary search Ordered search: Given an array A with n numbers in a sorted sequence, and a number x, find the smallest i such that A[i]¸ x Use A[n+1]=1

Linear Search Simplest way to search Run through A (from 1 to n) and compare A[i] with x until A[i]¸ x is found, output i Time: £(n) Can also be used to search unsorted Arrays

Binary Search If the array is sorted already, we can find an item much faster! Assume we search for a x among A[1]<...<A[n] Algorithm (to be called with l=1 and r=n): BinSearch(x,A,l,r] If r-l=0 test if A[l]=x, end Compare A[(r-l)/2+l] with x If A[(r-l)/2+l]=x output (r-l)/2+l, end If A[(r-l)/2+l]> x BinSearch(x,A,l,(r-l)/2+l) If A[(r-l)/2+l]<x Bin Search(x,A,(r-l)/2+l,r)

Time of Binary Search Define T(n) as the time/number of comparisons needed on Arrays of length n T(2)=1 T(n)=T(n/2)+1 Solution: T(n)=log(n) All logs have basis 2

Recursion We just described an algorithm via recursion: a procedure that calls itself This is often convenient but we must make sure that the recursion eventually terminates Have a base case (here r=l) Reduce some parameter in each call (here r-l)

Binary Insertion Sort Using binary search in InsertionSort reduces the number of comparisons to O(n log n) The outer loop is executed n times, each inner loop now uses log n comparisons Unfortunately the number of swaps does not decrease: To insert an element we need to shift the remaining array to the right!

Quicksort Quicksort follows the „Divide and Conquer“ paradigm The algorithm is best described recursively Idea: Split the sequence into two All elements in one sequence are smaller than in the other Sort each sequence Put them back together

Quicksort Quicksort(A,l,r) If l=r return A Choose a pivot position j between l and r u=1,v=1, initialize arrays B,C for (i=l…r): If A[i]<A[j] then B[u]=A[i], u++ If A[i]>A[j] then C[v]=A[i], v++ Run Quicksort(B,1,u) and Quicksort(C,1,v) and return their output (concatenated), with A[j] in the middle

How fast is it? The quality of the algorithm depends on how we split up the sequence Intuition: Even split will be best Questions: How are the asymptotics? Are approximately even splits good enough?

Worst Case Time We look at the case when we really just split into the pivot and the rest (maximally uneven) Let T(n) denote the number of comparisons for n elements T(2)=1 T(n)<T(n-1)+n-1 Solving the recurrence gives T(n)=O(n2)

Best Case Time Every pivot splits the sequence in half T(2)=1 T(n)=2T(n/2)+n-1 Questions: How to solve this? What if the split is 3/4 vs. 1/4 ?