design and algorithm analysis – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sun, 10 Mar 2024 17:00:27 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg design and algorithm analysis – Programmerbay https://programmerbay.com 32 32 C Program to Sort an array using Selection Sort in Ascending Order https://programmerbay.com/program-to-sort-an-array-using-selection-sort/ https://programmerbay.com/program-to-sort-an-array-using-selection-sort/#respond Sun, 03 Mar 2024 17:20:38 +0000 https://www.programmerbay.com/?p=2879 The program sorts uses Selection Sort technique to sort the given array. There are bunch of sorting mechanisms such as Bubble sort, Insertion sort and more to achieve this functionality. In this, we’ll be only focusing Selection Sort .

Selection Sort

Selection sort is a sorting technique wherein at each iteration, a minimum element is selected from an unsorted array and compared with all other remaining elements. if an element is found that is smaller than already assumed minimum element,  swapping would be performed.

It divides an array into two parts. One that is already sorted and another one which is still unsorted that need to be arranged.

From unsorted sublist, the algorithm finds the minimum element and adds on to sorted category. It ends when we get a single consolidated sorted sublist.

Pseudocode

FOR i=0 to Array.Length
minimum = i
FOR j=0 to Array.Length
if A[min]< A[j])
Swap A[min] with A[j]
End IF
END FOR
END FOR

C program to sort an array using Selection Sort in Ascending Order

#include<conio.h>
#include<stdio.h>
#define size 5
void main()
{
int arr[size],i,j,temp,min;
printf("Please enter elements: \n "); 
for(i=0;i<=size-1;i++)
{
scanf("%d",&arr[i]);
}

for(i=0;i<=size-1;i++)
{
min= i;
for(j=0;j<=size-1;j++)
{
if(arr[min]<arr[j]){
temp = arr[j];
arr[j] =arr[min];
arr[min] = temp;
}
}
}

printf(" \n Entered Elements :");

for(i=0;i<=size-1;i++)
{
printf("\n %d ",arr[i]);
} 
getch();

}

Output:

Screenshot from 2024 03 10 22 22 03

 

How does Selection Sort work?

Screenshot from 2024 03 10 22 28 33

 

Lastly, it is an inefficient algorithm for the large dataset as it requires O(n2) comparisons in all cases worst, average and best case. It is simple and occasionally has an upper hand over other complicated algorithms.

]]>
https://programmerbay.com/program-to-sort-an-array-using-selection-sort/feed/ 0
How to Calculate AVL Tree Balance Factor ? https://programmerbay.com/avl-tree-a-height-balancing-bst/ https://programmerbay.com/avl-tree-a-height-balancing-bst/#respond Wed, 15 Jun 2022 17:28:28 +0000 https://www.programmerbay.com/?p=2963  

In Data Structures, AVL tree (Adelson-Velsky and Landis tree)  is a height-balanced binary search tree in which difference between the heights of left subtree and right subtree can be atmost 1. It is a binary search tree where each node associated with a balance factor. If balance factor paired with node is either 1,0, or – 1, it is said to be balanced.

AVL Tree example

Balance factor = height of left subtree – height of right subtree

It is important for a binary search tree to be balanced so that insertion and deletion require less search time, resulting increased efficiency. It requires O(log n) time to perform deletion, insertion and search operation.

AVL Tree Basic Operations

Search operations

A search operation starts from the root node to all the way downward recursively. The targeted element is compared with the root node, if both are equal then the search would get terminated. If it is smaller than the root node then the search goes on at left subtree. And if the targeted value is greater, then search operation would be performed left subtree recursively.

Insert operation

To insert a given element, AVL first performs search operation, starting from the root node in order to locate the insertion position. Ones a NIL location is found then insertion is performed.

Inserting a new node may increase the chances of AVL tree to get unbalanced, therefore, rotation is performed with the every insertion if required .

AVL Tree Rotations

It is a process of balancing the AVL tree when balance factor is greater than 1 or smaller than -1.
These are the following types of rotation:

Left-Left Rotation
When insertion is performed in the left subtree of the left subtree then LL imbalance occurs and the rotation process perform on this case is therefore known as left left rotation. It is a single rotation.

