0% found this document useful (0 votes)
69 views23 pages

Algorithms for Factorial, Sorting, and Searching

The document outlines algorithms for calculating the factorial of a number, checking if a number is even or odd, determining if a number is prime, and finding the roots of a quadratic equation. It also discusses various searching algorithms like linear and binary search, as well as sorting algorithms including bubble sort, insertion sort, and selection sort, highlighting their characteristics and differences. Additionally, it provides examples and pseudocode for each algorithm discussed.

Uploaded by

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

Algorithms for Factorial, Sorting, and Searching

The document outlines algorithms for calculating the factorial of a number, checking if a number is even or odd, determining if a number is prime, and finding the roots of a quadratic equation. It also discusses various searching algorithms like linear and binary search, as well as sorting algorithms including bubble sort, insertion sort, and selection sort, highlighting their characteristics and differences. Additionally, it provides examples and pseudocode for each algorithm discussed.

Uploaded by

Neha Cheemarla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Algorithm for factorial of given number:

Factorial of a positive integer n is product of all values from n to 1. For


example, the factorial of 3 is (3 * 2 * 1 = 6).

Algorithm

Algorithm of this program is very easy −


START
Step 1 → Take integer variable A
Step 2 → Assign value to the variable
Step 3 → From value A upto 1 multiply each digit and store
Step 4 → the final stored value is factorial of A
STOP
Algorithm to check number is even or odd

Algorithm to check number is prime or not:


A number that is divisible only by 1 and itself is called a prime number..
For example −
7=1×7
Few prime number are − 1, 2, 3, 5 , 7, 11 etc.
Algorithm:
Algorithm, flowchart and C program to find the roots of a
quadratic equation:

Analysis

Input − a,b,c values


Output − r1, r2 values

Algorithm:

 Start
 Read a, b, c values
 Compute d = b*b-4ac
 if d > 0 then
o r1 = -b+ sqrt(d)/(2*a)
o r2 = -b-sqrt(d)/(2*a)
 Otherwise if d = 0 then
o compute r1 = -b/2a, r2=-b/2a
o print r1,r2 values
 Otherwise if d < 0 then print roots are imaginary
 Stop
Program:

# include<stdio.h>
# include<conio.h>
# include<math.h>
main (){
float a,b,c,r1,r2,d;
printf (“enter the values of a b c”);
scanf (“ %f %f %f”, &a, &b, &c);
d= b*b – 4*a*c;
if (d>0)
{
r1 = -b+sqrt (d) / (2*a);
r2 = -b-sqrt (d) / (2*a);
printf (“The real roots = %f %f”, r1, r2);
}
else if (d= =0)
{
r1 = -b/(2*a);
r2 = -b/(2*a);
printf (“roots are equal =%f %f”, r1, r2);
}
else
printf(“Roots are imaginary”);
getch ();
}

Testing:

Case 1: enter the values of a b c: 1 4 3


r1 = -1
r2 = -3
Algorithm for find minimum and maximum element in given list
Various complexities

Asymptotic notations
DIVIDE AND CONQUER STRATEGY
Searching techniques
Searching refers to the process of finding a desired element in set of
items. The desired element is called "target".
Searching Algorithms :
1. Linear Search
2. Binary Search

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.

Example:
Algorithm:
Linear Search ( Array Arr, Value a ) // Arr is the name of the array, and a
is the searched element.
Step 1: Set i to 0 // i is the index of an array which starts from 0
Step 2: if i > n then go to step 7 // n is the number of elements in array
Step 3: if Arr[i] = a then go to step 6
Step 4: Set i to i + 1
Step 5: Goto step 2
Step 6: Print element a found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit

Binary search
Difference between linear and binary search
Sorting
A sorting algorithm is used to arrange elements of an array/list in a
specific order. For example,
Sorting an
array
Here, we are sorting the array in ascending order.
There are various sorting algorithms that can be used to complete this
operation. And, we can use any algorithm based on the requirement.

Bubble Sort Algorithm:


Bubble Sort is the simplest sorting algorithm that works by repeatedly
swapping the adjacent elements if they are in the wrong order. This
algorithm is not suitable for large data sets as its average and worst-
case time complexity is quite high.

Algorithm:

In the algorithm given below, suppose arr is an array of n elements. The


assumed swap function in the algorithm will swap the values of given
array elements.

1. begin BubbleSort(arr)
2. for all array elements
3. if arr[i] > arr[i+1]
4. swap(arr[i], arr[i+1])
5. end if
6. end for
7. return arr
8. end BubbleSort
Insertion sort :
Insertion sort 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.

Characteristics of Insertion Sort:


 This algorithm is one of the simplest algorithm with simple
implementation
 Basically, Insertion sort is efficient for small data values
 Insertion sort is adaptive in nature, i.e. it is appropriate for data sets
which are already partially sorted.
 Algorithm
 // Sort an arr[] of size n
 insertionSort(arr, n)
 Loop from i = 1 to n-1.
 a) Pick element arr[i] and insert it into sorted sequence arr[0 1
2 ..i-1]
 Example:

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 sub arrays in a
given array.
 The subarray which is already sorted.
 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.

Selection sort is generally used when -

o A small array is to be sorted


o Swapping cost doesn't matter
o It is compulsory to check all elements
o
o ALGORITHM:
o Step 1 − Set MIN to location 0
o Step 2 − Search the minimum element in the list
o Step 3 − Swap with value at location MIN
o Step 4 − Increment MIN to point to next element
o Step 5 − Repeat until list is sorted

EXAMPLE :

DIFFERENCE BETWEEN INSERTION SORT AND SELECTION SORT

Insertion Sort Selection Sort


Finds the minimum / maximum
Inserts the value in the presorted array to sort the set number from the list and sort it
1. of values in the array. in ascending / descending order.

It is an unstable sorting
2. It is a stable sorting algorithm. algorithm.

The best-case time complexity is Ω(N) when the For best case, worst case and
array is already in ascending order. It have Θ(N2) in average selection sort have
3. worst case and average case. complexity Θ(N2).

The number of comparison operations performed in The number of comparison


this sorting algorithm is less than the swapping operations performed in this
4. performed. sorting algorithm is more than
Insertion Sort Selection Sort
the swapping performed.

It is less efficient than the


5. It is more efficient than the Selection sort. Insertion sort.

The location where to put the


element is previously known we
Here the element is known beforehand, and we search for the element to insert
6. search for the correct position to place them. at that position.

The selection sort is used when


 A small list is to be sorted
 The cost of swapping does not
matter
 Checking of all the elements is
compulsory
 Cost of writing to memory
matters like in flash memory
The insertion sort is used when: (number of Swaps is O(n) as
 The array is has a small number of elements compared to O(n2) of bubble
7.  There are only a few elements left to be sorted sort)

The insertion sort is Adaptive, i.e., efficient for data


sets that are already substantially sorted: the time
complexity is O(kn) when each element in the input
is no more than k places away from its sorted Selection sort is an in-place
8. position comparison sorting algorithm

You might also like