Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons...

117
Sorting II: לללל ללללל
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    223
  • download

    3

Transcript of Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons...

Page 1: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Sorting II:

הפרד ומשול

Page 2: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Last week: in-place sorting• Bubble Sort – O(n2) comparisons

– O(n) best case comparisons, O(n2) exchanges

• Selection Sort - O(n2) comparisons– O(n2) best case comparisons– O(n) exchanges (always)

• Insertion Sort – O(n2) comparisons– O(n) best case comparisons– Fewer exchanges than bubble sort– Best in practice for small lists (<30)

Page 3: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

This week

• Mergesort– O(n log n) always– O(n) storage

• Quick sort– O(n log n) average, O(n^2) worst – Good in practice (>30), O(log n) storage

Page 4: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

MergeSort

• A divide-and-conquer technique• Each unsorted collection is split into 2

– Then again• Then again

– Then again

» ……. Until we have collections of size 1» Now we merge sorted collections

– Then again

• Then again

– Then again

• Until we merge the two halves

Page 5: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

MS(N)

Merge sort analysis= N

= N/2 + N/2MS(N/2) MS(N/2)+

N/4 N/4 N/4 N/4 = 4*N/4+

N/8 N/8 N/8 N/8 N/8 N/8 N/8 N/8+

= 8*N/8

Each level contributes N

...

Page 6: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

MS(N)

Merge sort analysis

MS(N/2) MS(N/2)

N/4 N/4 N/4 N/4

N/8 N/8 N/8 N/8 N/8 N/8 N/8 N/8

N/2K = 1 N = 2K

lg N = K

log n levels * n per level= O( nlog(n) )

K levels

N/2K

Page 7: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

MergeSort(array a, indexes low, high)

1. If (low < high)

2. middle (low + high) /2

3. MergeSort(a,low,middle) // split 1

4. MergeSort(a,middle+1,high) // split 2

5. Merge(a,low,middle,high) // merge 1+2

Page 8: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Merge(arrays a, index low, mid, high)

1. bempty array, tmid+1, ilow, tllow

2. while (tl<=mid AND t<=high)

3. if (a[tl]<=a[t])

4. b[i]a[tl]

5. ii+1, tltl+1

6. else

7. b[i]a[t]

8. ii+1, tt+1

9. if tl<=mid copy a[tl…mid] into b[i…]

10.else if t<=high copy a[t…high] into b[i…]

11.copy b[low…high] onto a[low…high]

Page 9: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא

Initial: 25 57 48 37 12 92 86 33

Split: 25 57 48 37 12 92 86 33

Split: 25 57 48 37 12 92 86 33

Split: 25 57 48 37 12 92 86 33

Merge: 25 57 37 48 12 92 33 86

Merge: 25 37 48 57 12 33 86 92

Merge: 12 25 33 37 48 57 86 92

Page 10: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

The complexity of MergeSort

• Every split, we half the collection• How many times can this be done?

We are looking for x, where 2x = n

x = log2 n

• So there are a total of log n splits

Page 11: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

The complexity of MergeSort

• Each merge is of what run-time?

• First merge step: n/2 merges of 2 n

• Second merge step: n/4 merges of 4 n

• Third merge step: n/8 merges of 8 n

• ….

• How many merge steps? Same as splits log n

Total: n log n steps

Page 12: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Storage complexity of MergeSort

Every merge, we need to hold the merged array:

1 2 3 4 5 6

1 2 3 4 5 6

1 2 3 4

1 2 3 4 5 6

Page 13: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Storage complexity of MergeSort

• So we need temporary storage for merging– Which is the same size as the two collections

together

To merge the last two sub-arrays (each size n/2)

We need n/2+n/2 = n temporary storage

• Total: O(n) storage

Page 14: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

QuickSort

Key idea:

• Select a item (called the pivot)

• Put it into its proper FINAL position

• Make sure:– All greater item are on one side (side 1)– All smaller item are on other side (side 2)

• Repeat for side 1