LL Rotation AVL Tree

Right-Right Rotation
When insertion is performed in the right subtree of the right subtree then RR imbalance occurs and the rotation process performed in this case is therefore known as Right-Right rotation. It is a single rotation.

RR Rotation AVL tree

Left Right Rotation
When insertion is performed in the right subtree of the left subtree then LR imbalance occurs and the rotation process performed in this case is therefore known as Left-Right. It is double rotation.

LR rotation in AVL TREE

Right Left Rotation
When insertion is performed in the left subtree of the right subtree then RL imbalance occurs and the rotation process performed in this case is therefore known as Right-Left. It is double rotation.

RL Rotation in AVL Tree

]]>
https://programmerbay.com/avl-tree-a-height-balancing-bst/feed/ 0
What is Binary Search Tree and its Operations? https://programmerbay.com/what-is-binary-search-tree-and-its-operations/ https://programmerbay.com/what-is-binary-search-tree-and-its-operations/#respond Sat, 13 Jun 2020 17:07:02 +0000 https://www.programmerbay.com/?p=2928 What is Binary Search Tree ?

In Data structures, a binary search tree is a linked data structure that provides a way to store data orderly. All stored keys should satisfy the properties of a Binary search tree. These properties are:

  • Nodes of right subtree always have a value greater than the parent’s node
  • Nodes of left subtree always have value lesser than the parent’s node

It requires O(log n) time to perform basic operations in the average case. However, basic operations on BST take time proportional to the height of the tree.

Each node represents an object that consists link to the right subtree, a key value associated with the node, and a link to left subtree. If any parent or child node is missing, then it would be represented as NIL value.

image 2020 12 26 190922
Figure : Binary Search Tree Example

Binary search tree supports operations such as Insertion, Deletion, Searching of a node in the tree and also allow us to find Minimum, Maximum, Successor and Predecessor. Apart from that, a BST can be traversed in three-ways Inorder traversal, post-order traversal, and pre-order traversal.

Basic Operations in Binary Search Tree

  • Search Operation

A search operation starts from the root node to all the way downward recursively. The targeted element is compared with the root node, if both are equal then the search would get terminated.

If it is smaller than the root node then the search goes on at left subtree. And if the targeted value is greater, then search operation would be performed left subtree recursively.

  • Insertion and deletion

To insert a given element, BST first performs search operation, starting from the root node in order to locate the insertion position. Ones a NIL location is found then insertion is performed.

To delete a key from a BST we must know three cases:
1) If the targeted element has no child then remove the element and place NIL.
2) If the targeted element has only a single child, in this case, we just fill that place by simply putting its child node in its position.
3) If it has two children, then the key greater then the targeted value or Successor of targeted value would replace it.

  • Minimum and Maximum

A minimum key in a BST can be figured out by traversing left subtree from root node till a NIL is not encountered.

Similarly, A maximum key in a BST can be figured out by traversing right subtree from root node till a NIL is not encountered.

  • Successor and predecessor

A successor node for a given key value of BST can be derived without performing any comparison. If there is an immediate key available which is greater than the given node then the value would be returned otherwise NIL would be returned.

A predecessor node for a given key value of BST can be derived, if there is an immediate key smaller than given node then the value would be returned otherwise NIL.

  • Traversal 

Traversing refers to visit each and every node of a tree. Binary search tree can be traversed in three ways

In-order traversal

In this way of traversing a tree, we start from visiting all the nodes of left subtree, then root and then lastly right subtree

Pre-order traversal

In this way of traversing a tree, we start from the root, then visit all the nodes of left subtree and then last traverse right subtree

Post-order traversal

In this way of traversing a tree, we start from visiting all the nodes of left subtree, then all the nodes of the right subtree and then lastly root

]]>
https://programmerbay.com/what-is-binary-search-tree-and-its-operations/feed/ 0
C Program to sort an array using Quicksort in Ascending Order https://programmerbay.com/program-to-sort-an-array-using-quicksort/ https://programmerbay.com/program-to-sort-an-array-using-quicksort/#respond Thu, 11 Jun 2020 12:05:22 +0000 https://www.programmerbay.com/?p=2894 Quicksort is based on Divide and Conquer paradigm. It works by partitioning the given input array into two subarrays and sorts them through recursive calls until they get sorted.

