0% found this document useful (0 votes)
121 views20 pages

Quadratic Equation Root Algorithm

Uploaded by

Sugnanarao M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views20 pages

Quadratic Equation Root Algorithm

Uploaded by

Sugnanarao M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

UNIT-V

Introduction to Algorithms

An algorithm is a step by step procedure to solve a problem. In normal language, the algorithm is
defined as a sequence of statements which are used to perform a task. In computer science, an
algorithm can be defined as follows...

Algorithms are used to convert our problem solution into step by step statements. These
statements can be converted into computer programming instructions which form a program.
This program is executed by a computer to produce a solution. Here, the program takes required
data as input, processes data according to the program instructions and finally produces a result
as shown in the following picture.

Algorithm for finding roots of a quadratic equations

A quadratic equation is (ax2+bx+c=0)

An equation is quadratic only if a is non zero.

If a is zero and b is non zero in the above equation then it becomes a linear equation (bx + c =
0).

If a and b are zeros then the it becomes a constant equation.

Nature of roots of quadratic equation can be known from the quadrant  = b2-4ac

If b2-4ac >0 then roots are real and unequal

If b2-4ac =0 then roots are real and equal

If b2-4ac <0 then roots are imaginary

Algorithm:
Step 1: start

Step 2: read the a,b,c value

Step 3: if(a==0 && b==0 && c==0) then

Print Invalid coefficients and enter valid inputs

Step 4: if(a==0) then

Print Linear equation

Print Root is -c/b

Step 5: if b*b-4ac>0 then

Root 1= (-b+ pow((b*b-4*a*c),0.5))/2*a

Root 2= (-b-pow((b*b-4*a*c),0.5))/2*a

Step 6: if b*b-4ac=0 then

Root1 = Root2 = -b/(2*a)

Step 7: if b*b-4*a*c<0

Print Imaginary roots.

real=-b/(2*a);

imag=sqrt(fabs(d))/(2*a);

root1=real+i imag

root2=real-i imag

Step 8: print roots

Step 9: stop

Algorithm for finding minimum and maximum numbers of a given set:

Step 1: start

Step 2: read no of elements of list(n)

Step 3: initialize i=0

Step 4: if i<n do as follows. If not goto step 5


Read a[i]

Increment i

Goto step 4

Step 5: min=a[0], max=a[0]

Step 6: initialize i=0

Step 7: if i<n do as follows. If not goto step 8

If a[i]<min

Assign min=a[i]

If a[i]>max

Assign max=a[i]

Increment i goto Step 7

Step 8: print min,max

Step 9: stop

Algorithm for finding if a number is prime number:

Step 1: Start
Step 2: Read number n
Step 3: Set f=0
Step 4: For i=2 to n-1
Step 5: If n mod i=0 then
Step 6: Set f=1 and break
Step 7: Loop
Step 8: If f=0 then
print 'The given number is prime'
else
print 'The given number is not prime'
Step 9: Stop

Basic searching in an array of elements (linear and binary search techniques):

Search is a process of finding a value in a list of values. In other words, searching is the process
of locating given value position in a list of values.
Linear Search:

Linear search is a very simple search algorithm. In this type of search, a sequential search is
made over all items one by one. Every item is checked and if a match is found then that
particular item is returned, otherwise the search continues till the end of the data collection.

Step 1 –Start

Step 2 – Read the number of elements of list

Step 3 – Read the elements of list using array

For(i=0;i<n;i++)

Read a[i]

Step 4 - Read the search element from the user.

Step 5 - Compare the search element with the first element in the list.If both are matched,
then display "Given element is found!!!" and terminate the function. If both are not
matched, then compare search element with the next element in the list. Repeat until
search element is compared with last element in the list. If last element in the list also
doesn't match, then display "Element is not found!!!" and terminate the function.

Step 6: for (i = 0; i < n; i++)


{
if (arr[i] == x)
{
Flag=1
Break
}
}
Step 7: if(flag==1)
Print element found at location i+1
Else
Print element not found

Step 8: stop

Binary Search:

 Step 1 - Read the search element from the user.


 Step 2 - Find the middle element in the sorted list.
 Step 3 - Compare the search element with the middle element in the sorted list.
 Step 4 - If both are matched, then display "Given element is found!!!" and terminate the
function.
 Step 5 - If both are not matched, then check whether the search element is smaller or
larger than the middle element.
 Step 6 - If the search element is smaller than middle element, repeat steps 2, 3, 4 and 5
