תרגול 11 מיון בזמן ליניארי מבני נתונים 152 תרגול 11 14.11.2018.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

Sorting in linear time (for students to read) Comparison sort: –Lower bound:  (nlgn). Non comparison sort: –Bucket sort, counting sort, radix sort –They.
Analysis of Algorithms CS 477/677 Linear Sorting Instructor: George Bebis ( Chapter 8 )
Sorting in Linear Time Comp 550, Spring Linear-time Sorting Depends on a key assumption: numbers to be sorted are integers in {0, 1, 2, …, k}. Input:
Non-Comparison Based Sorting
CSE 3101: Introduction to the Design and Analysis of Algorithms
MS 101: Algorithms Instructor Neelima Gupta
1 Sorting in Linear Time How can we do better?  CountingSort  RadixSort  BucketSort.
Sorting in linear time Comparison sort: –Lower bound:  (nlgn). Non comparison sort: –Bucket sort, counting sort, radix sort –They are possible in linear.
Mudasser Naseer 1 5/1/2015 CSC 201: Design and Analysis of Algorithms Lecture # 9 Linear-Time Sorting Continued.
Counting Sort Non-comparison sort. Precondition: n numbers in the range 1..k. Key ideas: For each x count the number C(x) of elements ≤ x Insert x at output.
Comp 122, Spring 2004 Keys into Buckets: Lower bounds, Linear-time sort, & Hashing.
Comp 122, Spring 2004 Lower Bounds & Sorting in Linear Time.
Analysis of Algorithms CS 477/677 Midterm Exam Review Instructor: George Bebis.
Lecture 5: Master Theorem and Linear Time Sorting
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 5 Linear-time sorting Can we do better than comparison sorting? Linear-time sorting.
David Luebke 1 7/2/2015 Linear-Time Sorting Algorithms.
Lower Bounds for Comparison-Based Sorting Algorithms (Ch. 8)
David Luebke 1 8/17/2015 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
Computer Algorithms Lecture 11 Sorting in Linear Time Ch. 8
Sorting in Linear Time Lower bound for comparison-based sorting
1 Sorting in O(N) time CS302 Data Structures Section 10.4.
David Luebke 1 10/13/2015 CS 332: Algorithms Linear-Time Sorting Algorithms.
CSC 41/513: Intro to Algorithms Linear-Time Sorting Algorithms.
Analysis of Algorithms CS 477/677
Fall 2015 Lecture 4: Sorting in linear time
Mudasser Naseer 1 11/5/2015 CSC 201: Design and Analysis of Algorithms Lecture # 8 Some Examples of Recursion Linear-Time Sorting Algorithms.
Survey of Sorting Ananda Gunawardena. Naïve sorting algorithms Bubble sort: scan for flips, until all are fixed Etc...
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.
COSC 3101A - Design and Analysis of Algorithms 6 Lower Bounds for Sorting Counting / Radix / Bucket Sort Many of these slides are taken from Monica Nicolescu,
1 Algorithms CSCI 235, Fall 2015 Lecture 17 Linear Sorting.
Foundations of Data Structures Practical Session #12 Linear Sorting.
Linear Sorting. Comparison based sorting Any sorting algorithm which is based on comparing the input elements has a lower bound of Proof, since there.
19 March More on Sorting CSE 2011 Winter 2011.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting So Far Insertion sort: –Easy to code –Fast on small inputs (less than ~50 elements) –Fast on nearly-sorted.
David Luebke 1 7/2/2016 CS 332: Algorithms Linear-Time Sorting: Review + Bucket Sort Medians and Order Statistics.
Lower Bounds & Sorting in Linear Time
Sorting.
Bucket-Sort and Radix-Sort
Linear-Time Sorting Continued Medians and Order Statistics
MCA 301: Design and Analysis of Algorithms
Algorithm Design and Analysis (ADA)
Bucket-Sort and Radix-Sort
Quick-Sort 11/14/2018 2:17 PM Chapter 4: Sorting    7 9
(2,4) Trees 11/15/2018 9:25 AM Sorting Lower Bound Sorting Lower Bound.
Sorting in linear time Idea: if we can assume there are only k possible values to sort, we have extra information about where each element might need.
Linear Sorting Sections 10.4
Quick-Sort 11/19/ :46 AM Chapter 4: Sorting    7 9
Keys into Buckets: Lower bounds, Linear-time sort, & Hashing
Ch8: Sorting in Linear Time Ming-Te Chi
Counting (Pigeon Hole)
Bucket-Sort and Radix-Sort
Sorting in linear time (for students to read)
Linear Sort "Our intuition about the future is linear. But the reality of information technology is exponential, and that makes a profound difference.
(2,4) Trees 12/4/2018 1:20 PM Sorting Lower Bound Sorting Lower Bound.
תרגול 11 מיון בזמן לינארי DS162-ps
Linear Sorting Sorting in O(n) Jeff Chastine.
Linear Sort "Our intuition about the future is linear. But the reality of information technology is exponential, and that makes a profound difference.
Noncomparison Based Sorting
Lower Bounds & Sorting in Linear Time
Linear Sorting Section 10.4
Linear-Time Sorting Algorithms
Quick-Sort 2/23/2019 1:48 AM Chapter 4: Sorting    7 9
Bucket-Sort and Radix-Sort
Algorithms CSCI 235, Spring 2019 Lecture 18 Linear Sorting
Algorithms Sorting.
Algorithm/Running Time Analysis
Sorting We have actually seen already two efficient ways to sort:
Bucket-Sort and Radix-Sort
Presentation transcript:

תרגול 11 מיון בזמן ליניארי מבני נתונים 152 תרגול 11 14.11.2018

Counting-Sort אלגוריתם מיון שאינו מבוסס על השוואות ותומך בחזרה של מפתחות. A הינו מערך הקלט באורך n. B הינו מערך הפלט ואורכו גם כן n. C הינו מערך עזר בגודל k. הנחה: מערך הקלט A מכיל ערכים שלמים חיוביים בטווח 1…𝑘 . מבני נתונים 152 תרגול 11 14.11.2018

Counting-Sort רעיון האלגוריתם: נספור כמה פעמים מופיע כל אחד מהאיברים. לפי הספירה נדע איך לסדר את האיברים. מבני נתונים 152 תרגול 11 14.11.2018

Counting-Sort for i ← 1 to k C[i] ← = 0 for j ← 1 to n C[A[j]] ← C[A[j]] + 1 // C[i] = the number of appearances of i in A. for i ← 2 to k C[i] ← C[i] + C[i-1] // C[i] = the number of elements in A that are ≤ i for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] - 1 return B מבני נתונים 152 תרגול 11 14.11.2018

Counting-Sort for i ← 1 to k C[i] ← = 0 A 1 3 2 C מבני נתונים 152 תרגול 11 14.11.2018

Counting-Sort for j ← 1 to n C[A[j]] ← C[A[j]] + 1 // C[i] = the number of appearances of i in A 1 3 2 C 3 1 2 מבני נתונים 152 תרגול 11 14.11.2018

Counting-Sort for i ← 2 to k C[i] ← C[i] + C[i-1] // C[i] = the number of elements in A that are ≤ i A 1 3 2 C 6 3 2 מבני נתונים 152 תרגול 11 14.11.2018

Counting-Sort for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 A 1 3 2 C 6 3 2 B מבני נתונים 152 תרגול 11 14.11.2018

Counting-Sort for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 j=6, A[6]=1, C[1]=2 A 1 3 2 C 6 3 2 B מבני נתונים 152 תרגול 11 14.11.2018

Counting-Sort for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 j=6, A[6]=1, C[1]=2 A 1 3 2 C 6 3 1 B 1 מבני נתונים 152 תרגול 11 14.11.2018

Counting-Sort for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 j=5, A[5]=3, C[3]=6 A 1 3 2 C 6 3 1 B 1 מבני נתונים 152 תרגול 11 14.11.2018

Counting-Sort for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 j=5, A[5]=3, C[3]=6 A 1 3 2 C 5 3 1 B 3 1 מבני נתונים 152 תרגול 11 14.11.2018

Counting-Sort for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 j=4, A[4]=1, C[1]=1 A 1 3 2 C 5 3 1 B 3 1 מבני נתונים 152 תרגול 11 14.11.2018

Counting-Sort for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 j=4, A[4]=1, C[1]=1 A 1 3 2 C 5 3 B 3 1 מבני נתונים 152 תרגול 11 14.11.2018