In this, a pivot element is used that acts as a base for partitioning the array, where element lesser than pivot element arranged at the left side and bigger ones at the right side.

It requires O(n log n) running time to sort the given array when it comes to the average case.

Therefore, it can be used to sort a large set of elements. However, in the worst case, the running time of this algorithm hops up to O(n2).

Pseudocode

Quicksort( Array,first,last)
IF first <last
partitionpoint = Partition(arr,first,last);
Quicksort(Array,first,partitionpoint-1)
Quicksort(Array,partitionpoint+1,last)

Partitioning

Partition(Array,first,last)
pivot = arr[last]
i=first-1
FOR j=first to last - 1
IF Array[j]<pivot
i=i+1
exchange Array[i] with Array[j]
END IF
END FOR
exchange Array[i] with Array[last]
return i+1

C program to sort an array using Quicksort in Ascending Order

#include<stdio.h>
#define size 5

void quicksort(int arr[],int first,int last)
{

if(first <last)
{
int partitionpoint;
partitionpoint = sort_me(arr,first,last);
quicksort(arr,first,partitionpoint-1);
quicksort(arr,partitionpoint+1,last);
}

}

int sort_me(int arr[],int first,int last)
{
int pivot = arr[last],j,temp,n;
int i=first-1;
for(j=first;j<last;j++)
{
if(arr[j]<pivot)
{
i++;
temp =arr[j];
arr[j]=arr[i];
arr[i]=temp;

}
}
temp =arr[i+1];
arr[i+1]=arr[last];
arr[last]=temp;

return i+1;
}

int main()
{
int arr[size],j,temp,n;
printf("Please enter elements: \n "); 
for(n=0;n<=size-1;n++)
{
scanf("%d",&arr[n]);
}

quicksort(arr,0,size-1);

printf(" \n Sorted Elements :");

for(n=0;n<=size-1;n++)
{
printf("\n %d ",arr[n]);
} 
return 0;

}

 

Output:

quicksort

]]>
https://programmerbay.com/program-to-sort-an-array-using-quicksort/feed/ 0
C program to sort an array using Bubble Sort in Ascending Order https://programmerbay.com/program-to-sort-an-array-using-bubble-sort/ https://programmerbay.com/program-to-sort-an-array-using-bubble-sort/#respond Wed, 10 Jun 2020 10:56:44 +0000 https://www.programmerbay.com/?p=2867 The program sorts an array in ascending order based on the given input. There are various sorting algorithms to achieve this functionality Such as Selection Sort, Insertion sort and more . In this, We’ll be using Bubble Sort algorithm.

Bubble sort is one of the simplest and most popular algorithms, but inefficient. It repeatedly compares each element with their adjacent elements and performs swapping if required.

Pseudocode

FOR i=0 to Array.Length
FOR j=Array.Length-1 to i
IF A[j]< A[j-1])
Swap A[j] and A[j-1]
END IF
END FOR
END FOR

C program to sort an array using Bubble Sort in Ascending Order

#include<conio.h>
#include<stdio.h>
#define size 5
void main()
{
int arr[size],i,j,temp;
printf("Please enter elements: \n "); 
for(i=0;i<=size-1;i++)
{
scanf("%d",&arr[i]);
}

for(i=0;i<=size-1;i++)
{
for(j=size-1;j>i;j--)
{
if(arr[j]<arr[j-1]){
temp = arr[j];
arr[j] =arr[j-1];
arr[j-1] = temp;
}

}

}

printf(" \n Entered Elements :");

for(i=0;i<=size-1;i++)
{
printf("\n %d ",arr[i]);
} 
getch();

}

 

Output:

bubble sort 1

How does Bubble Sort program work?

bubble sort working

It requires a constant amount O(1) of additional memory space and O(n2) comparisons in the worst case and average case to sort an array.

