Introduction.to.Algorithms.Getting Started (39-70)
Introduction.to.Algorithms.Getting Started (39-70)
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.
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:
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
RAM Model:
The Random Access Machine (RAM) model assumes all basic operations (arithmetic,
data movement, control flow) take constant time.
3n2+2n+1=Θ(n2)3n^2 + 2n + 1 = Θ(n^2).
Answers to Exercises:
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]
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)
Time Complexity: Θ(nlogn)Θ(n \log n), better than insertion sort for large arrays.
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فيما يلي شرح واضح ومفصل للفصل الثاني من كتاب
.مع إجابات شاملة لألسئلة والتدريبات باللغة العربية )،مقدمة في الخوارزميات
ترتب هذه الخوارزمية المصفوفة عن طريق تقسيمها إلى جزء مرتب وآخر غير مرتب،
.وإدخال كل عنصر من الجزء غير المرتب في مكانه الصحيح ضمن الجزء المرتب تدريجًيا
:مثال
المصفوفة األصلية]3 ,1 ,6 ,4 ,2 ,5[ :
المصفوفة بعد الترتيب]6 ,5 ,4 ,3 ,2 ,1[ :
:حلول التمارين
[]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
)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
RAM:نموذج
يفترض أن العمليات األساسية ) (Random Access Machineنموذج آلة الوصول العشوائي
.تستغرق وقًتا ثابًتا
(Θ-notation):ترتيب النمو
:حلول التمارين
:تمرين 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
(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