اموزش الگوریتم!! 1
CHAPTER 1: INTRODUCTION
1.1 Algorithms
Input: A sequence of n numbers
a1, a2, . . . , an
.
Output: A permutation (reordering)
of the input sequence such that
.
Insertion sort
Figure 1.1 Sorting a hand of cards using insertion sort.
INSERTION-SORT (A)
1 for j2 to length[A]
2 do keyA[j]
3Insert A[j] into the sorted sequence A[1 . . j - 1].
4 ij - 1
5 while i > 0 and A[i] > key
6 do A[i + 1]A[i]
7 ii - 1
8 A[i + 1]key
Figure 1.2 The operation of INSERTION-SORT on the array A =
5, 2, 4, 6, 1, 3
. The position of index j is indicated by a circle.
Pseudocode conventions
We use the following conventions in our pseudocode.
3. The symbol
indicates that the remainder of the line is a comment.
Sometimes, a pointer will refer to no object at all. In this case, we give it the special value NIL.
Exercises
Rewrite the INSERTION-SORT procedure to sort into nonincreasing instead of nondecreasing order.
Consider the searching problem:
Input: A sequence of n numbers A =
a1, a2, . . . ,an
and a value v.
Output: An index i such that v = A[i] or the special value NIL if v does not appear in A.
Write pseudocode for linear search, which scans through the sequence, looking for v.
1.2 Analyzing algorithms
Analysis of insertion sort
T(n) = c1n + c2 (n - 1) + c4 (n - 1) + c5 (n - 1) + c8 (n - 1)
= (c1 + c2 + c4 + c8)n - (c2 + c4 + c5 + c8).
Worst-case and average-case analysis
Order of growth
Exercises
Express the function n3/1000 - 100n2 - 100n + 3 in terms of
-notation.
How can we modify almost any algorithm to have a good best-case running time?
1.3 Designing algorithms
1.3.1 The divide-and-conquer approach
The divide-and-conquer paradigm involves three steps at each level of the recursion:
Divide the problem into a number of subproblems.
Combine the solutions to the subproblems into the solution for the original problem.
Divide: Divide the n-element sequence to be sorted into two subsequences of n/2 elements each.
Conquer: Sort the two subsequences recursively using merge sort.
Combine: Merge the two sorted subsequences to produce the sorted answer.
MERGE-SORT(A,p,r)
1 if p < r
2 then q![]()
(p + r)/2
3 MERGE-SORT(A,p,q)
4 MERGE-SORT(A, q + 1, r)
5 MERGE(A,p,q,r)
1.3.2 Analyzing divide-and-conquer algorithms
Figure 1.3 The operation of merge sort on the array A =
5, 2, 4, 6, 1, 3, 2, 6
. The lengths of the sorted sequences being merged increase as the algorithm progresses from bottom to top.
In Chapter 4, we shall see how to solve common recurrences of this form.
Analysis of merge sort
Exercises
Write pseudocode for MERGE(A,p,q,r).
Use mathematical induction to show that the solution of the recurrence
1.4 Summary
Exercises
Problems
1-1 Comparison of running times
1-2 Insertion sort on small arrays in merge sort
b. Show that the sublists can be merged in
(n lg(n/k)) worst-case time.
d. How should k be chosen in practice?
a. List the five inversions of the array
2, 3, 8, 6, 1
.
Chapter notes

2 to length[A]
Insert A[j] into the sorted sequence A[1 . . j - 1].


key in line 5 when i has its initial value of j - 1. Thus tj = 1 for j = 2,3, . . ., n, and the best-case running time is


The worst-case running time of an algorithm is an upper bound on the
running time for any input. Knowing it gives us a guarantee that the
algorithm will never take any longer. We need not make some educated
guess about the running time and hope that it never gets much worse.
. Describe a straightforward 
r, the subarray has at most one element and is therefore already sorted. Otherwise, the divide step simply computes an index q that partitions A[p. .r] into two subarrays: A[p. .q], containing n/2] elements, and A[q + 1. .r], containing
n/2
elements.4
x
denotes the least integer greater than or equal to x, and 




