Category Archives: Algorithm

Recaman’s sequence

Given an integer n. Print first n elements of Recaman’s sequence. Examples: Input : n = 6 Output : 0, 1, 3, 6, 2, 7 Input : n = 17 Output : 0, 1, 3, 6, 2, 7, 13, 20, 12, 21, 11, 22, 10, 23, 9, 24, 8 It is basically a function with… Read More »

Stooge Sort

The Stooge sort is a recursive sorting algorithm. It is defined as below (for ascending order sorting). Step 1 : If value at index 0 is greater than value at last index, swap them. Step 2: Recursively, a) Stooge sort the initial 2/3rd of the array. b) Stooge sort the last 2/3rd of the array.… Read More »

Search, insert and delete in a sorted array

In this post search, insert and delete operation in a sorted array is discussed. Search Operation In a sorted array, the search operation can be performed by using binary search. Output: Index: 5 Insert Operation In an unsorted array, the insert operation is faster as compared to sorted array because we don’t have to care… Read More »

Search, insert and delete in an unsorted array

In this post search, insert and delete operation in an unsorted array is discussed. Search Operation In an unsorted array, the search operation can be performed by linear traversal from the first element to the last element. Output: Element Found at Position: 5 Insert Operation In an unsorted array, the insert operation is faster as… Read More »

Warnsdorff’s algorithm for Knight’s tour problem

Problem : A knight is placed on the first block of an empty board and, moving according to the rules of chess, must visit each square exactly once. Following is an example path followed by Knight to cover all the cells. The below grid represents a chessboard with 8 x 8 cells. Numbers in cells… Read More »