• Repeat for side 2

Page 15: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Short example

25 57 48 37 12 92 86 33• Let’s select 25 as our initial pivot. • We move items such that:

– All left of 25 are smaller– All right of 25 are larger– As a result 25 is now in its final position

12 25 57 48 37 92 86 33

Page 16: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

• Now, repeat (recursively) for left and right sides

12 25 57 48 37 92 86 33– Sort 12– Sort 57 48 37 92 86 33

• 12 needs no sorting• For the other side, we repeat the process

– Select a pivot item (let’s take 57)– Move items around such that left items are smaller,

etc.

Page 17: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

12 25 57 48 37 92 86 33

Changes into

12 25 48 37 33 57 92 86

And now we repeat the process for left

12 25 37 33 48 57 92 86

12 25 33 37 48 57 92 86

12 25 33 37 48 57 92 86

And for the right

12 25 33 37 48 57 86 92

12 25 33 37 48 57 86 92

Page 18: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

QuickSort(array a; index low, hi)

1. if (low >= hi)

2. return ; // a[low..hi] is sorted

3. pivotfind_pivot(a,low,hi)

4. p_index=partition(a,low,high,pivot)

5. QuickSort(a,low,p_index-1)

6. QuickSort(a,p_index+1,hi)

Page 19: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Key questions

How do we select an item (FindPivot())?• If we always select the largest item as the pivot

– Then this process becomes Selection Sort– Which is O(n2)

• So this works only if we select items “in the middle”– Since then we will have log n divisions

How do we move items around efficiently (Partition()?)• This offsets the benefit of partitioning

Page 20: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

FindPivot

• To find a real median (middle item) takes O(n)• In practice however, we want this to be O(1)• So we approximate:

– Take the first item (a[low]) as the pivot– Take the median of {a[low],a[hi],a[(low+hi)/2]}

FindPivot(array a; index low, high)

1. return a[low]

Page 21: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Partition (in O(n))

Key idea:

• Keep two indexes into the array

– up points at lowest item >= pivot

– down points at highest item <= pivot

• We move up, down in the array

• Whenever they point inconsistently, interchange

At end:

• up and down meet in location of pivot

Page 22: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

partition(array a; index low,hi; pivot; index pivot_i)

1. downlow, uphi

2. while(down<up)

3. while (a[down]<=pivot && down<hi)

4. downdown + 1

5. while (a[hi]>pivot)

6. upup – 1

7. if (down < up)

8. swap(a[down],a[up])

9. a[pivot_i]=a[up]

10.a[up] = pivot

11.return up

Page 23: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Example: partition() with pivot=25

First pass through loop on line 2:

25 57 48 37 12 92 86 33

down up

Page 24: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Example: partition() with pivot=25

First pass through loop on line 2:

25 57 48 37 12 92 86 33

down up

We go into loop in line 3 (while a[down]<=pivot)

Page 25: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Example: partition() with pivot=25

First pass through loop on line 2:

25 57 48 37 12 92 86 33

down up

We go into loop in line 5 (while a[up]>pivot)

Page 26: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Example: partition() with pivot=25

First pass through loop on line 2:

25 57 48 37 12 92 86 33

down up

We go into loop in line 5 (while a[up]>pivot)

Page 27: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Example: partition() with pivot=25

First pass through loop on line 2:

25 57 48 37 12 92 86 33

down up

Now we found an inconsistency!

Page 28: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Example: partition() with pivot=25

First pass through loop on line 2:

25 12 48 37 57 92 86 33

down up

So we swap a[down] with a[up]

Page 29: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Example: partition() with pivot=25

Second pass through loop on line 2:

25 12 48 37 57 92 86 33

down up

Page 30: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Example: partition() with pivot=25

Second pass through loop on line 2:

25 12 48 37 57 92 86 33

down up

Move down again (increasing) – loop on line 3

Page 31: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Example: partition() with pivot=25

Second pass through loop on line 2:

25 12 48 37 57 92 86 33

down up

