Let’s get started…..
Algorithms are step by step solution for a program. So as developer or programmer we all have to learn some some algorithms or write new algorithms.
Let’s learn some popular and useful algorithms.
Factorial of a number is multiplication of 1 to number. Factorial of a number is denoted by n!(where n is the number).
above solution is called iterative solution. An alternative solution is called recursive solution. let’s see that
In above approach a function call itself again and again which is called recursion.
Yo can use either first one or second one.
2. Fibonacci Number or Series:
A Fibonacci number is the total of it’s two previous number in Fibonacci series.
Fibonacci series be like- 0,1,1,2,3,5,8,13,21,34………..
Let’s write a program to find nth number in series
In recursive approach
3. Linear Search:
4. Binary Search:
5. Bubble Sort:
Bubble sort is a sorting algorithm,which runs on complexity O(n²). which is not an ideal solution for large list.
The idea behind bubble sort is that every pair of adjacent values is compared, and then the two values swap positions if the first value is greater than the second. This way during each pass through the array, the largest value “bubbles up” to the top, and after each pass the elements furthest to the right are in the correct order.
let’s see the code
6. Selection Sort:
Selection sort is a sorting algorithm runs on complexity O(n²) time. This algorithm is very simple and easy to implement, however it is also very inefficient .
let’s see an example
7. Insertion Sort:
Before we dive into the specifics regarding the implementation of insertion sort, let’s talk more about it’s benefits. Insertion sort runs in O(n²), or quadratic, time in the worst case. This typically isn’t very effective and should not be used for large lists. Because of insertion sort’s low hidden constant value, however, it usually outperforms more advanced algorithms such as quick sort or merge sort on smaller lists. Some implementations of those advanced algorithms will even switch from using their more advanced methodology to insertion sort when the list gets small enough. In practice, insertion sort is also usually more efficient than other quadratic sorting algorithms such as bubble sort or selection sort. It’s best case run time is O(n), or linear, which occurs if the input array is already sorted. On average, insertion sort’s run time is still quadratic.
8. Quick Sort :
If you’re interviewing for Software Engineering position, one of the more intimidating questions to deal with is explaining how the Quicksort algorithm works.
The simplest algorithmic steps for Quicksort is:
- Pick a pivot element that divides the list into two sublists. We can select a random element as the pivot.
- Reorder the list so that all elements less than the pivot element are placed before (towards its left) the pivot and all elements greater than the pivot are placed after it (towards its right).
- Repeat steps 1 and 2 on both the smaller and larger list. That is, Recursively apply the above steps to the sub-array of elements with smaller values and separately to the sub-array of elements with greater values.
Below is a basic implementation without usuing swap function and partition function.
9. Merge Sort:
That’s it ! wish me luck !