Counting-Sort for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 j=3, A[3]=3, C[3]=5 A 1 3 2 C 5 3 B 3 1 מבני נתונים 152 תרגול 11 14.11.2018

Counting-Sort for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 j=3, A[3]=3, C[3]=5 A 1 3 2 C 4 3 B 3 1 מבני נתונים 152 תרגול 11 14.11.2018

Counting-Sort for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 j=2, A[2]=2, C[2]=3 A 1 3 2 C 4 3 B 3 1 מבני נתונים 152 תרגול 11 14.11.2018

Counting-Sort for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 j=2, A[2]=2, C[2]=3 A 1 3 2 C 4 2 B 3 2 1 מבני נתונים 152 תרגול 11 14.11.2018

Counting-Sort for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 j=1, A[1]=3, C[3]=4 A 1 3 2 C 4 2 B 3 2 1 מבני נתונים 152 תרגול 11 14.11.2018

מה נעשה אם הטווח מכיל גם מספרים שליליים, לדוגמה [-k,k] ? Counting-Sort for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] – 1 j=1, A[1]=3, C[3]=4 מה נעשה אם הטווח מכיל גם מספרים שליליים, לדוגמה [-k,k] ? A 1 3 2 C 3 2 B 3 2 1 מבני נתונים 152 תרגול 11 14.11.2018

זמן ריצה -Counting sort for i ← 1 to k C[i] ← = 0 for j ← 1 to n C[A[j]] ← C[A[j]] + 1 // C[i] = the number of appearances of i in A. for i ← 2 to k C[i] ← C[i] + C[i-1] // C[i] = the number of elements in A that are ≤ i for j ← n downto 1 B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] - 1 return B O(k) O(n) O(k) O(n) Total = O(n+k) מבני נתונים 152 תרגול 11 14.11.2018

Question 1: Size of ranges using Counting-Sort נתונים n משתנים בטווח [1..k]. הציעו אלגוריתם שמבצע חישוב מקדים בזמן O(n+k), ולאחריו יודע להחזיר עבור כל a,b את כמות האיברים בטווח [a..b] בזמן O(1). מבני נתונים 152 תרגול 11 14.11.2018

Question 1: Size of ranges using Counting-Sort פתרון: נשמור את מערך העזר C של Counting Sort. C[i] = number of elements that are ≤i נחזיר את C[b]-C[a-1] Range(C,a,b) return(C[b]-C[a-1]) מבני נתונים 152 תרגול 11 14.11.2018

Radix Sort זהו אלגוריתם מעטפת שמשתמש במיון אחר בתוכו. נשתמש במיון יציב כלשהו, בדר"כ Counting sort נמיין ע"פ ספרות במפתח. נתחיל באחדות ונתקדם משם. d – כמות הספרות, b – הבסיס שבו מיוצג המפתח Radix-Sort(A,d) for i=1 to d sort A on digit i with Counting-Sort Running time: O(d(n+b))=O(n) מבני נתונים 152 תרגול 11 14.11.2018

(זה של ספרה יחידה) יהיה יציב? Radix Sort Example Sort the following numbers using Radix 329, 457, 657, 839, 436, 720, 355 למה חשוב שהמיון המשני (זה של ספרה יחידה) יהיה יציב? sorted by digit 1 sorted by digit 2 (and 1) sorted 329 457 657 839 436 720 355 720 355 436 457 657 329 839 720 355 436 457 657 329 839 720 329 436 839 355 457 657 720 329 436 839 355 457 657 329 355 436 457 657 720 839 ⇒ ⇒ ⇒ מבני נתונים 152 תרגול 11 14.11.2018

Question 5: Sorting lexicographically נניח שהמילים כולן באותיות קטנות המילים לא באותו האורך. הציעו אלגוריתם למיון המילים לפי סדר לקסיקוגרפי בזמן 𝑂 𝑛 . מבני נתונים 152 תרגול 11 14.11.2018

Question 5: Sorting lexicographically פתרון: רדוקציה: נניח שיש לנו שתי בעיות 𝑃 1 , 𝑃 2 . יש לנו אלגוריתם שפותר את 𝑃 2 . רדוקציה מבעיה 𝑃 1 לבעיה 𝑃 2 זוהי דרך לפתור את הבעיה 𝑃 1 בעזרת האלגוריתם לפתרון 𝑃 2 מבני נתונים 152 תרגול 11 14.11.2018