However, this sorting technique is too slow and impractical if we compare it with Insertion sort

]]>
https://programmerbay.com/program-to-sort-an-array-using-bubble-sort/feed/ 0
C Program to sort an array using Insertion sort in Ascending order https://programmerbay.com/program-to-sort-an-array-using-insertion-sort/ https://programmerbay.com/program-to-sort-an-array-using-insertion-sort/#respond Tue, 09 Jun 2020 18:33:09 +0000 https://www.programmerbay.com/?p=2846 The program sorts an array based on given input. There are bunch of sorting techniques such as Bubble sort, Selection sort and more to achieve this functionality. In this, we’ll be discussion Insertion Sort technique.

Insertion sort

In this sorting technique, the number that needs to be sorted is known as Key. This algorithm is efficient for sorting a small unit of elements.

It can be considered as a way that people use to sort playing cards. A set of cards facing towards the table are picked one by one and put at their correct place.

we compare a new card lifted from the table with the existing ones in our hand.

playing cards

Similarly, in insertion sort at each iteration, an element from the input dataset is taken out and placed to the location where it belongs to be. It is followed until all the inputs get placed and sorted.

Pseudocode

for j=1 to Array.Length

key=arr[j]
k=j-1;
while k>=0 && arr[k]>key
arr[k+1] = arr[k]
arr[k]=key
k= k-1
End While
End for

C program to sort an array using Insertion Sort in Ascending Order

#include<conio.h>
#include<stdio.h>
#define size 6
void main()
{
int arr[size],i,j,k,key,temp;
printf("Please enter elements: \n "); 
for(i=0;i<=size-1;i++)
{
scanf("%d",&arr[i]);
}

for(j=1;j<=size-1;j++)
{
key=arr[j];
k=j-1;
while (k>=0 && arr[k]>key)
{
arr[k+1] = arr[k];
arr[k]=key;
k=k-1;
}
}

printf(" \n Entered Elements :");

for(i=0;i<=size-1;i++)
{
printf("\n %d ",arr[i]);
} 
getch();
}

 

Output:

insertion sort

How does it work?

insertion sort working

It requires a constant amount O(1) of additional memory space and O(n2) comparisons in the worst case and average case to sort an array.

However, sorting time to sort the same size of elements can be varied depending on how much a given array is already sorted.

]]>
https://programmerbay.com/program-to-sort-an-array-using-insertion-sort/feed/ 0
Substitution Method for Solving Recurrences with example https://programmerbay.com/recurrences-substitution-method/ https://programmerbay.com/recurrences-substitution-method/#respond Sun, 07 Jun 2020 17:18:46 +0000 https://www.programmerbay.com/?p=2821 Recurrence:

The term Recurrence can be defined as any kind of inequality or equation that focuses on the value over the small inputs of the function. The running time of an algorithm with recursive calls can be easily described by recurrence.

This is the reason that recurrence is often used in Divide-and-Conquer problems.

Substitution method

There are 3 ways of solving recurrence:

  • SUBSTITUTION METHOD – A guess for the solution is made, and then we prove that our guess was incorrect or correct using mathematical induction.
  • ITERATION METHOD – We need to draw each and every level of recurrence tree and then calculate the time at each level.
  • MASTER METHOD – In this method, we have some predefined recurrence equation cases, and our focus is to get a direct solution for it.

 

SUBSTITUTION METHOD

  • Firstly, guess a solution for the given equation.
  • Now, using mathematical induction prove that the guess is correct.

An example is given below to show the method in detail.

substitution method 2

Substitution Method

 

]]>
https://programmerbay.com/recurrences-substitution-method/feed/ 0
Growth of Functions https://programmerbay.com/growth-of-functions/ https://programmerbay.com/growth-of-functions/#respond Sun, 07 Jun 2020 16:44:09 +0000 https://www.programmerbay.com/?p=2787 Growth of Functions

Algorithm’s rate of growth enables us to figure out an algorithm’s efficiency along with the ability to compare the performance of other algorithms. Input size matters as constants and lower order terms are influenced by the large sized of inputs.

For small inputs or large enough inputs for the order of growth of execution time, we can find out the more efficient algorithm among all other algorithms with the help of asymptotic notation.

However, for large inputs where the size of inputs grows without bound become the main concern for the increase in running time of an algorithm. Asymptotic analysis of an algorithm can be simplified with Asymptotic notations.

Asymptotic Notations

Asymptotic notations are used to represent the asymptotic( within limit) running time of an algorithm. These are defined using functions. These can be implied on functions that provide aspects like running time of algorithms and amount of space that they use and more.

 

Theta Notation ( Θ Notation) :

It bounds a function inbetween the constant factors that are tightly bound. In this, lower order terms of an asymptotically positive function are ignored as they are insignificant and coefficient of order term is also ignored. It asymptotically bounds a function from above and below.

Θ (g(n)) = {  f(x) : there exist positive constants a1,a2 and n0 such that  0 <= a1g(n)<= f(n) <= a2g(n) for all n>= n0 }

theta notation

Big O Notation ( O Notation) :

It asymptotically bounds a function only from above. It is used to get upper bound on a function,  to within a constant factor. we use it to bound the worst case runntime of an algorithm

O (g(n)) = {  f(x) : there exist positive constants a and n0 such that  0 <= f(n) <= ag(n) for all n>= n0 }

 

O notation

Big Omega Notation ( Ω Notation) :

It asymptotically bounds a function only from below. It is used to get lower bound on a function. we use it to bound the best case running time of an algorithm.

Delta notation

Ω (g(n)) = {  f(x) : there exist positive constants a and n0 such that  0 <= ag(n) <= f(n)  for all n>= n0 }

]]>
https://programmerbay.com/growth-of-functions/feed/ 0
Dynamic Programming Paradigm https://programmerbay.com/dynamic-programming-paradigm/ https://programmerbay.com/dynamic-programming-paradigm/#respond Tue, 20 Aug 2019 06:05:32 +0000 https://www.programmerbay.com/?p=2932 Dynamic Programming Paradigm

Dynamic programming can be said as the optimization of the plain recursion technique.

So before defining any technicalities what is the essence of this Dynamic programming paradigm?

The dynamic programming paradigm has been to solve the complex problems by decomposing it into various sub-parts, now the most unique thing is that, these small sub-parts will hold onto their individual result to avoid the recalculation of the same.

One of the most popular problems solving paradigm is Dynamic Programming Paradigm. This paradigm focuses on the concept of optimization over the recursion concept. The main objective of this Dynamic Programming Paradigm is to optimize the solution for cases where the same inputs are being used repeatedly again and again.

This objective can be achieved by simply providing storage for the issues/sub-problems that we face which in turn makes the solution finding the process easier for afterward. This storage of values of the problems also guarantees to save time by reducing the revaluation steps for the solution; which in turn provides a more dynamic output for the problem.

This Dynamic Programming Paradigm works on the principle of less time complexity. The sole reason for this principle is based on the above-discussed optimization practice for the problem. The reduced time complexity enables the solution to work faster in the real world scenario. This, in turn, converts the time complexities of the problems from exponential (highly complex) to a polynomial(less complex).

Optimal Substructure-

The concept of Optimal substructure says that the answer/solution for the given optimized problem can be achieved by a combination of various optimal solutions to its sub-problems. The technique of Recursion is used to define the optimal substructures in dynamic programming.

Overlapping Sub-problems-

The concept of Overlapping sub-problems states that the ideal space of sub-problems must be small. This is because as for any recursive algorithm should be solving the same sub-problems again and again till the time it is completely dissolved, rather than generating new problems.

 

 

Dynamic programming based Algorithms are as follows:

  • Knapsack Problem
  • Tower of Hanoi
  • Fibonacci Series
  • Dijkstra Algorithm
  • Floyd – Warshall Algorithm

 

 

Example: Following Dynamic Programming Paradigm for Fibonacci series-

i=2;

F[0]=0;

F[1]=1;

While( i<=n)

{ F[i] = F[i-1] + F[i-2];

i++;

}

Return F[n];

 

]]>
https://programmerbay.com/dynamic-programming-paradigm/feed/ 0