Bubble sort

The sorting problem Input: Array of numbers , unsorted. Eg. Output : Same numbers sorted in some order, say increasing order. Eg. What is Bubble Sort? The bubble sort works by comparing each item in the list with the item next to it, and swapping them if required. The algorithm repeats this process until it makes a pass all the way through the list without swapping any items (in other words, all items are in the correct order). [Read More]

Merge Sort

Mergesort is one of the older algorithms known since 1945. It is very good example of divide and conquer paradigm and is better than other simpler sort algorithms like selection, insertion or bubble sort. The sorting problem Input: Array of numbers , unsorted. Eg. Output : Same numbers sorted in some order, say increasing order. Eg. So, merge sort algorithm is a recursive algorithm which calls itself on the smaller problem set. [Read More]

Binary search on Array - Recursive and iterative

There are two basic searching algorithms used. One is the simplest technique and is discussed here. Here we will discuss the binary search method. The other search method is the binary search method. The binary search technique is a very fast and a more efficient technique when compared to the linear search method, and gets us the result in O(log n) where n is number of elements. The only requirement for this method is that the input array of elements must be in the sorted order. [Read More]

Routine to draw a circle (x ** 2 + y ** 2 = r ** 2) without making use of any floating point computations at all.

Problem Write a routine to draw a circle (x ** 2 + y ** 2 = r ** 2) without making use of any floating point computations at all. Solution Let (x ^ 2 + y ^2 = r ^ 2)……………………………..1 The basic idea is to draw one quadrant and replicate it to other four quadrants. Assuming the center is given as (a,b) and radius as r units, then start X from (a+r) down to (a) and start Y from (b) up to (b+r). [Read More]

WAP to check whether string is palindrome

Problem Write a method which will accept a string and return true if the string is a palindrome and false if it isn’t. Follow up: a) your method should consider lower case and upper case characters to be the same. b) your method should ignore special characters and white spaces, for e.g. if your input were the strings were “Madam, I’m Adam!!”, then you should consider it a palindrome and hence return true ignoring case and special characters. [Read More]

Program to merge two 1-D arrays

Here is the program in cpp: #include <iostream.h> #include <conio.h> using namespace std; const int MAX1 = 5 ; const int MAX2 = 7 ; class array { private : int \*arr ; int size ; public : void create ( int sz ) ; void sort( ) ; void display( ) ; void merge ( array &a , array &b ) ; } ; // creates array of given size sz, dynamically void array :: create ( int sz ) { size = sz ; arr = new int\[size\] ; int n ; for ( int i = 0 ; i < size ; i++ ) { cout << "\\nEnter the element no. [Read More]