Selection Sort

Selection sort is an in-place, comparison sorting algorithm. Like bubble sort it also divides the given array in two sub arrays sorted and unsorted, divided by an imaginary wall. Initially all the elements will be in the unsorted list. Each pass will pick the smallest element from the unsorted list and add it at the end […]

Optimized bubble sort algorithm

What is Bubble Sort. Write algorithm of mention the Time & Space complexity of the Algorithm. Also suggest improvements which will improve the best case running time of Algorithm to O(n). Solution: Bubble Sort is a sorting algorithm which compares two adjacent elements and swap them if they are not in the right order. To […]

Binary Search or Half-interval search

Binary search is a Divide & Conquer algorithm used to search for an element in the array. It requires the array to be sorted. If array is not sorted then the only way to search in the array is Linear search. Binary search divide the number of elements to be searched in two halves in […]

Build binary tree from ancestor matrics

You are given the Ancestor matrix of a Binary tree, write an Algorithm to construct the corresponding tree. For example, the below tree: Will have the following ancestor Matrix The order of rows in the above matrix is not defined. I have kept it in the ascending order because the data in our nodes is […]

Print both diagonals of a matrix

Given a matrix of order N*N, write code to print both the diagonals of that matrix. For example: the matrix and its 2 diagonals are given below: In the above diagram, I have colored the elements in first diagonal as red and elements in 2nd diagonal as green.

Print elements of a matrix in diagonal order

Given a square matrix of order N*N, write code to print all the elements in the order of their diagonal. For example, in the below matrix, the elements should be printed in the marked (in red) order, and the final output should be as shown below: