SlideShare a Scribd company logo
1
‫איטרטיבי‬ ‫מיזוג‬ ‫מיון‬
‫ארצי‬ ‫שי‬ ‫של‬ ‫השקפים‬ ‫על‬ ‫מבוסס‬
,
‫גיתית‬
‫רוקנשטיין‬
,
‫איתן‬
‫אביאור‬
,
‫סאהר‬
‫אסמיר‬
,
‫אלעד‬ ‫מיכאל‬
,
‫רביב‬ ‫ודן‬ ‫קימל‬ ‫רון‬
.
‫ע‬ ‫אחרון‬ ‫עדכון‬
"
‫תשפ‬ ‫קיץ‬ ‫ארז‬ ‫יעל‬ ‫י‬
"
‫ב‬
2
‫המצגת‬ ‫נושאי‬
•
‫ממוינים‬ ‫מערכים‬ ‫מיזוג‬
•
‫מיזוג‬ ‫באמצעות‬ ‫מערכים‬ ‫מיון‬
3
‫ממוינים‬ ‫מערכים‬ ‫מיזוג‬
-
Merging
•
‫הנתון‬ ‫הקלט‬
:
‫ממוינים‬ ‫מערכים‬ ‫שני‬
a[]
‫ו‬
-
b[]
‫שאורכיהם‬
na
‫ו‬
-
,nb
‫בהתאמה‬
.
•
‫הרצוי‬ ‫הפלט‬
:
‫אחד‬ ‫ממוין‬ ‫מערך‬
,
c[]
,
‫המערכים‬ ‫מאיברי‬ ‫המורכב‬
a[]
‫ו‬
-
b[]
.
•
‫האלגוריתם‬
:
•
‫מערך‬ ‫לכל‬ ‫אינדקס‬ ‫משתנה‬ ‫נחזיק‬
ic ,ib ,ia
(
‫ל‬ ‫מאותחלים‬
-
0
.)
•
‫את‬ ‫נשווה‬
a[ia]
‫ל‬
-
b[ib]
•
‫הקטן‬ ‫את‬
‫מביניהם‬
‫ב‬ ‫הבא‬ ‫למקום‬ ‫נעתיק‬
-
c[ic]
‫את‬ ‫ונקדם‬
ic
.
•
‫מ‬ ‫איבר‬ ‫לקחנו‬ ‫אם‬
-
a
‫את‬ ‫נקדם‬
ia
.
‫את‬ ‫נקדם‬ ‫אחרת‬
ib
.
•
‫הרשימות‬ ‫אחת‬ ‫לסוף‬ ‫כשנגיע‬
,
‫ל‬ ‫האחרת‬ ‫הרשימה‬ ‫שארית‬ ‫את‬ ‫נעתיק‬
-
c[]
.
4
‫ממוינים‬ ‫מערכים‬ ‫מיזוג‬
–
‫דוגמא‬
16 19 25 48 50 67
14 22 37 45 56 61
14 16 19 22 25 37 45 48 50 56 61 67
5
void merge(int a[], int na, int b[], int nb, int c[]){
int ia, ib, ic;
for(ia = ib = ic = 0; (ia < na) && (ib < nb); ic++){
if(a[ia] < b[ib]) {
c[ic] = a[ia];
ia++;
}
else {
c[ic] = b[ib];
ib++;
}
}
for(;ia < na; ia++, ic++)
c[ic] = a[ia];
for(;ib < nb; ib++, ic++)
c[ic] = b[ib];
}
Merge
–
‫מימוש‬
ia = ib = ic = 0;
while (ia < na && ib < nb)
c[ic++] = (a[ia] < b[ib]) ? a[ia++] : b[ib++];
while (ia < na)
c[ic++] = a[ia++];
while (ib < nb)
c[ic++] = b[ib++];
6
‫המיזוג‬ ‫של‬ ‫הזמן‬ ‫סיבוכיות‬
•
‫את‬ ‫מקדמים‬ ‫אנו‬ ‫איטרציה‬ ‫בכל‬
ia
‫או‬
ib
.
•
‫פעולות‬ ‫מספר‬ ‫מבצעים‬ ‫אנו‬ ‫איטרציה‬ ‫בכל‬
(
‫והעתקות‬ ‫השוואות‬
)
‫ע‬ ‫החסום‬
"
‫הקלט‬ ‫בגודל‬ ‫תלוי‬ ‫שאינו‬ ‫קבוע‬ ‫י‬
(
‫סיבוכיות‬
‫קבועה‬
(1)
.)
•
‫היא‬ ‫הזמן‬ ‫סיבוכיות‬ ‫לכן‬
(na + nb)
.
‫המיזוג‬ ‫אלגוריתם‬ ‫של‬ ‫הזמן‬ ‫סיבוכיות‬
‫הינה‬
‫ליניארית‬
‫שלו‬ ‫הקלט‬ ‫בגודל‬
.
7
‫המצגת‬ ‫נושאי‬
•
‫ממוינים‬ ‫מערכים‬ ‫מיזוג‬
•
‫מיזוג‬ ‫באמצעות‬ ‫מערכים‬ ‫מיון‬
8
‫מיזוגים‬ ‫באמצעות‬ ‫מיון‬
:
Merge-Sort
•
‫מערך‬ ‫נתון‬
a[]
‫המכיל‬
n=2k
‫מספרים‬
.
‫המערך‬ ‫את‬ ‫למיין‬ ‫יש‬
‫יורד‬ ‫לא‬ ‫בסדר‬
.
•
‫תתי‬ ‫למזג‬ ‫ביכולתנו‬ ‫נשתמש‬
-
‫הפרופורציונית‬ ‫ליניארית‬ ‫בסיבוכיות‬ ‫סדרות‬
‫במשותף‬ ‫איבריהן‬ ‫לסך‬
.
•
‫האלגוריתם‬
:
•
‫לזוגות‬ ‫המערך‬ ‫את‬ ‫נחלק‬
.
‫זוג‬ ‫כל‬ ‫של‬ ‫פנימי‬ ‫מיון‬ ‫נבצע‬
.
•
‫סמוכים‬ ‫זוגות‬ ‫שני‬ ‫כל‬ ‫נמזג‬
(
‫ממוינים‬
)
‫ממוינת‬ ‫לרביעייה‬
.
•
‫סמוכות‬ ‫רביעיות‬ ‫שתי‬ ‫כל‬ ‫נמזג‬
(
‫ממוינות‬
)
‫ממוינת‬ ‫לשמינייה‬
.
•
...
•
‫באורך‬ ‫ממוינות‬ ‫רשימות‬ ‫שתי‬ ‫נמזג‬
2k-1
‫באורך‬ ‫ממוינת‬ ‫לרשימה‬
2k
.
9
5
2
0 1
6 7
4
3
2
6
0 3
5 4
1
7
‫מיזוג‬ ‫באמצעות‬ ‫מיון‬
–
‫דוגמה‬
2
6
0 3
5 4
1
7
5
2
0 1
6 7
4
3
2
1
0 4
3 7
6
5
‫איטרציה‬
3
‫איטרציה‬
2
5
0
6 7
2 4
1
3
‫איטרציה‬
1
10
‫המחשב‬ ‫למדעי‬ ‫מבוא‬
.
‫שמורות‬ ‫הזכויות‬ ‫כל‬
© 10
‫שלב‬ ‫כל‬ ‫סיבוכיות‬ ‫ניתוח‬
•
‫המוצע‬ ‫האלגוריתם‬ ‫של‬ ‫הזמן‬ ‫סיבוכיות‬ ‫מהי‬
?
•
‫באורך‬ ‫סדרות‬ ‫שתי‬ ‫של‬ ‫מיזוגים‬ ‫הם‬ ‫הבניין‬ ‫אבני‬
2j
‫עם‬
(2j+1)
‫פעולות‬
.
•
‫ה‬ ‫באלגוריתם‬
-
Merge-Sort
:
•
‫שלב‬
1
:
‫על‬ ‫פועלים‬
2k-1
‫באורך‬ ‫רשימות‬
21
‫ב‬ ‫אחת‬ ‫כל‬ ‫ומסדרים‬
-
2
‫פעולות‬
←
‫סך‬
‫של‬
2k
‫פעולות‬
(
=n
.)
•
‫שלב‬
2
:
‫על‬ ‫פועלים‬
2k-2
‫באורך‬ ‫רשימות‬
22
,
‫זוגות‬ ‫שני‬ ‫של‬ ‫כאיחוד‬ ‫נבנית‬ ‫אחת‬ ‫כשכל‬
‫עם‬
(22)
.
‫של‬ ‫סך‬ ‫יידרשו‬ ‫הכול‬ ‫בסך‬
2k
‫פעולות‬
(
=n
.)
•
...
•
‫שלב‬
j
:
‫יוצרים‬
2k-j
‫באורך‬ ‫רשימות‬
2j
,
‫באורך‬ ‫רשימות‬ ‫שתי‬ ‫מיזוג‬ ‫הינה‬ ‫אחת‬ ‫כשכל‬
2j-1
‫סיבוכיות‬ ‫עם‬
(
2j
)

‫ולכן‬
‫של‬ ‫סך‬ ‫יידרשו‬
2k
‫פעולות‬
(
=n
.)
•
‫מסקנה‬
-
‫היא‬ ‫באלגוריתם‬ ‫מהשלבים‬ ‫אחד‬ ‫כל‬ ‫סיבוכיות‬
(n)
.
11
‫המחשב‬ ‫למדעי‬ ‫מבוא‬
.
‫שמורות‬ ‫הזכויות‬ ‫כל‬
© 11
‫סיבוכיות‬ ‫ניתוח‬
Merge-Sort
•
‫כולו‬ ‫המיון‬ ‫כל‬ ‫של‬ ‫הזמן‬ ‫סיבוכיות‬ ‫מהי‬
?
•
‫מספר‬
‫האיטרציות‬
‫באלגוריתם‬
:
k=log2n
‫שלבים‬
‫שבכל‬
‫אחד‬
(2k=n)
‫פעולות‬
.
•
‫סיבוכיות‬
‫היא‬ ‫האלגוריתם‬
:
(k·2k) = (nlog2n)
.
•
‫האלגוריתם‬
Merge-Sort
‫הריבועיים‬ ‫המיון‬ ‫מאלגוריתמי‬ ‫יותר‬ ‫יעיל‬
‫שהכרנו‬
.
•
‫ניתן‬
‫להוכיח‬
(
‫כאן‬ ‫לא‬
)
‫מיון‬ ‫אלגוריתם‬ ‫קיים‬ ‫לא‬ ‫כי‬
,
‫השוואות‬ ‫מבוסס‬
,
‫יותר‬ ‫קטנה‬ ‫זמן‬ ‫סיבוכיות‬ ‫בעל‬
.
12
‫מיזוג‬ ‫באמצעות‬ ‫מיון‬
–
‫איטרטיבי‬ ‫מימוש‬
•
‫הפונקציה‬
merge_sort()
‫מקוננות‬ ‫לולאות‬ ‫משתי‬ ‫מורכבת‬
.
•
‫החיצונית‬ ‫הלולאה‬ ‫משתנה‬
,
len
,
‫שממזגים‬ ‫הרשימות‬ ‫אורך‬ ‫את‬ ‫שומר‬
1,2,4,8, ... ,2(k-1)
.
•
‫הפנימית‬ ‫הלולאה‬ ‫משתנה‬
base
‫הרשימה‬ ‫התחלת‬ ‫כתובת‬ ‫את‬ ‫מחזיק‬
‫למיזוג‬ ‫רשימות‬ ‫זוג‬ ‫בכל‬ ‫הראשונה‬
.
•
‫הפונקציה‬
merge_sort()
‫בפונקציה‬ ‫משתמשת‬
merge()
,
‫פגשנו‬ ‫אותה‬
‫קודם‬
.
•
‫בפונקציית‬ ‫שימוש‬ ‫נעשה‬ ‫גם‬ ‫אנו‬
‫הספריה‬
memcpy
‫מ‬
-
string.h
‫להעתקת‬
‫ידועים‬ ‫ואורך‬ ‫במיקום‬ ‫זיכרון‬ ‫נתח‬
.
‫אחר‬ ‫זמני‬ ‫למערך‬ ‫נעשה‬ ‫שהמיזוג‬ ‫כיוון‬ ‫זאת‬
,
‫המקורי‬ ‫במערך‬ ‫למקומו‬ ‫חזרה‬ ‫להעבירו‬ ‫ויש‬
.
13
Merge Sort
–
‫מימוש‬
int merge_sort(int ar[], int n){
int len;
int *temp_array, *base;
temp_array = (int*)malloc(sizeof(int)*n);
if(temp_array == NULL) {
printf("Dynamic Allocation Error in merge_sort");
return FAILURE;
}
for (len = 1; len < n; len *= 2) {
for (base = ar; base < ar + n; base += 2 * len) {
merge(base, len, base + len, len, temp_array);
memcpy(base, temp_array, 2*len*sizeof(int));
}
}
free(temp_array);
return SUCCESS;
}
RUN
14
‫המחשב‬ ‫למדעי‬ ‫מבוא‬
.
4
Merge-Sort
–
‫גראפית‬ ‫המחשה‬
‫ע‬ ‫האלגוריתם‬ ‫התקדמות‬ ‫דרך‬ ‫את‬ ‫נדגים‬
"
‫סידור‬ ‫י‬
4096
‫בתחום‬ ‫שונים‬ ‫מספרים‬
0-4095
‫התוצאה‬ ‫המחשת‬
‫כגרף‬ ‫איטרציה‬ ‫בכל‬
.
‫כי‬ ‫ניכר‬
:
•
‫מסודרים‬ ‫האיברים‬
‫וגדלים‬ ‫הולכים‬ ‫במקבצים‬
‫ע‬
"
‫תתי‬ ‫מיזוג‬ ‫י‬
-
‫סדרות‬
‫יותר‬ ‫קצרות‬
.
•
‫הנחוצה‬ ‫הצעדים‬ ‫כמות‬
‫הינה‬
12
‫על‬ ‫מעברים‬
‫הנתונים‬
.
0 500 1000 1500 2000 2500 3000 3500 4000
0
500
1000
1500
2000
2500
3000
3500
4000
0 500 1000 1500 2000 2500 3000 3500 4000
0
500
1000
1500
2000
2500
3000
3500
4000
0 500 1000 1500 2000 2500 3000 3500 4000
0
500
1000
1500
2000
2500
3000
3500
4000
0 500 1000 1500 2000 2500 3000 3500 4000
0
500
1000
1500
2000
2500
3000
3500
4000
0 500 1000 1500 2000 2500 3000 3500 4000
0
500
1000
1500
2000
2500
3000
3500
4000
0 500 1000 1500 2000 2500 3000 3500 4000
0
500
1000
1500
2000
2500
3000
3500
4000
0 500 1000 1500 2000 2500 3000 3500 4000
0
500
1000
1500
2000
2500
3000
3500
4000
0 500 1000 1500 2000 2500 3000 3500 4000
0
500
1000
1500
2000
2500
3000
3500
4000
0 500 1000 1500 2000 2500 3000 3500 4000
0
500
1000
1500
2000
2500
3000
3500
4000
0 500 1000 1500 2000 2500 3000 3500 4000
0
500
1000
1500
2000
2500
3000
3500
4000
0 500 1000 1500 2000 2500 3000 3500 4000
0
500
1000
1500
2000
2500
3000
3500
4000
15
•
‫כזכור‬
:
•
‫בעזרת‬ ‫המיון‬ ‫של‬ ‫הזמן‬ ‫סיבוכיות‬
Merge-Sort
‫הינה‬
(nlog2n)
.
•
‫אותם‬ ‫האחרים‬ ‫באלגוריתמים‬ ‫המיון‬ ‫של‬ ‫הזמן‬ ‫סיבוכיות‬
‫פגשנו‬
(
Max-Sort, Bubble-Sort
)
‫הינה‬
(n2)
.
•
‫לדוגמה‬
,
•
‫מסקנות‬
:
•
‫באלג‬
'
‫ב‬ ‫הכפלה‬ ‫כל‬ ‫הריבועיים‬
-
2
‫פי‬ ‫הכפלה‬ ‫נותנת‬ ‫בכניסה‬
4
‫בסיבוכיות‬
,
•
‫באלג‬
'
‫ה‬
-
Merge-Sort
‫פי‬ ‫הכפלה‬
2
‫מפי‬ ‫יותר‬ ‫מעט‬ ‫של‬ ‫עליה‬ ‫גוררת‬
2
‫בסיבוכיות‬
.
•
‫ה‬
-
Merge-Sort
‫כ‬ ‫פי‬ ‫יעיל‬
-
500
‫עבור‬ ‫הריבועיים‬ ‫שבאלגוריתמים‬ ‫הטוב‬ ‫מול‬
215
n=
.
‫סיבוכיות‬
Merge-Sort
‫המתחרים‬ ‫מול‬
n Bubble-Sort Max-Sort Merge-Sort
215 14 sec 4 sec 0.008 sec
216 55 sec 17 sec 0.016 sec
217 221 sec 69 sec 0.034 sec

