Chapter 2 Itterative Algorithms
Chapter 2 Itterative Algorithms
Contents
Algorithm For GCD(a,b) ....................................................................................................................... 2
Sorting .................................................................................................................................................... 4
1. Start
2. if a=0
Gcd=b
Goto step 6
3. if b=0
Gcd=a
Goto step 6
4. otherwise,
r=a mod b
a=b
b=r
5. Goto Step 2
6. Stop
Example:
Find GCD of 270 and 192
I. a=270, b=192
r=78
II. a=192, b=78
r=36
III. a=78, b=36
r=6
IV. a=36, b=6
r=0
V. a=6, b=0
Gcd=6
Analysis:
So, the number of steps will always be less than O(log n), where n is the smaller of our two input
values. Therefore, O(log n) is an upper bound on the worst-case cost of our algorithm.
1. start
2. firstterm=0
3. secondterm=1
4. nextterm=firstterm+secondterm
5. for i=3 to n
6. firstterm=secondterm
7. secondterm=nextterm
8. nextterm=firstterm+secondterm
9. stop
Analysis
T(n)= O(n)
Since, the loop executes almost n times.
1. start
2. for i=0 to n-1
3. if arr[i] = key
4. print key found at ith index
5. print key not found
6. stop
Sorting
• Sorting refers to the operation or technique of arranging and rearranging sets of data in some
specific order.
• Sorting refers to arranging data in a particular format. Sorting algorithm specifies the way to
arrange data in a particular order.
Types of Sorting
• The techniques of sorting can be divided into two categories. These are:
o Internal Sorting
o External Sorting
• Internal Sorting: If all the data that is to be sorted can be adjusted at a time in the main memory,
the internal sorting method is being performed.
• External Sorting: When the data that is to be sorted cannot be accommodated in the memory at
the same time and some has to be kept in auxiliary memory such as hard disk, floppy disk,
magnetic tapes etc, then external sorting methods are performed.
Insertion sort
• It is a simple sorting algorithm that works similar to the way you sort playing cards in your
hands. The array is virtually split into a sorted and an unsorted part. Values from the unsorted
part are picked and placed at the correct position in the sorted part.
Algorithm
To sort an array of size n in ascending order:
1. Iterate from arr[1] to arr[n] over the array.
2. Compare the current element (key) to its predecessor.
3. If the key element is smaller than its predecessor, compare it to the elements before. Move
the greater elements one position up to make space for the swapped element.
insertionSort(arr,n)
1. for i = 1 to n-1
2. key = arr[i]
3. j=i-1
4. while j >= 0 AND arr[j] > key
5. arr[j + 1] = arr[j]
6. j=j-1
7. arr[j + 1] = key
Selection Sort
• The selection sort algorithm sorts an array by repeatedly finding the minimum element
(considering ascending order) from unsorted part and putting it at the beginning. The algorithm
maintains two subarrays in a given array.
1. The subarray which is already sorted.
2. Remaining subarray which is unsorted.
• In every iteration of selection sort, the minimum element (considering ascending order) from
the unsorted subarray is picked and moved to the sorted subarray.
selectionSort(arr,n)
1. for i = 0 to n-2
2. min_idx = i;
3. for j = i+1 to n-1
4. if arr[j] < arr[min_idx]
5. min_idx = j
6. swap(arr[min_idx], arr[i])
Exchange sort
• Bubble sort
Bubble sort
• Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent
elements if they are in wrong order.
• Bubble sort is an algorithm that compares the adjacent elements and swaps their positions if
they are not in the intended order.
Algorithm
bubbleSort(arr,n)
1. for pass=1 to n-1
2. issort=1
3. for i = 0 to n-pass-1
4. if arr[i] > arr[i+1]
5. temp = arr[i]
6. arr[i] = arr[i+1]
7. arr[i+1] = temp
8. issort=0
9. if issort=1
10. break
pass=1
pass=2
pass=3
pass=4