Question 5: Sorting lexicographically פתרון: במקרה שלנו הרדוקציה היא מבעיית המיון הלקסיקוגרפי לבעית radix-sort. הרדוקציה: תרגום הקלט של 𝑃 1 להיות קלט של 𝑃 2 : יהי m האורך של המילה המקסימלית בקלט. mהוא קבוע. כל מילה נייצג ע"י ספרה בבסיס 27. a=1,b=2,…z=26.סה"כ גודל הייצוג - 𝑂 𝑛𝑚 =𝑂 𝑛 . למילים עם k<m אותיות נוסיף אפסים. זאת כדי שכולם יהיו באותו גודל. נפתור את 𝑃 2 עם הקלט החדש: בצע radix-sort כאשר 𝑑=𝑚, 𝑏=27. זמן ריצה 𝑂 𝑛 . תרגם את הפתרון של 𝑃 2 חזרה ל 𝑃 1 שנה את המספרים חזרה לאותיות Total run time is O(n). מבני נתונים 152 תרגול 11 14.11.2018

Question 5: Sorting lexicographically Lexicographical-sort input: {blue, red, green} Radix-sort input: { (2,12, 21,5,0), (18,5,4,0,0), (7, 18,5,5,14) } Radix-sort output: { (2,12, 21,5,0), (7,8,5,5,14) , (18,5,4,0,0) } Lexicographical-sort output: {blue, green, red} מבני נתונים 152 תרגול 11 14.11.2018

… Bucket Sort הנחה: כל המפתחות מפוזרים באופן אחיד ב [0,1) הרעיון הכללי – נחלק לn מקטעים (דליים) את כל המפתחות, ואז נאחד את המקטעים … [0, 1 𝑛 ) [ 1 𝑛 , 2 𝑛 ) [ 2 𝑛 , 3 𝑛 ) [ 𝑛−1 𝑛 , 1 ) מבני נתונים 152 תרגול 11 14.11.2018

Bucket Sort נחשוב על זה לרגע בעולם פשוט יותר 0…99 . נחשוב על זה לרגע בעולם פשוט יותר 0…99 . יהיו המספרים 21,55,44,73,86,34,86,92,18,8 0…9 10…19 20…29 30…39 40…49 50…59 60…69 70…79 80…89 90…99 מבני נתונים 152 תרגול 11 14.11.2018

Bucket Sort נחשוב על זה לרגע בעולם פשוט יותר 0…99 . נחשוב על זה לרגע בעולם פשוט יותר 0…99 . יהיו המספרים 21,55,44,73,86,34,86,92,18,8 21 0…9 10…19 20…29 30…39 40…49 50…59 60…69 70…79 80…89 90…99 מבני נתונים 152 תרגול 11 14.11.2018

Bucket Sort נחשוב על זה לרגע בעולם פשוט יותר 0…99 . נחשוב על זה לרגע בעולם פשוט יותר 0…99 . יהיו המספרים 21,55,44,73,86,34,86,92,18,8 55 0…9 10…19 20…29 30…39 40…49 50…59 60…69 70…79 80…89 90…99 21 מבני נתונים 152 תרגול 11 14.11.2018

Bucket Sort נחשוב על זה לרגע בעולם פשוט יותר 0…99 . נחשוב על זה לרגע בעולם פשוט יותר 0…99 . יהיו המספרים 21,55,44,73,86,34,86,92,18,8 44 0…9 10…19 20…29 30…39 40…49 50…59 60…69 70…79 80…89 90…99 21 55 מבני נתונים 152 תרגול 11 14.11.2018

Bucket Sort נחשוב על זה לרגע בעולם פשוט יותר 0…99 . נחשוב על זה לרגע בעולם פשוט יותר 0…99 . יהיו המספרים 21,55,44,73,86,34,86,92,18,8 73 0…9 10…19 20…29 30…39 40…49 50…59 60…69 70…79 80…89 90…99 21 44 55 מבני נתונים 152 תרגול 11 14.11.2018

Bucket Sort נחשוב על זה לרגע בעולם פשוט יותר 0…99 . נחשוב על זה לרגע בעולם פשוט יותר 0…99 . יהיו המספרים 21,55,44,73,86,34,86,92,18,8 86 0…9 10…19 20…29 30…39 40…49 50…59 60…69 70…79 80…89 90…99 21 34 44 55 73 86 מבני נתונים 152 תרגול 11 14.11.2018