for the left sublist of the middle element.
 Step 7 - If the search element is larger than middle element, repeat steps 2, 3, 4 and 5 for
the right sublist of the middle element.
 Step 8 - Repeat the same process until we find the search element in the list or until
sublist contains only one element.
 Step 9 - If that element also doesn't match with the search element, then display
"Element is not found in the list!!!" and terminate the function.

int binarySearch(int arr[], int l, int r, int x)


{
while (l <= r) {
int m = l + (r - l) / 2;

// Check if x is present at mid


if (arr[m] == x)
return m;

// If x greater, ignore left half


if (arr[m] < x)
l = m + 1;

// If x is smaller, ignore right half


else
r = m - 1;
}

// if we reach here, then element was


// not present
return -1;
}

int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n - 1, x);
(result == -1) ? printf("Element is not present"
" in array")
: printf("Element is present at "
"index %d",
result);
return 0;
}

Basic algorithms to sort array of elements (Bubble, Insertion and Selection sort
algorithms):

Bubble sort:

 Is a straightforward and simplistic method of sorting data.


 The algorithm starts at the beginning of the data set.
 It compares the first two elements, and if the first is greater than the second, it
swaps them.
 It continues doing this for each pair of adjacent elements to the end of the data set.
 It then starts again with the first two elements.
 Repeating until no swaps have occurred on the last pass.
 While simple, this algorithm is highly inefficient and is rarely used except in
education.

Complexity of bubble sort: Best case :O(n)


Average case
Worst case : O(n2)
C program for bubble sort

#include<stdio.h>

#include<conio.h>

void bubble(int a[20],int n);

void main()

int i,a[20],n;

clrscr();

printf("enter array size");

scanf("%d",&n);

printf("enter elements one by one");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

bubble(a,n);
printf("after sorting\n");

for(i=0;i<n;i++)

printf("%d\t",a[i]);

getch();

void bubble(int a[20],int n)

int i,j,temp;

for(i=0;i<n-1;i++)

for(j=0;j<n;j++)

if(a[i]<a[j])

temp=a[i];

a[i]=a[j];

a[j]=temp;

Complexity of Bubble Sort

The complexity of sorting algorithm is depends upon the number of comparisons that are made.

Total comparisons in Bubble sort is

n ( n – 1) / 2 ≈ n 2 – n
Complexity = O ( n 2 )

Selection sort

 Selection sort is a sorting algorithm, specifically an in-place comparison sort.


 It has O(n2) time complexity, making it inefficient on large lists
 Generally performs worse than the similar insertion sort.
 Selection sort is noted for its simplicity, and also has performance advantages over more
complicated algorithms in certain situations, particularly where auxiliary memory is
limited.

The algorithm works as follows:

1. Find the minimum value in the list


2. Swap it with the value in the first position
3. Repeat the steps above for the remainder of the list (starting at the second position and
advancing each time)

Example:
Flowchart

Program for selection sort:


void main()

int a[20],min,temp,i,j,n;

clrscr();

printf("\n enter no of elements of list\n");

scanf("%d",&n);

for(i=0;i<n;i++)

scanf("%d",&a[i]);

