Category Archives: Java

An Interface in Solving Quantitative Distances

Heapsort

The Code Vault

Java Code To Illustrate Heapsort:

 

package heapsort;

import edu.princeton.cs.introcs.*;

public class Heapsort
{

public static int heapsize;

public static int Parent(int i)
{
return i/2;
}

public static int Left(int i)
{
return 2*i+1;
}

public static int Right(int i)
{
return 2*i+2;
}

public static void MaxHeapify(int A[], int i)
{
int l = Left(i);
int r = Right(i);
int greatest,temp;
if (l<heapsize && A[l]>=A[i]) greatest = l;
else greatest = i;
if (r<heapsize && A[r]>=A[greatest]) greatest = r;
if (greatest!=i)
{
temp = A[greatest];
A[greatest] = A[i];
A[i] = temp;
MaxHeapify(A,greatest);
}
}

public static void BuildHeap(int A[], int n)
{
int i;
for (i=(n/2)-1;i>=0;i–) MaxHeapify(A,i);
}

public static void heapsort(int A[])
{
BuildHeap(A,heapsize);
int i,temp;
for (i=A.length-1;i>=1;i–)
{
temp = A[0];
A[0] = A[i];
A[i] = temp;
heapsize–;
BuildHeap(A,heapsize);
}
}

public static void displayheap(int A[])
{
int height = (int)((Math.log10(A.length)) / (Math.log10(2)));
int spaces =…

View original post 82 more words

QuickSort

The Code Vault

Java Code To Illustrate QuickSort:

package quicksort;

import edu.princeton.cs.introcs.*;

public class QuickSort
{

public static void display(int a[])
{
for (int k=0;k<a.length;k++) System.out.print(a[k] + ” “);
System.out.println();
}

public static int Partition(int A[], int p, int r)
{
int pivot = A[r];
int i = p-1;
int temp;
for (int j=p;j<r;j++)
{
if (A[j]<=pivot)
{
i++;
temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
temp = A[r];
A[r] = A[i+1];
A[i+1] = temp;
return i+1;
}

public static int HoarePartition(int A[], int p, int r)
{
int i = p;
int j = r;
int x = A[p];
int temp;
while (i<j)
{
while(A[j]>=x && j>p) –j;
while(A[i]<=x && i<r) ++i;
if(i<j)
{
temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
temp = A[p];
A[p] = A[j];
A[j] = temp;
return j;
}

 

public static void quicksort(int A[],int p, int r)
{
if…

View original post 55 more words

Counting Sort

The Code Vault

Java Code To Illustrate Counting Sort:

 

package countingsort;

import java.util.Scanner;

public class CountingSort
{

public static void display(int A[])
{
for (int i=0;i<A.length;i++) System.out.print(A[i] + ” “);
System.out.println();
}

public static void CountingSort(int A[], int n, int k)
{
int i;
int B[] = new int[n];
int C[] = new int[k+1];
for (i=0;i<k+1;i++) C[i] = 0;
display(C);
for (i=0;i<n;i++) C[A[i]]++;
display(C);
for (i=1;i<k+1;i++) C[i] += C[i-1];
display(C);
for (i=A.length-1;i>=0;i–)
{
B[C[A[i]]-1] = A[i];
C[A[i]]–;
}
System.out.println(“Sorted Array:”);
display(B);
}

public static void main(String[] args)
{
Scanner x = new Scanner(System.in);
System.out.println(“Counting Sort”);
System.out.println(“Enter Number Of Elements”);
int n = x.nextInt();
int A[] = new int[n];
int max;
System.out.println(“Enter array”);
for (int i=0;i<n;i++) A[i] = x.nextInt();
max = A[0];
for (int i=0;i<n;i++) if (A[i]>max) max = A[i];
CountingSort(A,n,max);
}
}

View original post

Project Euler Problem #2 Solution in Java

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

/**
* @author Khuram Ali
*/
public class Problem2
{
public static void main(String[] args)
{
int term_1 = 1;
int term_2 = 2;
int term_3 = 0;
int sum = 0;
long MaxTerm = 4000000;

while (term_1 <= MaxTerm)
{
System.out.println (term_1);
if ((term_1 %2) == 0){sum += term_1;}

term_3 = term_1 + term_2;
term_1 = term_2;
term_2 = term_3;
}
System.out.println (“the sum of even value numbers is: “);
System.out.println (sum);
}
}

Project Euler Problem #1 Solution in Java

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

Solution in Java:

package problem1;
/**
* @author Khuram Ali
*/
public class Problem1
{

public static void main(String[] args)
{
int maxNum = 1000;
int result = 0;

for (int i = 2; i < maxNum; i++)
{
if (i % 5 == 0)
{
result += i;
}else if (i % 3 == 0)
{
result += i;
}
}
System.out.println (“total of multiples of 3 or 5: “) ;
System.out.println (result);
}
}