0% found this document useful (0 votes)
2 views

Introduction.to.Algorithms.Getting Started (39-70)

Chapter 2 of 'Introduction to Algorithms' covers fundamental sorting algorithms, specifically Insertion Sort and Merge Sort, along with algorithm analysis and loop invariants. It provides pseudocode for these algorithms, discusses their time complexities, and includes exercises with detailed answers. The chapter emphasizes the divide-and-conquer method for Merge Sort and the importance of worst-case analysis in evaluating algorithm performance.

Uploaded by

Ahmed Jaha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Introduction.to.Algorithms.Getting Started (39-70)

Chapter 2 of 'Introduction to Algorithms' covers fundamental sorting algorithms, specifically Insertion Sort and Merge Sort, along with algorithm analysis and loop invariants. It provides pseudocode for these algorithms, discusses their time complexities, and includes exercises with detailed answers. The chapter emphasizes the divide-and-conquer method for Merge Sort and the importance of worst-case analysis in evaluating algorithm performance.

Uploaded by

Ahmed Jaha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Here's a detailed explanation of Chapter 2: "Getting Started" from the textbook

"Introduction to Algorithms" by Cormen, Leiserson, Rivest, and Stein, with


comprehensive answers to the exercises and problems:

Chapter 2: Getting Started


This chapter introduces two fundamental sorting algorithms (Insertion Sort and Merge
Sort), discusses how algorithms can be analyzed, and introduces the concept of loop
invariants and pseudocode conventions.

Section 2.1: Insertion Sort


Algorithm Explanation

Insertion Sort sorts an array by building a sorted sub-array incrementally. At each step,
the algorithm takes an element from the input data and inserts it into the correct position
within the already-sorted sub-array.

Example (Insertion Sort):


Initial array: [5, 2, 4, 6, 1, 3]
Sorted array: [1, 2, 3, 4, 5, 6]

Pseudocode:
INSERTION-SORT(A, n)
1 for i = 2 to n
2 key = A[i]
3 j = i - 1
4 while j > 0 and A[j] > key
5 A[j + 1] = A[j]
6 j = j - 1
7 A[j + 1] = key

Loop Invariant:

 Initialization: Initially true (single-element array sorted by definition).


 Maintenance: True through each iteration (element properly placed).
 Termination: Ends with the entire array sorted.

Answers to Exercises:
 Exercise 2.1-1: Illustrate INSERTION-SORT with [31,41,59,26,41,58]
similarly.
 Exercise 2.1-2:
Loop invariant for SUM-ARRAY: At the start of each iteration, sum is equal to
the sum of elements from A[1] to A[i-1].
Initialization: True initially (sum=0).
Maintenance: Each iteration adds A[i].
Termination: Ends when all elements are summed, returning the sum of A[1] to
A[n].
 Exercise 2.1-3: To sort in decreasing order, modify comparison A[j] > key to
A[j] < key.
 Exercise 2.1-4: Linear search pseudocode:

LINEAR-SEARCH(A, n, x)
1 for i = 1 to n
2 if A[i] == x
3 return i
4 return NIL

 Loop invariant: If the element is present, it's in positions not yet checked.
 Exercise 2.1-5: ADD-BINARY-INTEGERS pseudocode:

ADD-BINARY-INTEGERS(A, B, n)
1 carry = 0
2 for i = 0 to n-1
3 sum = A[i] + B[i] + carry
4 C[i] = sum mod 2
5 carry = sum div 2
6 C[n] = carry
7 return C

Section 2.2: Analyzing Algorithms


Analyzing an algorithm involves determining its resource usage (usually time)
concerning the input size.

RAM Model:

The Random Access Machine (RAM) model assumes all basic operations (arithmetic,
data movement, control flow) take constant time.

Analysis of Insertion Sort:

 Best-case: Array already sorted → O(n)O(n)


 Worst-case: Array reverse-sorted → O(n2)O(n^2)

Worst-case and Average-case analysis:


Typically, worst-case analysis is preferred because it guarantees an upper bound on
performance.

Order of Growth (Θ-notation):

Simplifies expressions by ignoring lower-order terms and constant factors, focusing on


the primary growth rate:

 3n2+2n+1=Θ(n2)3n^2 + 2n + 1 = Θ(n^2).

Answers to Exercises:

 Exercise 2.2-1: n3/1000+100n2−100n+3=Θ(n3)n^3/1000 + 100n^2 - 100n + 3 =


Θ(n^3).
 Exercise 2.2-2 (Selection Sort):
o Pseudocode:

SELECTION-SORT(A, n)
1 for i = 1 to n - 1
2 min = i
3 for j = i + 1 to n
4 if A[j] < A[min]
5 min = j
6 exchange A[i] with A[min]

 Invariant: Sub-array A[1..i] is always sorted.


 Time complexity: Best & worst case: Θ(n2)Θ(n^2).
 Exercise 2.2-3 (Linear Search):
o Average elements checked: n/2n/2; Worst case: nn.
o Running time: Average and worst-case: Θ(n)Θ(n).
 Exercise 2.2-4: Modify the sorting algorithm to check if the array is sorted first,
providing O(n)O(n) best-case.

Section 2.3: Designing Algorithms (Merge Sort)


Divide-and-Conquer Method:

This method involves three steps:

1. Divide: Problem into sub-problems.


2. Conquer: Solve recursively.
3. Combine: Solutions of sub-problems into a final solution.

Merge Sort:
 Uses divide-and-conquer by splitting the array in half, recursively sorting each
half, then merging the sorted halves.

MERGE pseudocode:
MERGE(A, p, q, r)
1 nL = q - p + 1; nR = r - q
2 let L[1..nL] and R[1..nR] be new arrays
3 copy A[p..q] into L[1..nL]
4 copy A[q+1..r] into R[1..nR]
5 i = j = 1; k = p
6 while i ≤ nL and j ≤ nR
7 if L[i] ≤ R[j]
8 A[k++] = L[i++]
9 else
10 A[k++] = R[j++]
11 copy remaining elements from L or R back to A

MERGE-SORT pseudocode:
MERGE-SORT(A, p, r)
1 if p < r
2 q = floor((p + r)/2)
3 MERGE-SORT(A, p, q)
4 MERGE-SORT(A, q+1, r)
5 MERGE(A, p, q, r)

Analysis of Merge Sort:

 Time Complexity: Θ(nlog⁡n)Θ(n \log n), better than insertion sort for large arrays.

Answers to Exercises (Section 2.3):

 Exercise 2.3-1: Illustrate MERGE-SORT with a sample array step-by-step similar


to Figure 2.4 in the text.
 Exercise 2.3-2: Recurrence for Merge Sort:
o T(n)=2T(n/2)+Θ(n)T(n) = 2T(n/2) + Θ(n), solved to Θ(nlog⁡n)Θ(n \log n).
 Exercise 2.3-3: Loop invariant for MERGE:
o Before each iteration, the sub-array A[p..k-1] contains the smallest
elements from L and R, sorted.
 Exercise 2.3-4:
MERGE procedure doesn't need to check if subarrays are exhausted explicitly
after line 12, since loops on lines 20 and 24 handle leftovers.
 Exercise 2.3-5: Rewrite MERGE without sentinels by adding explicit checks for
empty subarrays.
‫‪Reputable Source:‬‬

‫‪This content is directly based on:‬‬

‫‪‬‬ ‫‪Cormen, T. H., Leiserson, C. E., Rivest, R. L., & Stein, C. (2022).‬‬
‫‪Introduction to Algorithms (4th ed.). MIT Press.‬‬
‫‪o Introduction to Algorithms - MIT Press‬‬

‫‪If you require more detailed explanations or specific assistance on a particular exercise or‬‬
‫!‪section, please inform me‬‬

‫( "‪ "Introduction to Algorithms‬فيما يلي شرح واضح ومفصل للفصل الثاني من كتاب‬
‫‪.‬مع إجابات شاملة لألسئلة والتدريبات باللغة العربية ‪)،‬مقدمة في الخوارزميات‬