Bucket Sort נחשוב על זה לרגע בעולם פשוט יותר 0…99 . נחשוב על זה לרגע בעולם פשוט יותר 0…99 . יהיו המספרים 21,55,44,73,86,34,86,92,18,8 0…9 10…19 20…29 30…39 40…49 50…59 60…69 70…79 80…89 90…99 18 21 34 44 55 73 86 92 8 86 מבני נתונים 152 תרגול 11 14.11.2018

Bucket Sort O(n) O(n) Total = O(n) Pseudo-code: n = length(A) for i=1 to n 𝐵 𝑛𝐴 𝑖 =𝐴[𝑖] sort B[i] with insertion sort Concatenate the lists in order O(n) O(n) O(1)? Total = O(n) מבני נתונים 152 תרגול 11 14.11.2018

Bucket Sort Example 0.05, 0.12, 0.29, 0.07, 0.21, 0.06, 0.78, 0.91, 0.94, 0.81 for i=1 to n 𝐵 𝑛𝐴 𝑖 =𝐴[𝑖] [0,0.1) / [0.1,0.2) / [0.2,0.3) / [0.3,0.4) / [0.4,0.5) / [0.5,0.6) / [0.6,0.7) / [0.7,0.8) / [0.8,0.9) / [0.9,1) / מבני נתונים 152 תרגול 11 14.11.2018

Bucket Sort Example 0.05, 0.12, 0.29, 0.07, 0.21, 0.06, 0.78, 0.91, 0.94, 0.81 for i=1 to n 𝐵 𝑛𝐴 𝑖 =𝐴[𝑖] [0,0.1) 0.05 / [0.1,0.2) / [0.2,0.3) / [0.3,0.4) / [0.4,0.5) / [0.5,0.6) / [0.6,0.7) / [0.7,0.8) / [0.8,0.9) / [0.9,1) / מבני נתונים 152 תרגול 11 14.11.2018

Bucket Sort Example 0.05, 0.12, 0.29, 0.07, 0.21, 0.06, 0.78, 0.91, 0.94, 0.81 for i=1 to n 𝐵 𝑛𝐴 𝑖 =𝐴[𝑖] [0,0.1) 0.05 / [0.1,0.2) 0.12 / [0.2,0.3) / [0.3,0.4) / [0.4,0.5) / [0.5,0.6) / [0.6,0.7) / [0.7,0.8) / [0.8,0.9) / [0.9,1) / מבני נתונים 152 תרגול 11 14.11.2018

Bucket Sort Example 0.05, 0.12, 0.29, 0.07, 0.21, 0.06, 0.78, 0.91, 0.94, 0.81 for i=1 to n 𝐵 𝑛𝐴 𝑖 =𝐴[𝑖] [0,0.1) 0.05 / [0.1,0.2) 0.12 / [0.2,0.3) 0.29 / [0.3,0.4) / [0.4,0.5) / [0.5,0.6) / [0.6,0.7) / [0.7,0.8) / [0.8,0.9) / [0.9,1) / מבני נתונים 152 תרגול 11 14.11.2018

Bucket Sort Example 0.05, 0.12, 0.29, 0.07, 0.21, 0.06, 0.78, 0.91, 0.94, 0.81 for i=1 to n 𝐵 𝑛𝐴 𝑖 =𝐴[𝑖] [0,0.1) 0.07 / 0.05 [0.1,0.2) 0.12 / [0.2,0.3) 0.29 / [0.3,0.4) / [0.4,0.5) / [0.5,0.6) / [0.6,0.7) / [0.7,0.8) / [0.8,0.9) / [0.9,1) / מבני נתונים 152 תרגול 11 14.11.2018

Bucket Sort Example 0.05, 0.12, 0.29, 0.07, 0.21, 0.06, 0.78, 0.91, 0.94, 0.81 for i=1 to n 𝐵 𝑛𝐴 𝑖 =𝐴[𝑖] [0,0.1) 0.07 0.05 / [0.1,0.2) 0.12 / [0.2,0.3) 0.21 / 0.29 [0.3,0.4) / [0.4,0.5) / [0.5,0.6) / [0.6,0.7) / [0.7,0.8) / [0.8,0.9) / [0.9,1) / מבני נתונים 152 תרגול 11 14.11.2018