for(i=0;i<n-1;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;

printf("\n array after sorting");


for(i=0;i<n;i++)

printf("%d\n",a[i]);

getch();

Complexity of Selection Sort

Best Case : O ( n2 )

Average Case : O ( n2 )

Worst Case : O ( n2 )

Insertion sort:

 Insertion sort is a simple sorting algorithm: a comparison sort in which the sorted array
(or list) is built one entry at a time.
 It is much less efficient on large lists than more advanced algorithms such as quicksort,
heapsort, or merge sort.

However, insertion sort provides several advantages:

 Simple implementation
 Efficient for (quite) small data sets
 Adaptive (i.e., efficient) for data sets that are already substantially sorted: the time
complexity is O(n + d), where d is the number of inversions
 More efficient in practice than most other simple quadratic (i.e., O(n2)) algorithms such
as selection sort or bubble sort; the best case (nearly sorted input) is O(n)
 Stable; i.e., does not change the relative order of elements with equal keys
 In-place; i.e., only requires a constant amount O(1) of additional memory space
 Online; i.e., can sort a list as it receives it
 Insertion sort is an example of an incremental algorithm;

It works the way you might sort a hand of playing cards:


1. We start with an empty left hand [sorted array] and the cards face down on the table
[unsorted array].
2. Then remove one card [key] at a time from the table [unsorted array], and insert it into
the correct position in the left hand [sorted array].
3. To find the correct position for the card, we compare it with each of the cards already in
the hand, from right to left.

Example:
Program for insertion sort

void main()

int a[20],index,i,j,n;

clrscr();

printf("\n enter no of elements of list\n");

scanf("%d",&n);

for(i=0;i<n;i++)

printf("\nenter element of a[%d]",i);

scanf("%d",&a[i]);

for(i=1;i<n;i++)

index=a[i];

j=i;

while((j>0) && (a[j-1]>index))

a[j]=a[j-1];

j=j-1;

a[j]=index;

printf("\n array after sorting");

for(i=0;i<n;i++)
printf("%d\n",a[i]);

getch();

Complexity of Insertion Sort

Best Case : O ( n )

Average Case : O ( n2 )

Worst Case : O ( n2 )

Basic concept of order of complexity through the example programs

Performance analysis of an algorithm is performed by using the following measures...

1. Space required to complete the task of that algorithm (Space Complexity). It includes
program space and data space
2. Time required to complete the task of that algorithm (Time Complexity)

What is Space complexity?


When we design an algorithm to solve a problem, it needs some computer memory to complete
its execution. For any algorithm, memory is required for the following purposes...

1. To store program instructions.


2. To store constant values.
3. To store variable values.
4. And for few other things like funcion calls, jumping statements etc,.

Space complexity of an algorithm can be defined as follows...


Example 1:
int square(int a)
{
return a*a;
}
In the above piece of code, it requires 2 bytes of memory to store variable 'a' and another 2 bytes
of memory is used for return value.
That means, totally it requires 4 bytes of memory to complete its execution. And this 4
bytes of memory is fixed for any input value of 'a'. This space complexity is said to
be Constant Space Complexity.
Example 2:

int sum(int A[ ], int n)


{
int sum = 0, i;
for(i = 0; i < n; i++)
sum = sum + A[i];
return sum;
}

In the above piece of code it requires


'n*2' bytes of memory to store array variable 'a[ ]'
2 bytes of memory for integer parameter 'n'
4 bytes of memory for local integer variables 'sum' and 'i' (2 bytes each)
2 bytes of memory for return value.

That means, totally it requires '2n+8' bytes of memory to complete its execution. Here, the
total amount of memory required depends on the value of 'n'. As 'n' value increases the
space required also increases proportionately. This type of space complexity is said to
be Linear Space Complexity.

What is Time complexity?


Every algorithm requires some amount of computer time to execute its instruction to perform the
task. This computer time required is called time complexity.
The time complexity of an algorithm can be defined as follows...
The time complexity of an algorithm is the total amount of time required by an algorithm
to complete its execution.
Generally, the running time of an algorithm depends upon the following...

1. Whether it is running on Single processor machine or Multi processor machine.


2. Whether it is a 32 bit machine or 64 bit machine.
3. Read and Write speed of the machine.
4. The amount of time required by an algorithm to
perform Arithmetic operations, logical operations, return value
and assignment operations etc.,
5. Input data

To calculate the time complexity of an algorithm, we need to define a model machine. Let us
assume a machine with following configuration...

1. It is a Single processor machine


2. It is a 32 bit Operating System machine
3. It performs sequential execution
4. It requires 1 unit of time for Arithmetic and Logical operations
5. It requires 1 unit of time for Assignment and Return value
6. It requires 1 unit of time for Read and Write operations

Now, we calculate the time complexity of following example code by using the above-defined
model machine...
int sum(int a, int b)
{
return a+b;
}
In the above sample code, it requires 1 unit of time to calculate a+b and 1 unit of time to return
the value. That means, totally it takes 2 units of time to complete its execution. And it does not
change based on the input values of a and b. That means for all input values, it requires the same
amount of time i.e. 2 units.
If any program requires a fixed amount of time for all input values then its time complexity
is said to be Constant Time Complexity.
Example 2:

int sum(int A[], int n)


{
int sum = 0, i;
for(i = 0; i < n; i++)
sum = sum + A[i];
return sum;
}
In above calculation
Cost is the amount of computer time required for a single operation in each line.
Repetition is the amount of computer time required by each operation for all its repetitions.
Total is the amount of computer time required by each operation to execute.
So above code requires '4n+4' Units of computer time to complete the task. Here the exact time
is not fixed. And it changes based on the n value. If we increase the n value then the time
required also increases linearly.

Totally it takes '4n+4' units of time to complete its execution and it is Linear Time
Complexity.

If the amount of time required by an algorithm is increased with the increase of input value
then that time complexity is said to be Linear Time Complexity.

You might also like