Unit 5
Unit 5
Unit 5
INTRODUCTION TO ALGORITHMS
5.1 ALGORITHMS
Step 1: Start
Step 2: Read a,b,c
Step3: calculate disc=b*b-4*a*c
Step 4: if(disc>0)
Begin
Step 5:root1=(-b+sqrt(disc))/(2*a)
Step 6:root2=(-b-sqrt(disc))/(2*a)
Step7:Print―Root1‖, ―Root2‖
End
Step 8: else if(disc=0)Begin
Step 9:root1=-b/(2*a)
Step 10:root2=root1;
Step11:Print―Root1‖,―Root2‖
End Step 12: else
Step 13: Print Roots are imaginary
Step 14: Stop
Step 1: Start
Step 8: stop
Step 1: Start
Step 2: Declare variables n,i,flag.
Step 3: Initialize variables
flag←1
i←2
Step 4: Read n from user.
Step 5: Repeat the steps until i<(n/2)
5.1 If remainder of n÷i equals 0
flag←0
Go to step 6
5.2 i←i+1
Step 6: If flag=0
Display n is not prime
else
Display n is prime
Step 7: Stop
Searching:
Searching refers to an operation of finding the location of an item in a table or a file. Depending
on the location of the records to be searched, searching techniques are classified into two types.
External Searching:
When the records are stored in files, disk, tape any secondary storage, then the searching is
known as external searching.
Internal Searching:
When the records are stored in main memory, then it is known as internal searching. In C
language we have 2 types of searching techniques,
Repeat the procedure for lower half or upper half of array until the element is found.
Step 2: repeat thru step 4 while low is less than or equal to high i.e., while(low<=high) do
high = mid – 1
low = mid + 1
5.3 Sorting:
Sorting is a method of arranging data in a file in ascending or descending order. Sorting makes
handling of records in a file is easier.
Algorithm
1. begin
2. for i := 0 to array_size - 1 do
3. begin
4. for j := 0 array_size–1-i do
5. if a [j] > a [j+1] then
6. begin
7. temp := a [j];
8. a [j] := a[j+1];
9. a[j+1] := temp;
10. end
11. end
12. end
Bubble sort (to sort in ascending order) is performed as given below, Consider an array of
n elements a[0] to a[n-1]
In first pass, compare a[0] with a[1]. If a[0] > a[1], swap the numbers otherwise leave it.
In second pass, Compare a[1] with a[2] and swap the numbers if a[1] > a[2] and so on
and compare a[n-2] with a[n-1] and swap the number if a[n-2] > a[n-1].
By this process the first largest element placed in nth position. This element is not further
compared in the remaining passes.
Now consider first n-1 elements in the list and repeat the above process to place the next
largest element in the (n-1)th position. Repeat this process until all the elements are placed
in proper positions.
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
5.3.2 Insertion sort
#include<stdio.h>
#include<conio.h>
void insertion(int [], int );
int main()
{
int arr[30];
int i,size;
printf("Enter total no. of elements : ");
scanf("%d",&size);
printf(“\n Enter the elements to sort:”);
for(i=0; i<size; i++)
scanf("%d",&arr[i]);
insertion(arr,size);
printf("\nAfter sorting\n");
for(i=0; i<size; i++)
printf(" %d",arr[i]);
getch();
return 0;
}
void insertion(int arr[], int size)
{
int i,j,tmp;
for(i=0; i<size; i++)
{
for(j=i-1; j>=0; j--)
{
if(arr[j]>arr[j+1])
{
tmp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=tmp;
}
else
break;
}
}
}
5.3.3 Selection Sort
Suppose that items in a list are to be sorted in their correct sequence. Using Selection
Sort, the first item is compared with the remaining n-1 items, and whichever of all is
lowest, is put in the first position.
Then the second item from the list is taken and compared with the remaining (n-2) items,
if an item with a value less than that of the second item is found on the (n-2) items, it is
swapped (Interchanged) with the second item of the list and so on.
Algorithm for Selection Sort:
Procedure Selection_Sort(A,N)
For Index = 1 to N-1 do
begin
IndexMinPostion
For j = Index+1 to N do
begin if A[j] < A[MinPosition] then
jMinPosition
end swap (A[Index], A[MinPosition])
end
Return
/*program using Selection Sort*/
#include <stdio.h>
void selection_sort();
int a[30], n;
void main()
{
int i;
printf("\nEnter size of an array: ");
scanf("%d", &n);
printf("\nEnter elements of an array:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
selection_sort();
printf("\n\nAfter sorting:\n");
for(i=0; i<n; i++)
printf("\n%d", a[i]);
getch();
}
void selection_sort()
{
int i, j, min, temp;
for (i=0; i<n; i++)
{
min = i;
for (j=i+1; j<n; j++)
{
if (a[j] > a[min])
min = j;
}
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
TUTORIAL QUESTIONS
1. Write a program to explain bubble sort. Which type of technique does it belong. (b) What
is the worst case and best case time complexity of bubble sort?
2. Explain the algorithm for bubble sort and give a suitable example. (OR) Explain the
algorithm for exchange sort with a suitable example.
3. Write a program to explain insertion sort . Which type of technique does it belong. (or)
Write a C-program for sorting integers in ascending order using insertion sort.
4. Explain the algorithm for insertion sort and give a suitable example
5. Demonstrate the insertion sort results for each insertion for the following initial array of
elements . 25 6 15 12 8 34 9 18 2
6. Demonstrate the selection sort results for each pass for the following initial array of
elements . 21 6 3 57 13 9 14 18 2
ASSIGNMENT QUESTIONS
SET-1:
1. Design an algorithm and flow chart to find maximum among three numbers.
2. Implement a C program to find roots of quadratic equation
SET-2:
1. Design an algorithm and flow chart to check given number is positive or negative
or zero
2. Implement a C program to check given number is prime or not.
SET-3:
SET-4:
1. Implement a C program to check given number is perfect number or not.
2. Explain selection sort with example.
SET-5:
1. Implement a C program to search a key using binary search.
2. Illustrate the complexity of bubble sort , selection sort and insertion sort.
IMPORTANT QUESTIONS
1. What is a flowchart? Explain the different symbols used in a flowchart.
2. (a) Define an Algorithm?
(b)What is the use of Flowchart?
(c) What are the different steps followed in the program development?
1. Write a program to explain selection sort. Which type of technique does it belong.
2. Explain the algorithm for selection sort and give a suitable example.
3. Formulate recursive algorithm for binary search with its timing analysis
4. Write a C program that searches a value in a stored array using linear search.
5. Implement a C program to find prime number from 1 to n.
6. Implement Fibonacci series using recursion
7. WACP to sort elements using insertion sort.
8. Compute and analysis time and space complexity.
.
OBJECTIVE QUESTIONS
3). Selection sort first finds the ………. element in the list and put it in the first position.
A. Middle element
B. Largest element
C. Last element
D. Smallest element
a) Insertion sort
b) Selection sort
c) Bubble sort
d) Merge sort