Bucket Sort Example 0.05, 0.12, 0.29, 0.07, 0.21, 0.06, 0.78, 0.91, 0.94, 0.81 for i=1 to n 𝐵 𝑛𝐴 𝑖 =𝐴[𝑖] [0,0.1) 0.06 0.07 / 0.05 [0.1,0.2) 0.12 / [0.2,0.3) 0.21 0.29 / [0.3,0.4) / [0.4,0.5) / [0.5,0.6) / [0.6,0.7) / [0.7,0.8) / [0.8,0.9) / [0.9,1) / מבני נתונים 152 תרגול 11 14.11.2018

Bucket Sort Example 0.05, 0.12, 0.29, 0.07, 0.21, 0.06, 0.78, 0.91, 0.94, 0.81 for i=1 to n 𝐵 𝑛𝐴 𝑖 =𝐴[𝑖] [0,0.1) 0.06 0.07 0.05 / [0.1,0.2) 0.12 / [0.2,0.3) 0.21 0.29 / [0.3,0.4) / [0.4,0.5) / [0.5,0.6) / [0.6,0.7) / [0.7,0.8) 0.78 / [0.8,0.9) / [0.9,1) / מבני נתונים 152 תרגול 11 14.11.2018

Bucket Sort Example 0.05, 0.12, 0.29, 0.07, 0.21, 0.06, 0.78, 0.91, 0.94, 0.81 for i=1 to n 𝐵 𝑛𝐴 𝑖 =𝐴[𝑖] [0,0.1) 0.06 0.07 0.05 / [0.1,0.2) 0.12 / [0.2,0.3) 0.21 0.29 / [0.3,0.4) / [0.4,0.5) / [0.5,0.6) / [0.6,0.7) / [0.7,0.8) 0.78 / [0.8,0.9) / [0.9,1) 0.91 / מבני נתונים 152 תרגול 11 14.11.2018

Bucket Sort Example 0.05, 0.12, 0.29, 0.07, 0.21, 0.06, 0.78, 0.91, 0.94, 0.81 for i=1 to n 𝐵 𝑛𝐴 𝑖 =𝐴[𝑖] [0,0.1) 0.06 0.07 0.05 / [0.1,0.2) 0.12 / [0.2,0.3) 0.21 0.29 / [0.3,0.4) / [0.4,0.5) / [0.5,0.6) / [0.6,0.7) / [0.7,0.8) 0.78 / [0.8,0.9) / [0.9,1) 0.94 / 0.91 מבני נתונים 152 תרגול 11 14.11.2018

Bucket Sort Example 0.05, 0.12, 0.29, 0.07, 0.21, 0.06, 0.78, 0.91, 0.94, 0.81 for i=1 to n 𝐵 𝑛𝐴 𝑖 =𝐴[𝑖] [0,0.1) 0.06 0.07 0.05 / [0.1,0.2) 0.12 / [0.2,0.3) 0.21 0.29 / [0.3,0.4) / [0.4,0.5) / [0.5,0.6) / [0.6,0.7) / [0.7,0.8) 0.78 / [0.8,0.9) 0.81 / [0.9,1) 0.94 / 0.91 מבני נתונים 152 תרגול 11 14.11.2018

Bucket Sort Example 0.05, 0.12, 0.29, 0.07, 0.21, 0.06, 0.78, 0.91, 0.94, 0.81 for i=1 to n sort B[i] with insertion sort [0,0.1) 0.06 0.07 0.05 / [0.1,0.2) 0.12 / [0.2,0.3) 0.21 0.29 / [0.3,0.4) / [0.4,0.5) / [0.5,0.6) / [0.6,0.7) / [0.7,0.8) 0.78 / [0.8,0.9) 0.81 / [0.9,1) 0.94 / 0.91 מבני נתונים 152 תרגול 11 14.11.2018

Bucket Sort Example 0.05, 0.12, 0.29, 0.07, 0.21, 0.06, 0.78, 0.91, 0.94, 0.81 for i=1 to n sort B[i] with insertion sort [0,0.1) 0.05 0.06 0.07 / [0.1,0.2) 0.12 / [0.2,0.3) 0.21 0.29 / [0.3,0.4) / [0.4,0.5) / [0.5,0.6) / [0.6,0.7) / [0.7,0.8) 0.78 / [0.8,0.9) 0.81 / [0.9,1) 0.94 / 0.91 מבני נתונים 152 תרגול 11 14.11.2018

