Tuesday, November 1, 2011

TCS Pre ILP Assignment1- Solutions(contd)

Question 2
Greatest common divisor
Calculate the greatest common divisor of two positive numbers a and b.
gcd(a,b) is recursively defined as
gcd(a,b) = a if a =b
gcd(a,b) = gcd(a-b, b) if a >b
gcd(a,b) = gcd(a, b-a) if b > a
Program:

import java.io.*;
import java.util.Scanner;
class program2
{
public static void main(String args[])
{
Scanner sr= new Scanner(System.in);
System.out.println("Enter The number a");
int a=sr.nextInt();
System.out.println("Enter The number b");
int b=sr.nextInt();
int gcd;
if(a==b)
gcd=a;
else if(a>b)
gcd=findgcd(a-b,b);
else
gcd=findgcd(b,b-a);
System.out.println("The greatest common divisor of numbers " + a + " and " + b + " is " + gcd);
}
public static int findgcd(int c,int d)
{
if (d == 0)
return c;
return findgcd(d, c % d);
}
}


Improve the understandability of the below given code:
class Problem1
{
int[] a;
int nElems;
public ArrayBub(int max)
{
a = new int[max];
}
public void insert(int value)
{
a[nElems] = value;
nElems++;
}
public void Sort()
{
int out, in;
for(out=nElems-1; out>1; out--)
for(in=0; in a[in+1] )
swap(in, in+1); }
public void swap(int one, int two)
{
long temp = a[one];
a[one] = a[two];
a[two] = temp;
}

The above code explains bubble sorting implementation.
Bubble sort is also known as exchange sort. Bubble sort is a simplest sorting algorithm. In bubble sort algorithm array is traversed from 0 to the length-1 index of the array and compared one element to the next element and swap values in between if the next element is less than the previous element. In other words, bubble sorting algorithm compare two values and put the largest value at largest index. The algorithm follows the same steps repeatedly until the values of array is sorted. In worst-case the complexity of bubble sort is O(n2) and in best-case the complexity of bubble sort is O(n).

Say we have an array unsorted a[0],a[1],a[2]................ a[n-1] and a[n] as input. Then the following steps are followed by bubble sort algorithm
to sort the values of an array.
1.Compare a[0] and a[1] .
2.If a[0]>a[1] then Swap a[0] and a[1].
3.Take next a[1] and a[2].
4.Comapre these values.
5.If a[1]>a[2] then swap a[1] and a[2]
...............................................................
................................................................
at last compare a[n-1] and a[n]. If a[n-1]>a[n] then swap a[n-1] and a[n]. As we see the highest value is reached at nth position. At next iteration leave nth value. Then apply the same steps repeatedly on a[0],a[1],a[2]................ a[n-1] elements repeatedly until the values of array is sorted.

So in the above code we obtain:

step 1: we initialise number of elements and an array.

step 2:we initialise max for a value and then set a varialble say a= maximum.

step3: we accept the value for the elements.

step4: we set two variables out and in and we compare a[0] with a[1] and if a[0] > a[1] then swap them. As we see the highest value is reached at nth position. At next iteration leave nth value. Then apply the same steps repeatedly on a[0],a[1],a[2]................ a[n-1] elements repeatedly until the values of array is sorted.

No comments:

Post a Comment