More Related Content

Featured

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
GetSmarter
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
Project for Public Spaces & National Center for Biking and Walking
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
Erica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Saba Software
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
Simplilearn
 

Featured (20)

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
 

lecture9_iterative_merge_sort (1).pdf

  • 1. 1 ‫איטרטיבי‬ ‫מיזוג‬ ‫מיון‬ ‫ארצי‬ ‫שי‬ ‫של‬ ‫השקפים‬ ‫על‬ ‫מבוסס‬ , ‫גיתית‬ ‫רוקנשטיין‬ , ‫איתן‬ ‫אביאור‬ , ‫סאהר‬ ‫אסמיר‬ , ‫אלעד‬ ‫מיכאל‬ , ‫רביב‬ ‫ודן‬ ‫קימל‬ ‫רון‬ . ‫ע‬ ‫אחרון‬ ‫עדכון‬ " ‫תשפ‬ ‫קיץ‬ ‫ארז‬ ‫יעל‬ ‫י‬ " ‫ב‬
  • 2. 2 ‫המצגת‬ ‫נושאי‬ • ‫ממוינים‬ ‫מערכים‬ ‫מיזוג‬ • ‫מיזוג‬ ‫באמצעות‬ ‫מערכים‬ ‫מיון‬
  • 3. 3 ‫ממוינים‬ ‫מערכים‬ ‫מיזוג‬ - Merging • ‫הנתון‬ ‫הקלט‬ : ‫ממוינים‬ ‫מערכים‬ ‫שני‬ a[] ‫ו‬ - b[] ‫שאורכיהם‬ na ‫ו‬ - ,nb ‫בהתאמה‬ . • ‫הרצוי‬ ‫הפלט‬ : ‫אחד‬ ‫ממוין‬ ‫מערך‬ , c[] , ‫המערכים‬ ‫מאיברי‬ ‫המורכב‬ a[] ‫ו‬ - b[] . • ‫האלגוריתם‬ : • ‫מערך‬ ‫לכל‬ ‫אינדקס‬ ‫משתנה‬ ‫נחזיק‬ ic ,ib ,ia ( ‫ל‬ ‫מאותחלים‬ - 0 .) • ‫את‬ ‫נשווה‬ a[ia] ‫ל‬ - b[ib] • ‫הקטן‬ ‫את‬ ‫מביניהם‬ ‫ב‬ ‫הבא‬ ‫למקום‬ ‫נעתיק‬ - c[ic] ‫את‬ ‫ונקדם‬ ic . • ‫מ‬ ‫איבר‬ ‫לקחנו‬ ‫אם‬ - a ‫את‬ ‫נקדם‬ ia . ‫את‬ ‫נקדם‬ ‫אחרת‬ ib . • ‫הרשימות‬ ‫אחת‬ ‫לסוף‬ ‫כשנגיע‬ , ‫ל‬ ‫האחרת‬ ‫הרשימה‬ ‫שארית‬ ‫את‬ ‫נעתיק‬ - c[] .
  • 4. 4 ‫ממוינים‬ ‫מערכים‬ ‫מיזוג‬ – ‫דוגמא‬ 16 19 25 48 50 67 14 22 37 45 56 61 14 16 19 22 25 37 45 48 50 56 61 67
  • 5. 5 void merge(int a[], int na, int b[], int nb, int c[]){ int ia, ib, ic; for(ia = ib = ic = 0; (ia < na) && (ib < nb); ic++){ if(a[ia] < b[ib]) { c[ic] = a[ia]; ia++; } else { c[ic] = b[ib]; ib++; } } for(;ia < na; ia++, ic++) c[ic] = a[ia]; for(;ib < nb; ib++, ic++) c[ic] = b[ib]; } Merge – ‫מימוש‬ ia = ib = ic = 0; while (ia < na && ib < nb) c[ic++] = (a[ia] < b[ib]) ? a[ia++] : b[ib++]; while (ia < na) c[ic++] = a[ia++]; while (ib < nb) c[ic++] = b[ib++];
  • 6. 6 ‫המיזוג‬ ‫של‬ ‫הזמן‬ ‫סיבוכיות‬ • ‫את‬ ‫מקדמים‬ ‫אנו‬ ‫איטרציה‬ ‫בכל‬ ia ‫או‬ ib . • ‫פעולות‬ ‫מספר‬ ‫מבצעים‬ ‫אנו‬ ‫איטרציה‬ ‫בכל‬ ( ‫והעתקות‬ ‫השוואות‬ ) ‫ע‬ ‫החסום‬ " ‫הקלט‬ ‫בגודל‬ ‫תלוי‬ ‫שאינו‬ ‫קבוע‬ ‫י‬ ( ‫סיבוכיות‬ ‫קבועה‬ (1) .) • ‫היא‬ ‫הזמן‬ ‫סיבוכיות‬ ‫לכן‬ (na + nb) . ‫המיזוג‬ ‫אלגוריתם‬ ‫של‬ ‫הזמן‬ ‫סיבוכיות‬ ‫הינה‬ ‫ליניארית‬ ‫שלו‬ ‫הקלט‬ ‫בגודל‬ .
  • 7. 7 ‫המצגת‬ ‫נושאי‬ • ‫ממוינים‬ ‫מערכים‬ ‫מיזוג‬ • ‫מיזוג‬ ‫באמצעות‬ ‫מערכים‬ ‫מיון‬
  • 8. 8 ‫מיזוגים‬ ‫באמצעות‬ ‫מיון‬ : Merge-Sort • ‫מערך‬ ‫נתון‬ a[] ‫המכיל‬ n=2k ‫מספרים‬ . ‫המערך‬ ‫את‬ ‫למיין‬ ‫יש‬ ‫יורד‬ ‫לא‬ ‫בסדר‬ . • ‫תתי‬ ‫למזג‬ ‫ביכולתנו‬ ‫נשתמש‬ - ‫הפרופורציונית‬ ‫ליניארית‬ ‫בסיבוכיות‬ ‫סדרות‬ ‫במשותף‬ ‫איבריהן‬ ‫לסך‬ . • ‫האלגוריתם‬ : • ‫לזוגות‬ ‫המערך‬ ‫את‬ ‫נחלק‬ . ‫זוג‬ ‫כל‬ ‫של‬ ‫פנימי‬ ‫מיון‬ ‫נבצע‬ . • ‫סמוכים‬ ‫זוגות‬ ‫שני‬ ‫כל‬ ‫נמזג‬ ( ‫ממוינים‬ ) ‫ממוינת‬ ‫לרביעייה‬ . • ‫סמוכות‬ ‫רביעיות‬ ‫שתי‬ ‫כל‬ ‫נמזג‬ ( ‫ממוינות‬ ) ‫ממוינת‬ ‫לשמינייה‬ . • ... • ‫באורך‬ ‫ממוינות‬ ‫רשימות‬ ‫שתי‬ ‫נמזג‬ 2k-1 ‫באורך‬ ‫ממוינת‬ ‫לרשימה‬ 2k .
  • 9. 9 5 2 0 1 6 7 4 3 2 6 0 3 5 4 1 7 ‫מיזוג‬ ‫באמצעות‬ ‫מיון‬ – ‫דוגמה‬ 2 6 0 3 5 4 1 7 5 2 0 1 6 7 4 3 2 1 0 4 3 7 6 5 ‫איטרציה‬ 3 ‫איטרציה‬ 2 5 0 6 7 2 4 1 3 ‫איטרציה‬ 1
  • 10. 10 ‫המחשב‬ ‫למדעי‬ ‫מבוא‬ . ‫שמורות‬ ‫הזכויות‬ ‫כל‬ © 10 ‫שלב‬ ‫כל‬ ‫סיבוכיות‬ ‫ניתוח‬ • ‫המוצע‬ ‫האלגוריתם‬ ‫של‬ ‫הזמן‬ ‫סיבוכיות‬ ‫מהי‬ ? • ‫באורך‬ ‫סדרות‬ ‫שתי‬ ‫של‬ ‫מיזוגים‬ ‫הם‬ ‫הבניין‬ ‫אבני‬ 2j ‫עם‬ (2j+1) ‫פעולות‬ . • ‫ה‬ ‫באלגוריתם‬ - Merge-Sort : • ‫שלב‬ 1 : ‫על‬ ‫פועלים‬ 2k-1 ‫באורך‬ ‫רשימות‬ 21 ‫ב‬ ‫אחת‬ ‫כל‬ ‫ומסדרים‬ - 2 ‫פעולות‬ ← ‫סך‬ ‫של‬ 2k ‫פעולות‬ ( =n .) • ‫שלב‬ 2 : ‫על‬ ‫פועלים‬ 2k-2 ‫באורך‬ ‫רשימות‬ 22 , ‫זוגות‬ ‫שני‬ ‫של‬ ‫כאיחוד‬ ‫נבנית‬ ‫אחת‬ ‫כשכל‬ ‫עם‬ (22) . ‫של‬ ‫סך‬ ‫יידרשו‬ ‫הכול‬ ‫בסך‬ 2k ‫פעולות‬ ( =n .) • ... • ‫שלב‬ j : ‫יוצרים‬ 2k-j ‫באורך‬ ‫רשימות‬ 2j , ‫באורך‬ ‫רשימות‬ ‫שתי‬ ‫מיזוג‬ ‫הינה‬ ‫אחת‬ ‫כשכל‬ 2j-1 ‫סיבוכיות‬ ‫עם‬ ( 2j )  ‫ולכן‬ ‫של‬ ‫סך‬ ‫יידרשו‬ 2k ‫פעולות‬ ( =n .) • ‫מסקנה‬ - ‫היא‬ ‫באלגוריתם‬ ‫מהשלבים‬ ‫אחד‬ ‫כל‬ ‫סיבוכיות‬ (n) .
  • 11. 11 ‫המחשב‬ ‫למדעי‬ ‫מבוא‬ . ‫שמורות‬ ‫הזכויות‬ ‫כל‬ © 11 ‫סיבוכיות‬ ‫ניתוח‬ Merge-Sort • ‫כולו‬ ‫המיון‬ ‫כל‬ ‫של‬ ‫הזמן‬ ‫סיבוכיות‬ ‫מהי‬ ? • ‫מספר‬ ‫האיטרציות‬ ‫באלגוריתם‬ : k=log2n ‫שלבים‬ ‫שבכל‬ ‫אחד‬ (2k=n) ‫פעולות‬ . • ‫סיבוכיות‬ ‫היא‬ ‫האלגוריתם‬ : (k·2k) = (nlog2n) . • ‫האלגוריתם‬ Merge-Sort ‫הריבועיים‬ ‫המיון‬ ‫מאלגוריתמי‬ ‫יותר‬ ‫יעיל‬ ‫שהכרנו‬ . • ‫ניתן‬ ‫להוכיח‬ ( ‫כאן‬ ‫לא‬ ) ‫מיון‬ ‫אלגוריתם‬ ‫קיים‬ ‫לא‬ ‫כי‬ , ‫השוואות‬ ‫מבוסס‬ , ‫יותר‬ ‫קטנה‬ ‫זמן‬ ‫סיבוכיות‬ ‫בעל‬ .
  • 12. 12 ‫מיזוג‬ ‫באמצעות‬ ‫מיון‬ – ‫איטרטיבי‬ ‫מימוש‬ • ‫הפונקציה‬ merge_sort() ‫מקוננות‬ ‫לולאות‬ ‫משתי‬ ‫מורכבת‬ . • ‫החיצונית‬ ‫הלולאה‬ ‫משתנה‬ , len , ‫שממזגים‬ ‫הרשימות‬ ‫אורך‬ ‫את‬ ‫שומר‬ 1,2,4,8, ... ,2(k-1) . • ‫הפנימית‬ ‫הלולאה‬ ‫משתנה‬ base ‫הרשימה‬ ‫התחלת‬ ‫כתובת‬ ‫את‬ ‫מחזיק‬ ‫למיזוג‬ ‫רשימות‬ ‫זוג‬ ‫בכל‬ ‫הראשונה‬ . • ‫הפונקציה‬ merge_sort() ‫בפונקציה‬ ‫משתמשת‬ merge() , ‫פגשנו‬ ‫אותה‬ ‫קודם‬ . • ‫בפונקציית‬ ‫שימוש‬ ‫נעשה‬ ‫גם‬ ‫אנו‬ ‫הספריה‬ memcpy ‫מ‬ - string.h ‫להעתקת‬ ‫ידועים‬ ‫ואורך‬ ‫במיקום‬ ‫זיכרון‬ ‫נתח‬ . ‫אחר‬ ‫זמני‬ ‫למערך‬ ‫נעשה‬ ‫שהמיזוג‬ ‫כיוון‬ ‫זאת‬ , ‫המקורי‬ ‫במערך‬ ‫למקומו‬ ‫חזרה‬ ‫להעבירו‬ ‫ויש‬ .
  • 13. 13 Merge Sort – ‫מימוש‬ int merge_sort(int ar[], int n){ int len; int *temp_array, *base; temp_array = (int*)malloc(sizeof(int)*n); if(temp_array == NULL) { printf("Dynamic Allocation Error in merge_sort"); return FAILURE; } for (len = 1; len < n; len *= 2) { for (base = ar; base < ar + n; base += 2 * len) { merge(base, len, base + len, len, temp_array); memcpy(base, temp_array, 2*len*sizeof(int)); } } free(temp_array); return SUCCESS; } RUN
  • 14. 14 ‫המחשב‬ ‫למדעי‬ ‫מבוא‬ . 4 Merge-Sort – ‫גראפית‬ ‫המחשה‬ ‫ע‬ ‫האלגוריתם‬ ‫התקדמות‬ ‫דרך‬ ‫את‬ ‫נדגים‬ " ‫סידור‬ ‫י‬ 4096 ‫בתחום‬ ‫שונים‬ ‫מספרים‬ 0-4095 ‫התוצאה‬ ‫המחשת‬ ‫כגרף‬ ‫איטרציה‬ ‫בכל‬ . ‫כי‬ ‫ניכר‬ : • ‫מסודרים‬ ‫האיברים‬ ‫וגדלים‬ ‫הולכים‬ ‫במקבצים‬ ‫ע‬ " ‫תתי‬ ‫מיזוג‬ ‫י‬ - ‫סדרות‬ ‫יותר‬ ‫קצרות‬ . • ‫הנחוצה‬ ‫הצעדים‬ ‫כמות‬ ‫הינה‬ 12 ‫על‬ ‫מעברים‬ ‫הנתונים‬ . 0 500 1000 1500 2000 2500 3000 3500 4000 0 500 1000 1500 2000 2500 3000 3500 4000 0 500 1000 1500 2000 2500 3000 3500 4000 0 500 1000 1500 2000 2500 3000 3500 4000 0 500 1000 1500 2000 2500 3000 3500 4000 0 500 1000 1500 2000 2500 3000 3500 4000 0 500 1000 1500 2000 2500 3000 3500 4000 0 500 1000 1500 2000 2500 3000 3500 4000 0 500 1000 1500 2000 2500 3000 3500 4000 0 500 1000 1500 2000 2500 3000 3500 4000 0 500 1000 1500 2000 2500 3000 3500 4000 0 500 1000 1500 2000 2500 3000 3500 4000 0 500 1000 1500 2000 2500 3000 3500 4000 0 500 1000 1500 2000 2500 3000 3500 4000 0 500 1000 1500 2000 2500 3000 3500 4000 0 500 1000 1500 2000 2500 3000 3500 4000 0 500 1000 1500 2000 2500 3000 3500 4000 0 500 1000 1500 2000 2500 3000 3500 4000 0 500 1000 1500 2000 2500 3000 3500 4000 0 500 1000 1500 2000 2500 3000 3500 4000 0 500 1000 1500 2000 2500 3000 3500 4000 0 500 1000 1500 2000 2500 3000 3500 4000
  • 15. 15 • ‫כזכור‬ : • ‫בעזרת‬ ‫המיון‬ ‫של‬ ‫הזמן‬ ‫סיבוכיות‬ Merge-Sort ‫הינה‬ (nlog2n) . • ‫אותם‬ ‫האחרים‬ ‫באלגוריתמים‬ ‫המיון‬ ‫של‬ ‫הזמן‬ ‫סיבוכיות‬ ‫פגשנו‬ ( Max-Sort, Bubble-Sort ) ‫הינה‬ (n2) . • ‫לדוגמה‬ , • ‫מסקנות‬ : • ‫באלג‬ ' ‫ב‬ ‫הכפלה‬ ‫כל‬ ‫הריבועיים‬ - 2 ‫פי‬ ‫הכפלה‬ ‫נותנת‬ ‫בכניסה‬ 4 ‫בסיבוכיות‬ , • ‫באלג‬ ' ‫ה‬ - Merge-Sort ‫פי‬ ‫הכפלה‬ 2 ‫מפי‬ ‫יותר‬ ‫מעט‬ ‫של‬ ‫עליה‬ ‫גוררת‬ 2 ‫בסיבוכיות‬ . • ‫ה‬ - Merge-Sort ‫כ‬ ‫פי‬ ‫יעיל‬ - 500 ‫עבור‬ ‫הריבועיים‬ ‫שבאלגוריתמים‬ ‫הטוב‬ ‫מול‬ 215 n= . ‫סיבוכיות‬ Merge-Sort ‫המתחרים‬ ‫מול‬ n Bubble-Sort Max-Sort Merge-Sort 215 14 sec 4 sec 0.008 sec 216 55 sec 17 sec 0.016 sec 217 221 sec 69 sec 0.034 sec