Bucket Sort Example 0.05, 0.12, 0.29, 0.07, 0.21, 0.06, 0.78, 0.91, 0.94, 0.81 for i=1 to n sort B[i] with insertion sort [0,0.1) 0.05 0.06 0.07 / [0.1,0.2) 0.12 / [0.2,0.3) 0.21 0.29 / [0.3,0.4) / [0.4,0.5) / [0.5,0.6) / [0.6,0.7) / [0.7,0.8) 0.78 / [0.8,0.9) 0.81 / [0.9,1) 0.91 / 0.94 מבני נתונים 152 תרגול 11 14.11.2018

Bucket Sort Example 0.05, 0.12, 0.29, 0.07, 0.21, 0.06, 0.78, 0.91, 0.94, 0.81 Concatenate the lists in order [0,0.1) 0.05 0.06 0.07 / [0.1,0.2) 0.12 / [0.2,0.3) 0.21 0.29 / [0.3,0.4) / [0.4,0.5) / [0.5,0.6) / [0.6,0.7) / [0.7,0.8) 0.78 / [0.8,0.9) 0.81 / [0.9,1) 0.91 / 0.94 מבני נתונים 152 תרגול 11 14.11.2018

Question 2 : An improved Bucket-Sort algorithm תכנן אלגוריתם למיון n מספרים בטווח [x,x+d) אשר: רץ בזמן ממוצע 𝑂 𝑛 אם המספרים מתפלגים אחיד בטווח. רץ בזמן 𝑂 𝑛 log 𝑛 במקרה של ההתפלגות גרועה ביותר. מבני נתונים 152 תרגול 11 14.11.2018

Question 2 : An improved Bucket-Sort algorithm פתרון: נשתמש באלגוריתם bucket sort המקורי בטווח 𝑥,𝑥+𝑑 𝑥,𝑥+𝑑 וכאשר נמיין כל תא, נמיינו ע"י מיון merge sort. ניתוח זמן ריצה בהתפלגות טובה (אחידה), בכל התאים יהיה איבר אחד. בהתפלגות הרעה, כל האיברים יהיו בתא אחד ונמיין זאת ב𝑂(𝑛 log⁡𝑛). מבני נתונים 152 תרגול 11 14.11.2018

Question 2 : An improved Bucket-Sort algorithm פתרון נוסף: נשתמש באלגוריתם bucket sort המקורי בטווח 𝑥,𝑥+𝑑 עם השינויים הבאים: האיברים בכל דלי יסודרו בעץ AVL במקום ברשימה. בחלק האחרון נשרשר את כל התאים לפי סדר inorder של העצים. הערה: פונקצית החלוקה לתאים היא: מבני נתונים 152 תרגול 11 14.11.2018

Question 2 : An improved Bucket-Sort algorithm Time Complexity: Let ni be the number of elements in the tree in bucket i. Inserting the n elements into the buckets takes O(n1logn1 + n2logn2 + ... + nnlognn) When the keys are uniformly distributed ni = O(1) for every i, hence O(n1logn1 + n2logn2 + ... + nnlognn) ≤ c(n1 + n2 + ... + nn) = cn, where c is a constant. In the worst distribution cases: O(n1logn1 + n2logn2 + ... + nnlognn) ≤ O(n1logn + n2logn + ... + nnlogn) = O((n1 + n2 + ... + nn )(logn)) = O(nlogn) Inorder traversals of all buckets takes O(n1 + n2 + ... + nn) = O(n) Concatenation of all inorder traversal lists takes O(n) The algorithm runs in O(n) time for uniformly distributed keys and runs in O(nlogn) in the worst distribution case. מבני נתונים 152 תרגול 11 14.11.2018

Question 2 : An improved Bucket-Sort algorithm פתרון נוסף: בצע במקביל את שני האלגוריתמים הבאים: מיון bucket sort רגיל כל מיון השוואות בזמן 𝑂 𝑛 log 𝑛 . עצור כאשר אחד האלגוריתמים עוצר והחזר את האיברים ממויינים. מבני נתונים 152 תרגול 11 14.11.2018