Now we begin to move up again – loop on line 5

Page 32: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Example: partition() with pivot=25

Second pass through loop on line 2:

25 12 48 37 57 92 86 33

down up

Again – loop on line 5

Page 33: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Example: partition() with pivot=25

Second pass through loop on line 2:

25 12 48 37 57 92 86 33

down up

down < up? No. So we don’t swap.

Page 34: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Example: partition() with pivot=25

Second pass through loop on line 2:

25 12 48 37 57 92 86 33

down up

Instead, we are done. Just put pivot in place.

Page 35: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Example: partition() with pivot=25

Second pass through loop on line 2:

12 25 48 37 57 92 86 33

down up

Instead, we are done. Just put pivot in place.

(swap it with a[up] – for us a[low] was the pivot)

Page 36: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Example: partition() with pivot=25

Second pass through loop on line 2:

12 25 48 37 57 92 86 33

down up

Now we return 2 as the new pivot index

Page 37: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Notes• We need the initial pivot_index in partition()• For instance, change FindPivot():

– return pivot (a[low]), as well as initial pivot_index (low)

– Then use pivot_index in the final swap

• QuickSort: Average O(n log n), Worst case O(n2)– works very well in practice (collections >30)

– Average O(n log n), Worst case O(n2)

– Space requirements O(log n) – for recursion

Page 38: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

