Found 547 Articles for Algorithms

Algorithms and Complexities

Monica Mona
Updated on 01-Nov-2023 14:23:31

33K+ Views

AlgorithmAn algorithm is a finite set of instructions, those if followed, accomplishes a particular task. It is not language specific, we can use any language and symbols to represent instructions.The criteria of an algorithmInput: Zero or more inputs are externally supplied to the algorithm.Output: At least one output is produced by an algorithm.Definiteness: Each instruction is clear and unambiguous.Finiteness: In an algorithm, it will be terminated after a finite number of steps for all different cases.Effectiveness: Each instruction must be very basic, so the purpose of those instructions must be very clear to us.Analysis of algorithmsAlgorithm analysis is an important part ... Read More

Print Matrix in spiral way

Ankith Reddy
Updated on 17-Jun-2020 10:14:06

888 Views

This algorithm is used to print the array elements in a spiral way. At first starting from the first row, print the whole content and then follow the last column to print, then the last row and so on, thus it prints the elements in spiral fashion. The time complexity of this algorithm is O(MN), M is the number of rows and N is the number of columns.Input and OutputInput: The matrix:  1   2   3   4   5   6  7   8   9  10  11  12 13  14  15  16  17  18 Output: Contents of ... Read More

Shuffle Array Contents

George John
Updated on 17-Jun-2020 10:17:55

410 Views

This algorithm will take an array and shuffle the contents of the array. It will generate a random permutation of the array elements.To solve this problem, we will swap elements starting from the last index to randomly generated an index in the array.Input and OutputInput: An array of integers: {1, 2, 3, 4, 5, 6, 7, 8} Output: Shuffle of array contents: 3 4 7 2 6 1 5 8 (Output may differ for next run)AlgorithmrandomArr(array, n)Input: The array, number of elements.Output: Shuffle the contents of the array.Begin    for i := n – 1 down to 1, do   ... Read More

Magic Square

Samual Sam
Updated on 17-Jun-2020 10:16:55

5K+ Views

The magic square is a square matrix, whose order is odd and where the sum of the elements for each row or each column or each diagonal is same. The sum of each row or each column or each diagonal can be found using this formula. n(n2+ 1)/2Here are the rules to construct a magic square −We will start from the middle column of the first row, of the matrix, and always go to the top left corner to place next numberIf the row exceeds, or the row is not in the matrix, then, change the column as left column and ... Read More

Tower Of Hanoi Problem

Chandu yadav
Updated on 17-Jun-2020 10:19:12

2K+ Views

Tower of Hanoi is a puzzle problem. Where we have three stands and n discs. Initially, Discs are placed in the first stand. We have to place discs into the third or destination stand, the second or auxiliary stand can be used as a helping stand.But there are some rules to follow. We can transfer only one disc for each movement.Only the topmost disc can be picked up from a stand.No bigger disc will be placed at the top of the smaller disc.This problem can be solved easily by recursion. At first, using recursion the top (n-1) discs are placed from ... Read More

Sort strings in Alphanumeric sequence

George John
Updated on 17-Jun-2020 09:48:30

4K+ Views

A list of given strings is sorted in alphanumeric order or Dictionary Order. Like for these words: Apple, Book, Aim, they will be sorted as Aim, Apple, Book.If there are some numbers, they can be placed before the alphabetic strings.Input and OutputInput: A list of strings: Ball Apple Data Area 517 April Man 506 Output: Strings after sort: 506 517 Apple April Area Ball Data ManAlgorithmsortStr(strArr, n)Input: The list of all strings, number of elements.Output − Strings in alphanumeric sorted order.Begin    for round := 1 to n-1, do       for i := 0 to n-round, do     ... Read More

Find Weekday using Zeller's Algorithm

Monica Mona
Updated on 17-Jun-2020 09:47:32

1K+ Views

Zeller’s Algorithm is used to find the weekday from a given date. The formula to find weekday using Zeller’s Algorithm is here:The formula is containing some variables; They are −d − The day of the date.m: It is the month code. From March to December it is 3 to 12, for January it is 13, and for February it is 14. When we consider January or February, then given year will be decreased by 1.y − Last two digits of the yearc − first two digits of the yearw − The weekday. When it is 0, it is Saturday, when ... Read More

Travelling Salesman Problem

Samual Sam
Updated on 17-Jun-2020 09:50:16

1K+ Views

One sales-person is in a city, he has to visit all other cities those are listed, the cost of traveling from one city to another city is also provided. Find the route where the cost is minimum to visit all of the cities once and return back to his starting city.The graph must be complete for this case, so the sales-person can go from any city to any city directly.Here we have to find minimum weighted Hamiltonian Cycle.Input and OutputInput: Cost matrix of the matrix. 0  20 42 25 30 20  0 30 34 15 42 30  0 10 10 ... Read More

Reservoir Sampling

Chandu yadav
Updated on 17-Jun-2020 09:51:21

812 Views

The Reservoir sampling is a randomized algorithm. In this algorithm, k items are chosen from a list with n different items.We can solve it by creating an array as a reservoir of size k. Then randomly pick one element from the main list and placed that item in the reservoir list. When one item is selected once, it will not be selected for next time. But his approach is not effective, we can increase the complexity by this method.In the reservoir list, copy first k items from the list, now one by one from the (k+1)th number in the list, ... Read More

Parity Check of a Number

Monica Mona
Updated on 17-Jun-2020 09:54:17

5K+ Views

Parity of a number is based on the number of 1’s present in the binary equivalent of that number. When the count of present 1s is odd, it returns odd parity, for an even number of 1s it returns even parity.As we know that the numbers in computer memory are stored in binary numbers, so we can shift numbers easily. In this case, by shifting the bits, we will count a number of 1’s present in the binary equivalent of the given number.Input and OutputInput: A number: 5 Binary equivalent is (101) Output: Parity of 5 is Odd.AlgorithmfinParity(n)Input: The number ... Read More

Advertisements