‫)‪ (Getting Started‬الفصل الثاني‪ :‬البداية‬


‫يتناول هذا الفصل مفهومين أساسيين في الخوارزميات‪ :‬خوارزمية الترتيب باإلدراج‬
‫كما يشرح كيفية تحليل أداء ‪ (Merge Sort)،‬وخوارزمية الترتيب بالدمج )‪(Insertion Sort‬‬
‫‪.‬الخوارزميات‬

‫‪ (Insertion‬القسم ‪ :2.1‬خوارزمية الترتيب باإلدراج‬


‫)‪Sort‬‬
‫‪:‬شرح الخوارزمية‬

‫ترتب هذه الخوارزمية المصفوفة عن طريق تقسيمها إلى جزء مرتب وآخر غير مرتب‪،‬‬
‫‪.‬وإدخال كل عنصر من الجزء غير المرتب في مكانه الصحيح ضمن الجزء المرتب تدريجًيا‬

‫‪:‬مثال‬
‫المصفوفة األصلية‪]3 ,1 ,6 ,4 ,2 ,5[ :‬‬
‫المصفوفة بعد الترتيب‪]6 ,5 ,4 ,3 ,2 ,1[ :‬‬

‫‪ - Pseudocode):‬الكود الوصفي( الخوارزمية‬


‫)‪INSERTION-SORT(A, n‬‬
‫‪1 for i = 2 to n‬‬
‫‪2‬‬ ‫]‪key = A[i‬‬
‫‪3‬‬ ‫‪j = i - 1‬‬
‫‪4‬‬ ‫‪while j > 0 and A[j] > key‬‬
‫‪5‬‬ ‫]‪A[j + 1] = A[j‬‬
‫‪6‬‬ ‫‪j = j - 1‬‬
‫‪7‬‬ ‫‪A[j + 1] = key‬‬

‫‪ (Loop Invariant):‬ثبات الحلقة‬

‫‪‬‬ ‫‪.‬التهيئة‪ :‬في البداية صحيحة (مصفوفة من عنصر واحد مرتبة)‬


‫‪‬‬ ‫‪.‬االحتفاظ‪ :‬في كل خطوة‪ ،‬العنصر الجديد يوضع في المكان الصحيح‬
‫‪‬‬ ‫‪.‬اإلنهاء‪ :‬عند النهاية‪ ،‬المصفوفة تكون مرتبة بالكامل‬

‫‪:‬حلول التمارين‬
‫‪‬‬ ‫[‪]31,41,59,26,41,58‬‬ ‫‪.‬تمرين ‪ :1-2.1‬توضيح خوارزمية الترتيب باإلدراج على‬
‫‪‬‬ ‫‪ (SUM-ARRAY):‬تمرين ‪2-2.1‬‬
‫هي مجموع عناصر ‪ sum‬ثبات الحلقة‪ :‬قبل كل تكرار‪ ،‬القيمة في المتغير ‪o‬‬
‫‪ A[i-1].‬حتى ]‪ A[1‬المصفوفة من‬
‫‪.‬التهيئة والصيانة واإلنهاء‪ :‬يثبت صحة المجموع في النهاية ‪o‬‬
‫‪‬‬ ‫‪ A[j] < key.‬إلى ‪ A[j] > key‬تمرين ‪ :3-2.1‬لترتيب تنازلي‪ ،‬نغير شرط الحلقة من‬
‫‪‬‬ ‫‪:‬تمرين ‪ :4-2.1‬خوارزمية البحث الخطي‬

‫)‪LINEAR-SEARCH(A, n, x‬‬
‫‪1 for i = 1 to n‬‬
‫‪2‬‬ ‫‪if A[i] == x‬‬
‫‪3‬‬ ‫‪return i‬‬
‫‪4 return NIL‬‬

‫‪‬‬ ‫‪.‬ثبات الحلقة‪ :‬العنصر موجود في الجزء الذي لم نفحصه بعد‬


‫‪‬‬ ‫‪:‬تمرين ‪ :5-2.1‬خوارزمية جمع رقمين ثنائيين‬

‫)‪ADD-BINARY-INTEGERS(A, B, n‬‬
‫‪1 carry = 0‬‬
‫‪2 for i = 0 to n-1‬‬
‫‪3‬‬ ‫‪sum = A[i] + B[i] + carry‬‬
‫‪4‬‬ ‫‪C[i] = sum mod 2‬‬
‫‪5‬‬ ‫‪carry = sum div 2‬‬
‫‪6 C[n] = carry‬‬
‫‪7 return C‬‬

‫‪ (Analyzing‬القسم ‪ :2.2‬تحليل الخوارزميات‬


‫)‪Algorithms‬‬
‫تحليل أداء الخوارزمية يتم من خالل تحديد موارد التنفيذ التي تستهلكها الخوارزمية (الوقت‬
‫‪.‬عادًة)‬

‫‪ RAM:‬نموذج‬
‫يفترض أن العمليات األساسية )‪ (Random Access Machine‬نموذج آلة الوصول العشوائي‬
‫‪.‬تستغرق وقًتا ثابًتا‬

‫‪:‬تحليل خوارزمية الترتيب باإلدراج‬

‫‪‬‬ ‫‪: O(n)O(n).‬أفضل حالة (مرتبة)‬


‫‪‬‬ ‫‪: O(n2)O(n^2).‬أسوأ حالة (معكوسة)‬

‫‪:‬تحليل أفضل وأسوأ ومتوسط الحاالت‬

‫‪.‬عادًة ُتستخدم أسوأ حالة لضمان عدم تجاوز حد زمني معين‬

‫‪ (Θ-notation):‬ترتيب النمو‬

‫‪:‬تهمل هذه الطريقة الثوابت والعناصر ذات الترتيب األقل‬

‫‪‬‬ ‫‪n2+2n+1=Θ(n2)3n^2 + 2n + 1 = Θ(n^2).‬مثاًل ‪3 :‬‬

‫‪:‬حلول التمارين‬
‫‪‬‬ ‫‪:‬تمرين ‪1-2.2‬‬
‫‪n3/1000+100n2−100n+3=Θ(n3)n^3/1000 + 100n^2 - 100n + 3 = Θ(n^3).‬‬
‫‪‬‬ ‫‪:‬تمرين ‪( 2-2.2‬الترتيب باالختيار)‬
‫‪:‬الكود الوصفي ‪o‬‬

‫)‪SELECTION-SORT(A, n‬‬
‫‪1 for i = 1 to n - 1‬‬
‫‪2‬‬ ‫‪min = i‬‬
‫‪3‬‬ ‫‪for j = i + 1 to n‬‬
‫‪4‬‬ ‫]‪if A[j] < A[min‬‬
‫‪5‬‬ ‫‪min = j‬‬
‫‪6‬‬ ‫]‪exchange A[i] with A[min‬‬

‫‪‬‬ ‫‪ i.‬ثبات الحلقة‪ :‬المصفوفة مرتبة حتى العنصر‬


‫‪‬‬ ‫‪ Θ(n2)Θ(n^2).‬التعقيد الزمني‪ :‬أفضل وأسوأ حالة‬
‫‪‬‬ ‫‪:‬تمرين ‪( 3-2.2‬البحث الخطي)‬
‫‪: nn.‬والعناصر في أسوأ حالة ‪: n/2n/2‬المتوسط ‪o‬‬
‫‪: Θ(n)Θ(n).‬التعقيد الزمني (متوسط وأسوأ) ‪o‬‬
‫‪‬‬ ‫تمرين ‪ :4-2.2‬يمكن تحسين الحالة األفضل ألي خوارزمية ترتيب بإضافة فحص ما‬
‫‪.‬إذا كانت المصفوفة مرتبة مسبًقا‬
‫خوارزمية الترتيب ( القسم ‪ :2.3‬تصميم الخوارزميات‬
‫)‪ - Merge Sort‬بالدمج‬
‫‪:‬تعتمد هذه الطريقة على ثالث خطوات‬

‫‪.‬القسمة‪ :‬تقسيم المشكلة ‪1.‬‬


‫‪.‬السيطرة (الحل)‪ :‬حل األجزاء بشكل مستقل ‪2.‬‬
‫‪.‬الدمج‪ :‬دمج الحلول الجزئية ‪3.‬‬

‫‪:‬خوارزمية الترتيب بالدمج‬

‫‪.‬تقسم المصفوفة إلى نصفين وتقوم بترتيب كل جزء ثم دمجهما‬

‫‪ (MERGE):‬خوارزمية الدمج‬
‫)‪MERGE(A, p, q, r‬‬
‫‪1 nL = q - p + 1; nR = r - q‬‬
‫‪2 let L[1..nL] and R[1..nR] be new arrays‬‬
‫]‪3 copy A[p..q] into L[1..nL‬‬
‫]‪4 copy A[q+1..r] into R[1..nR‬‬
‫‪5 i = j = 1; k = p‬‬
‫‪6 while i ≤ nL and j ≤ nR‬‬
‫‪7‬‬ ‫]‪if L[i] ≤ R[j‬‬
‫‪8‬‬ ‫]‪A[k++] = L[i++‬‬
‫‪9‬‬ ‫‪else‬‬
‫‪10‬‬ ‫]‪A[k++] = R[j++‬‬
‫‪11 copy remaining elements‬‬

‫‪ (MERGE-SORT):‬خوارزمية الترتيب بالدمج‬


‫)‪MERGE-SORT(A, p, r‬‬
‫‪1 if p < r‬‬
‫‪2‬‬ ‫)‪q = floor((p + r)/2‬‬
‫‪3‬‬ ‫)‪MERGE-SORT(A, p, q‬‬
‫‪4‬‬ ‫)‪MERGE-SORT(A, q+1, r‬‬
‫‪5‬‬ ‫)‪MERGE(A, p, q, r‬‬

‫‪:‬تحليل خوارزمية الدمج‬

‫‪‬‬ ‫أفضل من الترتيب باإلدراج للمصفوفات ‪: Θ(nlog⁡n)Θ(n \log n)،‬التعقيد الزمني‬


‫‪.‬الكبيرة‬

‫‪:‬حلول تمارين (القسم ‪)2.3‬‬


‫‪‬‬ ‫‪.‬تمرين ‪ :1-2.3‬يوضح بالتفصيل خطوات الترتيب بالدمج لمصفوفة معينة‬
‫‪‬‬ ‫‪:‬تمرين ‪ :2-2.3‬صيغة الزمن‬
‫‪ Θ(nlog⁡n)Θ(n \log n).‬وتحل إلى ‪o T(n)=2T(n/2)+Θ(n)T(n) = 2T(n/2) + Θ(n)،‬‬
‫‪‬‬ ‫‪:‬تمرين ‪ :3-2.3‬صيغة الثبات قبل كل تكرار‬
‫‪R.‬و ‪ L‬يحتوي أصغر العناصر من ]‪ A[p..k-1‬الجزء المرتب من ‪o‬‬
‫‪‬‬ ‫بعدم الحاجة للتحقق اإلضافي‪ ،‬ألن الحلقات ‪ MERGE‬تمرين ‪ :4-2.3‬يمكن تحسين‬
‫‪.‬الالحقة تكفي‬
‫‪‬‬ ‫‪.‬دون حدود إضافية ‪ MERGE‬تمرين ‪ :5-2.3‬إعادة كتابة‬

‫‪:‬المصدر المرجعي الموثوق‬


‫‪‬‬ ‫‪:‬المؤلفون ‪)،‬الطبعة الرابعة( "‪: "Introduction to Algorithms‬كتاب‬
‫‪ MIT Press‬من ‪Cormen، Leiserson، Rivest، Stein،‬‬
‫رابط المصدر‬

‫!إذا احتجت إلى تفاصيل إضافية أو توضيحات أخرى‪ ،‬يسعدني مساعدتك‬

You might also like