Review מבני נתונים 152 תרגול 11 14.11.2018

Question 3: Choosing sorting algorithm efficiently בהינתן מערך בגודל n של מספרים שלמים בטווח [1,n­3] הציעו אלגוריתם יעיל למיון האיברים. מה עם מיון השוואות? מה עם counting sort? משהו אחר? מבני נתונים 152 תרגול 11 14.11.2018

Question 3: Choosing sorting algorithm efficiently מיון מבוסס השוואות: זמן ריצה 𝑂 𝑛𝑙𝑜𝑔 𝑛 . Counting sort: k= n3 ⇒ O(n+ n3)=O(n3) . Radix sort: b=n, d =4, ⇒ O(d(b+n))=O(4(n+n))=O(n) אפשר יותר טוב אפילו גרוע יותר  מבני נתונים 152 תרגול 11 14.11.2018

Question 3: Choosing sorting algorithm efficiently פתרון: Radix sort: b=n, d =4, ⇒ O(d(b+n))=O(4 (n+n))=O(n) רעיון: לפני הפעלת radix sort ניצור קלט המתאים למיון. המר את כל המספרים לבסיס 𝑛 , זה מתבצע בזמן 𝑂 𝑛 . לצורך כך משתמשים בפונ' div,mod בצורה הבאה: x = [x3,x2,x1,x0] (x0= x mod n, xi = 𝑥 𝑛 𝑖 mod n) בצע מיון radix sort על הקלט החדש - 𝑂 𝑛 . כל המספרים הם בטווח 1 עד 𝑛 3 . לכן בבסיס החדש יש לכל היותר 4 ספרות לכל מספר. נניח 𝑛=2 אז המספרים בטווח 1 עד 8 מיוצגים ע"י 0001,0010,…,1000. סה"כ זמן ריצה האלגוריתם d=4, b=n ⇒ O(4(n+n))=O(n) . מבני נתונים 152 תרגול 11 14.11.2018

Question 4: Sorting a collection of sets efficiently נתונות m קבוצות S1, S2,...,Sm. כל אחת מהקבוצות מכילה מספרים שלמים בטווח [1..n] ומתקיים כי 𝑚=𝑜 𝑛 . ni = |Si| = number of elements in Si. n1 + n2 + ... + nm = O(n) הציעו אלגוריתם למיון כל הקבוצות S1, S2,...,Sm בסיבוכיות זמן ומקום של 𝑂 𝑛 . הערה: הפלט הוא m קבוצות ממויינות ולא קבוצה אחת מאוחדת. מבני נתונים 152 תרגול 11 14.11.2018

Question 4: Sorting a collection of sets efficiently פתרונות נאיביים לא עובדים: אם נמיין כל קבוצה ב𝑂 𝑛 log 𝑛 יביא לזמן ריצה של T(n) = O(n1logn1 + n2logn2 + ... + nmlognm) ≈O(nlogn). אם נמיין כל קבוצה בנפרד ע"י counting sort: T(n) = O((n1+n) + (n2+n) + ... + (nm+n)) = O(n+mn) = O(n2). סיבוכיות מקום של 𝑂 𝑛 . מבני נתונים 152 תרגול 11 14.11.2018

Question 4: Sorting a collection of sets efficiently פתרון: צרף שדה חדש לכל איבר -set-num . בנה מערך A עם כל האיברים מכל הקבוצות. מיין את A בעזרת אלגוריתם 𝑐𝑜𝑢𝑛𝑡𝑖𝑛𝑔 𝑠𝑜𝑟𝑡. פצל את המערך A ל k קבוצות לפי השדה שהוספנו - נעשה זאת ע"י counting sort לפי set-num Total running time: O(n) O(n) O(n) O(n) O(n) מבני נתונים 152 תרגול 11 14.11.2018

Question 6: Sorting input with anomalies מבני נתונים 152 תרגול 11 14.11.2018

Question 6: Sorting input with anomalies פתרון: סרוק את המערך A והוצא את עשרת המספרים שאינם בטווח לתוך מערך B. בצע counting sort על המערך A בצע מיון השוואות כלשהו על B מזג את A ו- B. סה"כ זמן ריצה- 𝑂(𝑛) O(n) O( (n-10) +10n)=O(n) O(1) O(n) מבני נתונים 152 תרגול 11 14.11.2018