Skip to main content

Posts

Showing posts with the label c++

Counting sort Algorithm

 Want to sort numbers in a given range? Use Counting sort algorithm. Counting sort is a integer sorting algorithm, It is a non-comparison sorting algorithm as it does not compare different elements rather it uses index of elements and their count while sorting them. Counting sort is a linear sorting algorithm and must be used when elements are in a given range and variation in the elements is not much. It simply operates by counting the number of occurrences of  different elements in a given range, as it is a linear sorting technique it has time complexity of   O(n + k) where 'n' is the number of elements in the array and 'k' is the number of elements between max and min elements. let us consider the below example,  let the array of numbers to be sorted be  arr = {1,3,2,1,3,3,2} here, n = 7, min = 1, max = 3 hence k = 3 now we create a array that holds counts of all the elements, 1 has occurred 2 times, 2 has occurred 2 times and 3 has o...

Quick Sort Algorithm using Recursion

Quick sort is one of the most important and widely used sorting algorithm. Quick sort is a very efficient algorithm which if implemented well can run two to three times faster than its competitors merge sort and and heap sort. The algorithm is a divide and conquer algorithm. The algorithm works by selecting a pivot element, this pivot element is usually the first element and choice of the pivot element affects the running time of the algorithm. The algorithm is basically to select a pivot element and then moving all the element less than the pivot element to its left and all the elements greater than pivot element to its right. Then we divide the array into 2 parts, one on the left of the pivot and other on the right of the pivot and then pass these 2 arrays to the algorithm for further sorting. Algorithm : Choose a Pivot element. Take 2 variables left_marker and right_marker excluding the pivot element. Set left_marker as the first element of the array Set right_marker as the...

Binary Search Algorithm

Binary Search also known as Algorithmic or binary chop is a searching algorithm that searches for a element in a sorted data structure. Binary search is a searching algorithm that searches the required value by comparing the target value with the middle value of the array. The following is the algorithm for binary search implementation Step 0 : Sort the array using any sorting algorithm (I am using bubble sort in the below program) Step 1 : Initialize first = 0 and last  = list.size() - 1 Step 2 :  while first < last and the element is not found calculate mid = (first + last) / 2 Step 3 : Compare target value with middle element Step 4 : if the middle value is greater than the target value then target value lies on the left side of the middle value do goto step 1 with last = mid - 1 Step 5 : if the middle value is lesser than the target value then target value lies on the right side of the middle value do goto step 1 with first = mid + 1 Step 6 : ...

Infix to Prefix conversion using Stack

This post is about conversion of Infix expression to Prefix conversion. For this conversion we take help of stack data structure, we need to push and pop the operators in and out of the stack. Infix expressions are the expressions that we normally use, eg. 5+6-7; a+b*c etc. Prefix expressions are the expressions in which the 2 operands are preceded by the operator eg. -+56 7 , +a*bc etc. This method is very similar to the method that we used to convert Infix to Postfix but the only difference is that here we need to reverse the input string before conversion and then reverse the final output string before displaying it. NOTE: This changes one thing that is instead of encountering the opening bracket we now first encounter the closing bracket and we make changes accordingly in our code. So, to convert an infix expression to a prefix expression we follow the below steps (we have 2 string, 1st is the input infix expression string 2nd is the output string which is empty initially)...

Insertion Sort

Insertion sort is a very simple sorting algorithm. Insertion sort is an in-place and comparison based algorithm in which an element is compared with its previous adjacent element and sorted. In this algorithm after each iteration we get a partially sorted algorithm. Insertion sort works well with small datasets but its efficiency decreases when the size of the dataset increases. It is less efficient that quick sort, merge sort or heap sort but is better that selection and bubble sort. It has a worst case time complexity of o( n 2 ), hence is less efficient for large datasets. To know more about insertion sort and its uses and advantages click Here . C Program C++ Program Java program Python Program C Program C++ Program Java Program Python Program Sample input and output to check the program You might also be interested in  Bubble sort Algorithm Selection Sort Algorithm Hashing with Quadratic Probing Hashing wi...

Selection Sort

Selection sort is one of the most simple sorting algorithm. This sorting algorithm is an in-place comparison-based algorithm in which the list or the array used is divided into two parts, the left part is the sorted part of the list and the right part consists of unsorted element of the array. Initially, the sorted part is empty and the unsorted part is the entire list. In Selection sort in each iteration the smallest element in the unsorted part of the array is brought to the to its correct position.Selection almost always outperforms bubble sort and gnome sort.  This algorithm is not suitable for large data sets as its average and worst case complexities are of Ο(n 2 ), where  n  is the number of items. See the following example for better understanding. To know more about selection sort click Here . C Program C++ Program Java program Python Program C Program C++ Program Ja...

