1
1
Preliminaries of algorithm:
Data Structures
Data structure is a collection of organized data that are related to each other.
Data structures can be classified into two types:
10 32 45 54 60
Fig: Tree
B C
D E F G
Recursion
#include <stdio.h>
main()
{
int fact(int n); /* function declaration*/
int n,m;
clrscr();
printf("enter n value");
scanf("%d", &n);
m=fact(n); /* function calling by main */
printf("factorial of a %d is %d",n,m);
getch();
}
fact(int n)
{
if(n<=1)
return 1;
else
return n*fact(n-1); /* function call by itself*/
}
Linear Recursion:
In linear recursion a function calls exactly once to itself each time the function is
invoked, and grows linearly in proportion to the size of the problem.
Example: Factorial of a given number using recursion
Factorial (int n)
{
if (n <= 1)
return 1;
else
return n * Factorial(n – 1);
}
Binary Recursion:
The binary recursion has the possible for calling itself twice instead of once as
linear recursion. This is more useful in such situation as binary trees as well as the
Fibonacci sequence.
Example: To find the nth Fibonacci number in the series using recursion
Fib (int n)
{
if (n <= 1)
return n;
else
return (fib(n-1) + fib(n-2));
}
fact(int n)
{
if (n <= 1)
return 1;
else
return (n * fact(n-1));
}
(N not equal to 0)
The Fibonacci sequence can be defined as each number in the series is the sum of the
previous two numbers and the series is starting from zero and one.
The first few numbers in the Fibonacci series are
0,1,1,2,3,5,8,13,21,34……………….
We can generate the mathematical definition of the Fibonacci series as follows:
The recursive algorithm for Fibonacci sequence using pseudocode:
Algorithm fib(n)
1. if (n equals to 0 or n equals to 1)
1.1 return n
2. else
2.1 return fib (n-1) + fib (n-2);
3. end if
4. end fib.
In C-Language representation for the above pseudocode is
fib(int n)
{
if (n==0||n==1)
return n;
else
return (fib(n-1) + fib(n-2));
}
Here fib() is the recursive function.
This combines results from 2 different recursive calls. This is sometimes known as
"deep" recursion, or in other cases "divide and conquer."
Let's consider all the recursive calls for fib(5).
Tail Recursion:
factorial(n)
{
return fact(n, 1);
}
fact(n, accumulator)
{
if (n == 0)
return accumulator;
return fact(n - 1, n * accumulator);
}
The inner function fact () calls itself last executable statement in the function
Significance of Tail-Recursion
The significance of tail recursion is that when making a tail-recursive call, the
caller's return position need not be saved on the call stack; when the recursive call
returns, it will branch directly on the previously saved return position. Therefore, on
compilers that support tail-recursion optimization, tail recursion saves both space and
time.
Linear Search
For a list with n items, the best case is when the value is equal to the first element of
the list, in which case only one comparison is needed.
The worst case is when the value is not in the list (or occurs only once at the
end of the list), in which case n comparisons are needed.
Binary Search
Fibonacci Search
Internal Sorting:
If all the data that is to be sorted can be accommodated at a time in memory is
called internal sorting.
External Sorting:
INSERTION SORT
It is a very simple sorting algorithm, in which the sorted arrays built one
element at a time.
The main idea behind insertion sort is that it inserts each item into its proper
place in the final list.
Features:
Sorted by considering one item at a time.
Efficient to use on small sets of data.
Twice as fast as the bubble sort.
40% faster than the selection sort.
No swapping is required.
It is said to be online sorting because it continues the sorting a list as and when
it receives new elements.
It does not change the relative order of elements with equal keys.
Reduces unnecessary travel through the array.
Requires low and constant amount of extra memory space.
Less efficient for larger lists.
The array of values to be sorted is divided into two sets. One that stores sorted
values and the other contains unsorted values.
The sorting algorithm will proceed until there are elements in the unsorted set.
Suppose there are n elements in the array. Initially the element with index
0(Lower Bound = 0) is in the sorted set, rest all the elements are in the
unsorted set.
the first element of the unsorted partition has array index 1 (if LB = 0)
During each iteration of the algorithm, the first element in the unsorted set is
picked up and inserted into the correct position in the sorted list.
Example:
Insertion_Sort ( A [ ] , N )
Step 1 : Repeat For K = 1 to N – 1
Begin
Step 2 : Set Temp = A [ K ]
Step 3 : Set J = K – 1
Step 4 :Repeat while Temp < A [ J ] AND J >= 0
Begin
Set A [ J + 1 ] = A [ J ]
ALGORITHM: Set J = J - 1
End While
Step 5 : Set A [ J + 1 ] = Temp
End For
Step 4 : Exit
Complexity of Insertion Sort
Best Case :O(n)
Average Case : O ( n2 )
Worst Case : O ( n2 )
SELECTION SORT
Advantages:
Disadvantages:
Algorithm:
Selection_Sort ( A [ ] , N )
Step 1 : Repeat For K = 0 to N – 2
Begin
Step 2 : Set POS = K
Step 3 : Repeat for J = K + 1 to N – 1
Begin
If A[ J ] < A [ POS ]
Set POS = J
End For
Step 5 : Swap A [ K ] with A [ POS ]
End For
Step 6 : Exit
Complexity of Selection Sort
BUBBLE SORT
It is a very simple method that sorts the array elements by repeatedly moving
the largest element to the highest index position of the array. In bubble sorting,
consecutive adjacent pairs of elements in the array are compared with each other. If
the element lower index is greater than the element at the higher index, the two
elements are interchanged so that the smaller element is placed before the bigger
one. This procedure of sorting is called bubble sorting because the smaller elements
“bubble” to the top of the list.
Features:
Example:
Let us an array that has the following elements
A[ ] = { 23,19,54,12,47,10};
Pass 1:
Pass 2: 19,23,12,47,10,54
Pass 3: 19,12,23,10,47,54
Pass 4: 12,19,10,23,47,54
ALGORITHM:
Bubble_Sort ( A [ ] , N )
Step 1 : Repeat For P = 1 to N – 1
Begin
Step 2 : Repeat For J = 1 to N – P
Begin
Step 3 : If ( A [ J ] < A [ J – 1 ] )
Swap ( A [ J ] , A [ J – 1 ] )
End For
End For
Step 4 : Exit
Complexity of Bubble Sort
QUICK SORT:
Example:
ALGORITHM:
MERGE SORT:
It is a sorting algorithm that uses the divide, conquer and combine algorithmic
paradigm. Where,
Divide means partitioning the n-element array to be sorted into two sub-
arrays of n/2 elements in each sub-array.(If A is an array containing zero
or one element, then it is already sorted. However, if there are more
elements in the array, divide A into two sub-arrays, A1 and A2, each
containing about half of the elements of A).
Conquer means sorting the two sub-arrays recursively using merge sort.
Combine means merging the two sorted sub-arrays of size n/2 each to
produce the sorted array of n elements.
Merge sort technique sorts a given set of values by combining two sorted
arrays into one larger sorted arrays.
A small list will take fewer steps to sort than a large list.
Fewer steps are required to construct a sorted list from two sorted lists
than two unsorted lists.
You only have to traverse each list once if they're already sorted .
Example: Sort the array given below using the merge sort.
39 9 81 45 90 27 72 18
ALGORITHM:
Radix sort:
Each key is first figuratively dropped into one level of buckets corresponding to
the value of the rightmost digit. Each bucket preserves the original order of the keys
as the keys are dropped into the bucket. There is a one-to-one correspondence
between the number of buckets and the number of values that can be represented by
a digit. Then, the process repeats with the next neighboring digit until there are no
more digits to process. In other words:
Take the least significant digit (or group of bits, both being examples of
radices) of each key.
Group the keys based on that digit, but otherwise keep the original order
of keys. (This is what makes the LSD radix sort a stable sort).
Repeat the grouping process with each more significant digit.
The sort in step 2 is usually done using bucket sort or counting sort, which are
efficient in this case since there are usually only a small number of digits.
Finally the list is sorted. The radix sorted list is 3 9 38 67 123 478 537 721.
ALGORITHM: