daa – 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 daa – 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
C Program to sort an array using Merge Sort with its Time Complexity https://programmerbay.com/program-to-sort-an-array-using-merge-sort-with-its-time-complexity/ https://programmerbay.com/program-to-sort-an-array-using-merge-sort-with-its-time-complexity/#respond Fri, 12 Jun 2020 16:22:39 +0000 https://www.programmerbay.com/?p=2904 The program sort an array using Merge Sort technique.

Merge Sort

Merge sort is one of the most common sorting techniques and it is based on the divide and conquer paradigm.

This paradigm focuses on the recurring nature of the problem and thus divides it into various branches (or subproblems). This division for sub-problems depends upon the complexity of the problem at hand.

Now, these sub-problems are further broken down recursively till they are solved at the very end and then, are retracted in a backward fashion and all these results are merged together to give a final solution of the problem.

C program to Sort an array using Merge Sort

#include<stdio.h>

void merging(int arr[],int  a,int b,int a1,int b1)

{

int temp[50];

int i,j,k;

i=a;

j=a1;

k=0;

while(i<=b && j<=b1)

{

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

temp[k++]=arr[i++];

else

temp[k++]=arr[j++];

}

while(i<=b)

temp[k++]=arr[i++];

while(j<=b1)

temp[k++]=arr[j++];

for(i=a,j=0;i<=b1;i++,j++)

arr[i]=temp[j];

}

void merge_sort(int arr[],int i,int j)

{

int mid;

if(i<j)

{

mid=(i+j)/2;

merge_sort(arr,i,mid);

merge_sort(arr,mid+1,j);

merging(arr,i,mid,mid+1,j);

}

}

int main()

{

int arr[30],n,i;

printf("Enter no of elements:");

scanf("%d",&n);

printf("Enter array elements:");

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

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

merge_sort(arr,0,n-1);

printf("\nSorted array is :");

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

printf("%d ",arr[i]);

return 0;

}

 

Output:

merge sort

 

The time complexity of the merge sort is O (n log n), because of the following reasons:

  • The divide phase, finds out the midpoint of each subsequent sub-array, which results in O(1) time for each step.
  • In the conquer phase, this is n/2 because this step recursively sorts 2 sub-arrays.
  • The merge phase takes O(n) time because it merges all the elements of the array.

]]>
https://programmerbay.com/program-to-sort-an-array-using-merge-sort-with-its-time-complexity/feed/ 0
C Program to search an element using Binary Search https://programmerbay.com/program-to-search-an-element-using-binary-search/ https://programmerbay.com/program-to-search-an-element-using-binary-search/#respond Fri, 12 Jun 2020 16:20:56 +0000 https://www.programmerbay.com/?p=2921 The program simply searches an element in the given array. There are two searching algorithms, linear search and binary search to achieve this functionality.

Binary Search

Binary search is a searching algorithm to find the given value within an array. This algorithm only works if the given array is already sorted. So that it can figure out whether the given element exists or not.

It works by dividing a list into two halves and compare the middle element with the desired value.

If the value doesn’t match, the half in which the desired value can’t be present gets eliminated and again the remaining one is split down from the middle and that middle term again compared. It is repeated until a match is not found.

It requires O(log n) running time to search an element in the worst case scenario. It is also known as half-interval search.

Pseudocode

first=0
last=Array.Length -1 
WHILE first<= last
mid= (first+last)/2
IF arr[mid]== element 
return mid
ELSE IF arr[mid]> element
last=mid-1
ELSE
first=mid+1
END IF
END IF
END WHILE

C program to search an element using binary search

#include<stdio.h>
#include<math.h>
int Binary_Search(int arr[], int element,int size)
{
int pos=-1,mid,first,last;
first=0;
last=size-1;
while(first<= last)
{
mid= (first+last)/2;
if(arr[mid]== element)
{
pos = mid;
break;
}
else if(arr[mid]>element)
last=mid-1;
else
first=mid+1;
}
return pos; 
}

int main()
{
int arr[]={1,2,3,4,5}; 
int i,pos,element;
printf("\n Please enter an element to search in list: ");
scanf("%d",&element);
pos =Binary_Search(arr,element,5);
if(pos!=-1)
printf("\n your element found at : %d position and the number was %d ", pos+1,arr[pos]);
else
printf("element is not found");
return 0;
}

 

Output:

binary search

How does Binary search work ?

binary search working

 

]]>
https://programmerbay.com/program-to-search-an-element-using-binary-search/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
Iteration Method for Solving Recurrences with example https://programmerbay.com/recurrences-iteration-method/ https://programmerbay.com/recurrences-iteration-method/#respond Sun, 07 Jun 2020 17:23:42 +0000 https://www.programmerbay.com/?p=2824 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.

 

ITERATION METHOD

  • Firstly draw the recursion tree.
  • Calculate the time in each level of the recursion tree.
  • Sum up all the time values.

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

 

iteration

]]>
https://programmerbay.com/recurrences-iteration-method/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