Bubble sort Algorithm

Bubble sort is the most simple sorting technique used to sort arrays , linked list etc. It is an algorithm that keeps traversing the list until the list is completly sorted.  Bubble sort has been occasionally referred to as a "sinking sort". The idea being that the larger elements are heavier hence they sink to the bottom of the list and smaller elements being light bubble to the top of the list. Algorithm In bubble we keep comparing 2 adjacent elements and swap their places is they are not in correct order. Performance Bubble sort being a simple algorithm works fine when the size of the data is small but its performance decreases as the size of the data increases, hence Bubble is used only when number of elements is small or only some elements are out of order. To know more about bubble sort, its performance, complexity and comparisons to other sorting methods click Here .    C Program C++ Program Java program Python Program C Program ...

Home Page

List of All Programs The Following is the List of all the programs on my Blog Math Programs Square Root of a number using Babylonian Method Finding The Next Smallest Palindrome Finding the Armstrong Numbers Factorial of a number GCD using Euclid's Algorithm Check if a number is Fibonacci Number or not LCM of 2 numbers Trailing Zeros in factorial of a number Sorting Algorithms Bubble Sort Algorithm Selection Sort Algorithm Insertion Sort Algorithm Shell Sort Algorithm Counting Sort Algorithm Linked List Programs Simple Singly Linked List Linked List in C++ Linked List in Python Linked List in Java Doubly Linked List Finding Kth element from the end of Linked List Delete a node from Linked List Delete Kth element from the end of Linked List Rotate Linked List in an Anti-clockwise direction Reversing first K nodes of a Linked List Binary Search Tree Left View of Binary Tree Righ...

Stack implementation using array

A stack is a data structure it serves as a collection of elements. It has 2 principle operation push and pop. The push operation pushes the element on the top and the pop operation removes the element on the top. This can be seen in the below image. Stack works on the principle of  Last In First Out or LIFO i.e the element that is entered last is removed first. Stacks can be implemented using arrays as well as linked list. There are a lot of applications of Stacks like Recursion Backtracking  Memory management  Activation records etc To know more about Stacks click here  The following program is to implement a stack using an array with maximum size of 20. C Program C++ Program Java program Python Program C Program C++ Program Java Program Python Program You might also be interested in  Linked List in python Linked List in C++ Finding the next Smallest Palindrome Finding facto...

factorial of a number

Factorial of a number is equal to the product of the number and all the positive numbers below it. Factorial of a number is denoted as n!. for example Factorial of 7 is denoted as 7!. 7! = 7*6*5*4*3*2*1 7! = 5040 also 0! is 1 according to the convention of empty product. To know more about factorials click here C Program C++ Program Java program Python Program C Program C++ Program Java Program Python Program Sample input and output to check the program You might also be interested in Linked List in Python Linked List in Java Program to find Armstrong Numbers Program to find next smallest Palindrome finding square root of a number using Babylonian method

Finding square root of a number by Babylonian method

The Babylonian method for finding square roots involves dividing and averaging, over and over, to obtain a more accurate solution with each repeat of the process. Inorder to find the square root of a number you need to follow the following steps. Make an initial guess for the square root  Divide the original number by this guess Find out the mean of the 2 numbers check if the difference between guess and quotient is less than the error if not then repeat the process again. use this mean value as your next guess Let us take an example of finding out the square root of 7 to 3 decimal places let n = 7 and error = 0.001 let us take an initial guess = 3 FIRST ITERATION Step 1: Guess = 3  Step 2: Divide 7 by 3 = 2.3333333 Step 3: Find average of 3 and 2.333333 = 2.666666 (because (2+2.333333)/2 = 2.666666) Step 4: difference (3 - 2.3333333) > error hence repeat the process again Step 5: Next guess is 2.666666 SECOND ITERATION Step 1: Guess ...

Finding next smallest Palindrome Number

A Palindrome  is a word, phrase,  number , or other sequence of  characters  which reads the same backward or forward for example '123321' or 'racecar'. Given a number inorder to find the next smallest palindrome we need to consider the following cases: Case 1 : if the number is less than 10 if yes then the smallest palindrome is 11. Case 2 : if the number contains all digits to be 9 eg. 9 , 99 , 999 , 9999 in such case the next smallest palindrome is 11 , 101 , 1001 , 10001 as we can see it contains n+1 digits with n-1 0's between two 1's. Case 3: considering if the entered number is already a palindrome then we have 2 cases to deal with  If it does not contain a 9 in its middle digits for eg 1771 or 171 in such cases it is very easy to find the next smallest palindrome you only need to add one to the middle digits so the answer for above cases is 1881 or 181. For the Second case if it contains one in the middle digits for eg. 1991 or 17971 in this c...