O(nlognממוצע (: סבוכיות

O(n2גרוע ביותר (

במקוםA[j],…., A[iמיון של האיברים [

שיטה:

)A[j],…..,A[i (אחד מערכי [V) בחר פיבוט, 1

כך ש: k) מצא (וארגן) נקודה 2

A[j],…..,A[k מופיעים ב- [V הערכים הקטנים מ-

A[k+1],…..,A[i מופיעים ב- [V והגדולים שווים ל-

רקורסיביתA[k+1],…..,A[i ו- A[j],…..,A[k]) מיין את [3

דוגמא לפיבוט:

האיבר הגדול ביותר בין השניים השונים זה מזה משמאל

QUICKSORT

Page 39: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

האלגוריתם מורכב מ:

FindPivot) מציאת פיבוט 1

Partition) חלוקה 2

) רקורסיה3

לך משמאל לימין ומצא את הגדול הראשון מבין .L, Rהשתמש בשני סמנים השניים

Rמתחיל מימין וזז שמאלה

Lמתחיל משמאל וזז ימינה

Pivot שמאלה כל עוד הערכים גדולים שווים מ R. הסע 1

Pivot ימינה כל עוד הערכים קטנים מ L. הסע 2

עצור.L>R. אם 3

R ו- L אחרת: החלף בין האיברים של

שמאלהRהזז

ימינהLהזז

. 1חזור ל

(R = L - 1)!!!!בדיקה

Page 40: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

פרט לקריאה הרקורסיבית, הכל לינארי במספר - QuickSortב) האלמנטים

סכם על כל האלמנטים את מספר הפעמים שמשתתפים זמן כולל: בקריאה

(כולם) סכום העומקים של האלמנטים=

ניתוח סיבוכיות:O(j - i + 1 לוקחת Partition(i, j)א) נראה ש (

- התחשבנות עם כל איבר- לכל איבר מתבצע:

א) הגעה עם המצביע ב) אחד מהשניים:

O(1) ממשיכים עם מצביע- (1 . ממתינים1) 2

O(1 (. מחליפים2 . מקדמים מצביע3

- לא חוזרים לאיבר

O(j - i + 1 היא Partition(i, j) סיבוכיות (מסקנה:

Page 41: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Worst Case ג)

יתחלק nאם כל פעם נבחר לפיבוט את הגדול ביותר אז המערך בגודל n -1 + מערך של 1למערך של

n

n - 1

n - 2

2

n -

1

סכום העבודה בכל הרמות (עבודה לינארית באלמנטים)= סכום העומקים

= n - 1 + (1 + 2 + …… + n - 1) =O(n2) =

12

)1(

nn

Page 42: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

ערימה/תור עדיפויות

מוטיבציהשונים מסוגים משימות הסמסטר במשך מקבל סטודנט כאשר יום, כל מתעדכנת הרשימה שונים. ובתאריכים בית תרגילי הם המשימות סוגי חדשות. משימות מתקבלות אחת לכל ומבחנים. מהגשה חופשיים בית תרגילי להגשה,

מהמשימות יש תאריך יעד מוגדר, שלא תלוי ביתר המשימות.

עדיפויות של מסודרת רשימה לעצמו לערוך רוצה הסטודנט לבצע לו כדאי משימה איזה את פעם כל שידע כך לעבודות,

עכשיו כדי לעמוד בזמנים של כל המשימות.

Page 43: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דרישות ממבנה הנתונים

מציאה מהירה של ראש סדר העדיפויות•

הכנסה יעילה של איבר (משימה) חדש•

את • מוציאים כאשר יעיל באופן המבנה ארגון (לאחר מהמבנה העדיפויות סדר ראש

מהמשימה בוצעה)

Page 44: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

"פעולות של מבנה הנתונים "ערימה

)make_heap(Q)צור ערימה ריקה (• insert ) לערימה xהכנס רשומה בעלת מפתח •

(Q,x)(הדפס את הרשומה בעלת המפתח הגדול ביותר •

)max(Q)בערימה (הוצא את הרשומה בעלת המפתח הגדול ביותר •

)del_max(Q)בערימה(הדפס את כל הרשומות בסדר יורד •

)heap_sort(Q)(

Page 45: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

הגדרה: עץ בינארי שלם

עץ בינארי הוא עץ, שלכל צומת שלו יש לכל היותר •שני בנים, ולכל צומת מלבד השורש יש בדיוק אבא

אחד.עץ בינארי מלא הוא עץ שבו לכל צומת מלבד העלים •

בנים.2יש בדיוק עץ בינארי שלם הוא עץ בינארי מלא שבו כל העלים •

הם בעלי אותו עומק. עץ בינארי כמעט שלם הוא עץ בינארי שלם שהוצאו •

ממנו עלים "מימין".

Page 46: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמאות לעצים בינאריים

עץ בינארי

עץ בינארי כמעט שלם

עץ בינארי מלא

עץ בינארי שלם

Page 47: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

מימוש ערימה בעזרת עץ בינארי כמעט שלם

החוק הבסיסי של ערימת מקסימום:

המפתח של כל הורה הוא גדול או שווה למפתחות של בניו

הרשומה בעלת המפתח הגדול ביותר יושבת תמיד בשורש העץ

Page 48: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא לערימת מקסימום

17

11

17

15

12

910

6 2 8 9

כל מסלול בעץ היוצא מהשורש הוא מסלול לא

עולה

השורש הוא המפתח

המקסימלי בעץ

Page 49: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

כל מערך יכול לייצג עץ בינארי כמעט מלא

כל איבר במערך מייצג צומת בעץ• במערך, בניו בעץ יושבים במקומות iלכל איבר •

2i – 2 וi+1 .במערך ]i/2[ במערך הוא האיבר ה – iאביו של האיבר ה - •שורש העץ הוא האיבר הראשון במערך•עלי העץ הם האיברים שבניהם לא נכללים •

בתחום המערך

Page 50: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא להשמת עץ בינארי כמעט שלם בתוך מערך

17

11

17

15

12

910

6 2 8 9

17 11 17 10 9 12 15 6 2 8 9

1 2 3 4 5 6 7 8 9 10 11

כל מערך יכול לייצג עץ בינארי כמעט

שלם,

וכל עץ בינארי כמעט שלם יכול להיות מיוצג

בעזרת מערך

Page 51: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

פונקצית עזר למימוש פעולות הערימה

חוקיות הערימה מחייבת שהשורש תמיד יכיל את הרשומה בעלת המפתח המקסימלי בעץ.

אחת הפעולות המותרות למבנה הנתונים ערימה הוא הוצאת האיבר המקסימלי מהערימה.

דרושה טכניקה יעילה שתחזיר את הערימה למצב תקין

Page 52: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Shift_down(v)פעולת

קלט: ערימה שהשורש שלה לא מקיים את תכונת הערימה•פלט: ערימה תקנית•

Shift_down(v)פעולת •

גדול משני בניו, סייםv עלה , או vאם 1.

עם הערך של הגדול משני בניו (בסימון vהחלף את ערכו של 2.w(

.wחזור על הפעולה עם הערימה ששורשה הוא 3.

Page 53: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Shift_downדוגמא לפעולת

8

17

16

15

12

910

6 2 8 9

קלט: עץ בו השורש לא מקיים את כללי הערימה

Page 54: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Shift_downדוגמא לפעולת

17

8 16

15

12

910

6 2 8 9

Page 55: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Shift_downדוגמא לפעולת

17

10

16

15

12

98

6 2 8 9

Page 56: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

17

10

16

15

12

98

6 2 8 9

Page 57: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

17

10

16

15

12

98

6 2 8 9

פלט: ערימה תקינה

Page 58: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Aבניית ערימה ממערך נתון כלשהו

•Make_heap(A)עבור על כל איברי המערך מימין לשמאל1..Shift_downעבור כל אחד מהם בצע 2.

Page 59: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא ליצירת ערימה

6 8 12 5 8 23 10 63 17 4 9

1 2 3 4 5 6 7 8 9 10 11

6

8 12

10

23

85

63

17

4 9

קלט: מערך כלשהו

Page 60: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא ליצירת ערימה - המשך

6 8 12 5 8 23 10 63 17 4 9

1 2 3 4 5 6 7 8 9 10 11

Page 61: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא ליצירת ערימה - המשך

6 8 12 5 8 23 10 63 17 4 9

1 2 3 4 5 6 7 8 9 10 11

Page 62: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא ליצירת ערימה - המשך

6 8 12 5 8 23 10 63 17 4 9

1 2 3 4 5 6 7 8 9 10 11

Page 63: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא ליצירת ערימה - המשך

6 8 12 5 8 23 10 63 17 4 9

1 2 3 4 5 6 7 8 9 10 11

Page 64: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא ליצירת ערימה - המשך

6 8 12 5 8 23 10 63 17 4 9

1 2 3 4 5 6 7 8 9 10 11

Page 65: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא ליצירת ערימה - המשך

6 8 12 5 8 23 10 63 17 4 9

1 2 3 4 5 6 7 8 9 10 11

Page 66: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא ליצירת ערימה - המשך

6 8 12 5 9 23 10 63 17 4 8

1 2 3 4 5 6 7 8 9 10 11

Page 67: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא ליצירת ערימה - המשך

6 8 12 63 9 23 10 5 17 4 8

1 2 3 4 5 6 7 8 9 10 11

Page 68: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא ליצירת ערימה - המשך

6 8 23 63 9 12 10 5 17 4 8

1 2 3 4 5 6 7 8 9 10 11

Page 69: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא ליצירת ערימה - המשך

6 63 23 8 9 12 10 5 17 4 8

1 2 3 4 5 6 7 8 9 10 11

Page 70: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא ליצירת ערימה - המשך

6 63 23 17 9 12 10 5 8 4 8

1 2 3 4 5 6 7 8 9 10 11

Page 71: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא ליצירת ערימה - המשך

63 6 23 17 9 12 10 5 8 4 8

1 2 3 4 5 6 7 8 9 10 11

Page 72: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא ליצירת ערימה - המשך

63 17 23 6 9 12 10 5 8 4 8

1 2 3 4 5 6 7 8 9 10 11

Page 73: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא ליצירת ערימה - המשך

63 17 23 8 9 12 10 5 6 4 8

1 2 3 4 5 6 7 8 9 10 11

Page 74: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא ליצירת ערימה - המשך

63 17 23 8 9 12 10 5 6 4 8

1 2 3 4 5 6 7 8 9 10 11

63

17

23

10

12

98

5 6 4 8

פלט:

ערימת מקסימום

Page 75: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

make_heapנכונות פעולת

צריך להוכיח כי בסיום הפעולה יש בידינו ערימה.

. צריך להוכיח שערכו גדול או שווה לערכי iנתבונן בצומת כלשהו בניו.

, ומכאן shift downבשלב המתאים באלגוריתם ביצענו עליו שבזמן זה ערכו היה גדול או שווה לערכי בניו. בפעמים הבאות

שעברה shift downששינינו את ערכו היה זה תוך כדי פעולת דרכו, ופעולה זו כמובן שומרת אף היא על קיום תכונת הערימה

iבצומת

הוא שרירותי, הרי שמתקבלת ערימה.iמכיוון שצומת

Page 76: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

זמן ריצה של בניית ערימה

זמן ריצה של בניית ערימה הוא סכום גבהי כל •הצמתים בעץ:

: סכום הגבהים של כל הצמתים בעץ משפט•.n קטן מ – H צמתים ובגובה nבינארי שלם בעל

: קודם כל יש לשים לב שעבור עץ הוכחת המשפט•בינארי שלם מתקיים

v

vhO )(

12 1 Hn

Page 77: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

המשך הוכחת המשפט

H-1 (השורש), שני צמתים בגובה Hישנו צומת בודד בגובה •, ..., ובאופן כללי יש H-2 צמתים בגובה 4(בניו של השורש),

.H-i צמתים בגובה לפיכך נסכום ונקבל:•

i2

nHHSS

HHHS

HHHiHS

HH

H

HH

i

i

)12(12...8422

)1(2...)2(8)1(422

)1(2...)2(4)1(2)(2

1

11

0

מש"ל

Page 78: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

del_max(Q) ו – max(Q)מימוש הפעולות

:max(Q)פעולת •

הדפס את שורש העץ (האיבר הראשון במערך)1.O(1)זמן הפעולה –

:del_max(Q)פעולת •

העבר את העלה הימני ביותר בעץ (האיבר האחרון במערך) 1. (המקום הראשון במערך) rלמקום השורש

shift_down(r)בצע 2.O(log n)זמן הפעולה –שימו לב שפעולה זו מחזירה ערימה תקינה אחרי הוצאת השורש –

מהערימה המקורית

Page 79: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Heap_sort(A)מיון

• Heap_Sort(A)

1. Q=Make_heap(A)

2. For i=1 to |A| do

3. print max(Q)

4. del_max(Q)

לא "לזרוק" את del_max(Q)הערה: במימוש ע"י מערך נהוג בפעולת האיבר הגדול ביותר, אלא להחליפו עם האיבר האחרון במערך ואז לבצע

shift_down "בצורה זו מתקבל מערך ממוין. כמובן שמחשיבים כ"ערימה .רק את האיברים המקוריים ללא תוספת הגדולים מימין.

Page 80: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Heap_sort(A)דוגמא מלאה ל –

6 8 12 5 8 23 10 63 17 4 9

1 2 3 4 5 6 7 8 9 10 11

6

8 12

10

23

85

63

17

4 9

A=

Aקלט: מערך כלשהו

Page 81: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

63 17 23 8 9 12 10 5 6 4 8

1 2 3 4 5 6 7 8 9 10 11

63

17

23

10

12

98

5 6 4 8

Q=

הערימה

Page 82: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

8 17 23 8 9 12 10 5 6 4 63

1 2 3 4 5 6 7 8 9 10 11

8

17

23

10

12

98

5 6 4

Q=

63ביטול הערך על ידי החלפתו

עם העלה הימני ביותר בעץ

Page 83: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

23 17 8 8 9 12 10 5 6 4 63

1 2 3 4 5 6 7 8 9 10 11

23

17

8

10

12

98

5 6 4

Q=

Shift_down(8)

Page 84: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

23 17 12 8 9 8 10 5 6 4 63

1 2 3 4 5 6 7 8 9 10 11

23

17

12

10

898

5 6 4

Q=

Shift_down(8)

Page 85: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

4 17 12 8 9 8 10 5 6 23 63

1 2 3 4 5 6 7 8 9 10 11

4

17

12

10

898

5 6

Q=

Del_max(Q)

Page 86: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

17 4 12 8 9 8 10 5 6 23 63

1 2 3 4 5 6 7 8 9 10 11

17

4 12

10

898

5 6

Q=

Shift_dowb(4)

Page 87: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

17 9 12 8 4 8 10 5 6 23 63

1 2 3 4 5 6 7 8 9 10 11

17

9 12

10

848

5 6

Q=

Shift_dowb(4)

Page 88: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

6 9 12 8 4 8 10 5 17 23 63

1 2 3 4 5 6 7 8 9 10 11

6

9 12

10

848

5

Q=

Del_max(Q)

Page 89: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

12 9 6 8 4 8 10 5 17 23 63

1 2 3 4 5 6 7 8 9 10 11

12

9 6

10

848

5

Q=

Shift_down(6)

Page 90: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

12 9 10 8 4 8 6 5 17 23 63

1 2 3 4 5 6 7 8 9 10 11

12

9 10

6848

5

Q=

Shift_down(6)

Page 91: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

5 9 10 8 4 8 6 12 17 23 63

1 2 3 4 5 6 7 8 9 10 11

5

9 10

6848

Q=

Del_max(Q)

Page 92: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

10 9 5 8 4 8 6 12 17 23 63

1 2 3 4 5 6 7 8 9 10 11

10

9 5

6848

Q=

Shift_down(5)

Page 93: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

10 9 8 8 4 5 6 12 17 23 63

1 2 3 4 5 6 7 8 9 10 11

10

9 8

6548

Q=

Shift_down(5)

Page 94: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

6 9 8 8 4 5 10 12 17 23 63

1 2 3 4 5 6 7 8 9 10 11

6

9 8

548

Q=

Del_max(Q)

Page 95: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

9 6 8 8 4 5 10 12 17 23 63

1 2 3 4 5 6 7 8 9 10 11

9

6 8

548

Q=

Shift_down(6)

Page 96: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

9 8 8 6 4 5 10 12 17 23 63

1 2 3 4 5 6 7 8 9 10 11

9

8 8

546

Q=

Shift_down(6)

Page 97: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

5 8 8 6 4 9 10 12 17 23 63

1 2 3 4 5 6 7 8 9 10 11

5

8 8

46

Q=

Del_max(Q)

Page 98: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

8 5 8 6 4 9 10 12 17 23 63

1 2 3 4 5 6 7 8 9 10 11

8

5 8

46

Q=

Shift_down(5)

Page 99: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

8 6 8 5 4 9 10 12 17 23 63

1 2 3 4 5 6 7 8 9 10 11

8

6 8

45

Q=

Shift_down(5)

Page 100: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

4 6 8 5 8 9 10 12 17 23 63

1 2 3 4 5 6 7 8 9 10 11

4

6 8

5

Q=

Del_max(Q)

Page 101: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

8 6 4 5 8 9 10 12 17 23 63

1 2 3 4 5 6 7 8 9 10 11

8

6 4

5

Q=

Shift_down(4)

Page 102: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

5 6 4 8 8 9 10 12 17 23 63

1 2 3 4 5 6 7 8 9 10 11

5

6 4

Q=

Del_max(Q)

Page 103: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

6 5 4 8 8 9 10 12 17 23 63

1 2 3 4 5 6 7 8 9 10 11

6

5 4

Q=

Shift_down(5)

Page 104: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

4 5 6 8 8 9 10 12 17 23 63

1 2 3 4 5 6 7 8 9 10 11

4

5

Q=

Del_max(Q)

Page 105: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

5 4 6 8 8 9 10 12 17 23 63

1 2 3 4 5 6 7 8 9 10 11

5

4

Q=

Shift_down(4)

Page 106: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

4 5 6 8 8 9 10 12 17 23 63

1 2 3 4 5 6 7 8 9 10 11

4

Q=

Del_max(Q)

Page 107: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

4 5 6 8 8 9 10 12 17 23 63

1 2 3 4 5 6 7 8 9 10 11

4

Q=

Shift_down(4)

Page 108: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמא - המשך

4 5 6 8 8 9 10 12 17 23 63

1 2 3 4 5 6 7 8 9 10 11

Q=

Del_max(Q)

Page 109: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

בסוף האלגוריתם: המערך ממוין

4 5 6 8 8 9 10 12 17 23 63

1 2 3 4 5 6 7 8 9 10 11

Q=

Page 110: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Heap_sort(A)אנליזת מקום וזמן של

אנליזת מקום: כל הפעולות מתרחשות בתוך •, לכן O(1), עם תוספות מקום של nהמערך שגודלו

.O(n)המקום הוא פעולות n בתוספת O(n)אנליזת זמן: בניית ערימה •

, לכן זמן הריצה עבור מיון זה:del_max(Q)של

)log()(log)( nnOnnOnO

Page 111: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

BucketSortמיון

k-1 מספרים שונים בתחום nמיון

k באורך Aנשתמש במערך בוליני 1

2

3

4

5

6

7

8

9

3 , 5 , 7 , 1 , 9 , 61

1

1

1

1

1 .Aאפס את 1.

A[x] = 1 בקלט בצע:xלכל 2.

{ for (i = 1; i < k ; i++)עבור על המערך ואסוף:3.

if (A[i] = = 1 ) output i { ;

1 3 5 6 7 9

Page 112: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

.Aאפס את 1.

A[x] = 1 בקלט בצע:xלכל 2.

{ for (i = 1; i < k ; i++)עבור על המערך ואסוף:3.

if (A[i] = = 1 ) output i { ;

BucketSortסיבוכיות מיון

k-1 מספרים שונים בתחום nמיון

kn

k

kn

nOk)(אם סבוכיות זמן

nO)(סבוכיות זמןסתירה לחסם התחתון!

Page 113: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

BucketSortמיון

k-1 בתחום שאינם בהכרח שונים מספרים nמיון

3 , 5 , 3 , 1 , 9 , 5 , 3

31 , 51 , 32 , 11 , 91 , 52 , 33

Stable sortמיון יציב

11 ,31 , 32 , 33 , 51 , 52 ,91

של תוריםk באורך Aנשתמש במערך

1

2

3

4

5

6

7

8

9

11

31 32 33

51 52

91 kn סבוכיות זמן

Page 114: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

BucketSortמיון

kn סבוכיות זמן

)(nOk אם

)(nO סבוכיות זמן

)( 2nOk אם

)( 2nO סבוכיות זמן

המספרים לקוחים n אינה יעילה כאשר BucketSortשיטת n << k, כלומר כאשר k..1מתחום "רחב מדי"

Page 115: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

321123231132213312111223332213

321231111132312332123213223213

1

1

1

2

2

3

2

3

3

4

111123132213213223231312321331

321123231132213312111223332213

RadixSortמיון Least Significant Digits

Page 116: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

Radix-Sort(A,d)

for i 1 to d

do use BucketSort to sort array A on digit i

RadixSortמיון

סבוכיות זמן

RadixSort מיון מספרים d10dn )10( )(ndO

)1(Od nO)(אם

בסיס

rRadixSort ממין מספרים dr

drn )(

Page 117: Sorting II: הפרד ומשול. Last week: in-place sorting Bubble Sort – O(n 2 ) comparisons –O(n) best case comparisons, O(n 2 ) exchanges Selection Sort -

דוגמה

kn סבוכיות זמן2..1..1 nk אם תחום המספרים

)( 2nO סבוכיות זמן

BucketSortמיון

RadixSortמיון בסיס

n

)(2 nn

סבוכיות זמן

)(nO

2d