You are on page 1of 11

GreatestCommonDivisor public class GreatestCommonDivisor { public static int gcd(int p, int q) { if (q == 0) { return p; } return gcd(q, p % q); }

prime number int i; for (i=1; i < num; i++ ){ int j; for (j=2; j<i; j++){ int n = i%j; if (n==0){ break; } } if(i == j){ System.out.print(" "+i); } } } }

Comparison counting sort

public CountSort(int size) { thearray = new int [size] ; Random ran = new Random(); for(int i = 0 ; i< size; i++) { thearray[i] = ran.Next(i,i*size); }

Sequential Search int Sequential_Search(int target, int[] list, int length) { int index = 0; while (index < length) if (list[index] = = target) return index; else index++; return -1; } Sequential Search : for loop { for (int index = 0; index < length; index ++) if (list[index ] == searchValue) return index return -1;

finding the value of largest element in a list of numbers

public static int getMaxValue(int[] numbers){ int maxValue = numbers[0]; for(int i=1;i < numbers.length;i++){ if(numbers[i] > maxValue){ maxValue = numbers[i]; } } return maxValue; } check whether all elements in a given array are distinct 07 int main() 08 { 09 int arr[10] = {10, 20, 40, 90, 30, 60, 35, 10 40, 85, 90}; 11 int i, k, origVal = 0, newVal = 0; 12 13 14 for (i = 0; i < 10; i++) 15 { 16 origVal = arr[i]; 17 18 for (k = 1; k < 10; k++) 19 { 20 newVal = arr[k]; 21 22 23 if (origVal == arr[k]) 24 { cout<<"The Array does not contain 25 completely distinct values"<<endl; 26 } 27 else 28 {

cout<< "The Array is distinct"<<endl; 30 } 31 } 32 } 33 34 system("PAUSE"); 35 return 0; 36 37 } 29

multiplication of two n*n matrices class MatrixMultiply{ public static void main(String[] args) int array[][]= {{5,6,7},{4,8,9}}; int array1[][]= {{6,4,8},{5,7,8}}; int x= array.length; System.out.println("Matrix 1 : ");

for(int i = 0; i < x; i++) { for(int j = 0; j <= x; j++) { System.out.print(" "+ array[i][j]); } System.out.println(); } int y= array1.length; System.out.println("Matrix 2 : "); for(int i = 0; i < y; i++) { for(int j = 0; j <= y; j++) { System.out.print(" "+array1[i][j]); } System.out.println(); } System.out.println("Multiply of both matrix : "); for(int i = 0; i < y; i++) { for(int j = 0; j <= y; j++) { System.out.print(" "+(array[i][j]*array1[i][j])); } System.out.println(); } } }

Factorial

public class Factorial { // Evaluate n! public static long factorial( int n ) { if( n <= 1 ) // base case return 1; else return n * factorial( n - 1 ); }

Other Prog in Factorial public static void main(String[] args) { try{ BufferedReader object = new BufferedReader(new InputStreamReader(Syst em.in)); System.out.println("enter the number"); int a= Integer.parseInt(object.readLine()); int fact= 1; System.out.println("Factorial of " +a+ ":"); for (int i= 1; i<=a; i++){ fact=fact*i; } System.out.println(fact); } catch (Exception e){} } }

Fibonacci
public class Fibonacci { public static void main(String[] args) { int N = Integer.parseInt(args[0]); int f = 0, g = 1; for (int i = 1; i <= N; i++) { f = f + g; g = f - g; System.out.println(f); } } }

fibonacci numbers iterative public class FibonacciIterative { public static int fib(int n) { int prev1=0, prev2=1; for(int i=0; i<n; i++) { int savePrev1 = prev1; prev1 = prev2; prev2 = savePrev1 + prev2; } return prev1; }

Selection sort
public static void selectionSort1(int[] x) { for (int i=0; i<x.length-1; i++) { for (int j=i+1; j<x.length; j++) { if (x[i] > x[j]) { //... Exchange elements int temp = x[i]; x[i] = x[j]; x[j] = temp; } } } }

1. public void SelectionSort() { 2. for (int i = 0; i < myList.length - 1; i++) { 3. int smallist = getSmallest(i, myList.length-2); 4. swap(i, smallist); 5. } 6. 7. } 8. 9. private int getSmallest(int a, int b) { 10. int small = a; 11. for (int i = a+1; i < myList.length; i++) { 12. if (myList[i].compareTo(myList[a]) > 0) { 13. myList[a] = myList[i]; 14. } 15. } 16. return small; 17. }

bubble sort
public void bubbleSort(){ int temp; for(int j=size-1; j > 0; j--){ for(int i=0; i < j; i++){ if(arr[i] > arr[i+1]){ temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } } }

string matching String string1 = "foo"; String string2 = "FOO"; if (string1.equals(string2)) { // this line will not print because the // java string equals method returns false: System.out.println("The two strings are the same.") }

Mergesort
public class Mergesort public static void main(String[ ] args) { final String BLANKS = " "; // A String of two blanks int i; // Array index int[ ] data = { 1000, 80, 10, 50, 70, 60, 90, 20, 30, 40, 0, -1000 }; for (i = 0; i < data.length; i++) System.out.print(data[i] + BLANKS); System.out.println( ); mergesort(data, 1, data.length-2); System.out.println("The numbers are now:"); for (i = 0; i < data.length; i++) System.out.print(data[i] + BLANKS); System.out.println( ); }

Quicksort
void quicksort (int[] a, int lo, int hi) { // lo is the lower index, hi is the upper index // of the region of array a that is to be sorted int i=lo, j=hi, h; // comparison element x int x=a[(lo+hi)/2]; // do { partition

while (a[i]<x) i++; while (a[j]>x) j--; if (i<=j) { h=a[i]; a[i]=a[j]; a[j]=h; i++; j--; } } while (i<=j); // recursion if (lo<j) quicksort(a, lo, j); if (i<hi) quicksort(a, i, hi); }

Or other prog in Quicksort


public 13: { 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: class QuickSort public QuickSort(int[] anArray) { a = anArray; } /** Sorts the array managed by this sorter */ public void sort() { sort(0, a.length - 1); } public void sort(int low, int high) { if (low >= high) return; int p = partition(low, high);

31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: }

sort(low, p); sort(p + 1, high); } private int partition(int low, int high) { // First element int pivot = a[low]; // Middle element //int middle = (low + high) / 2; //int pivot = a[middle]; int i = low - 1; int j = high + 1; while (i < j) { i++; while (a[i] < pivot) i++; j--; while (a[j] > pivot) j--; if (i < j) swap(i, j); } return j; } /** Swaps two entries of the array. @param i the first position to swap @param j the second position to swap

*/ private void swap(int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } private int[] a;

Binary Search
import java.util.*; public class BinarySearch { public static void main(String[] args) { int[] intArray = new int[10]; int searchValue = 0, index; System.out.println("Enter 10 numbers"); Scanner input = new Scanner(System.in); for (int i = 0; i < intArray.length; i++) { intArray[i] = input.nextInt(); } System.out.print("Enter a number to search for: "); searchValue = input.nextInt(); index = binarySearch(intArray, searchValue); if (index != -1) { System.out.println("Found at index: " + index); } else { System.out.println("Not Found"); } } static int binarySearch(int[] search, int find) { int start, end, midPt; start = 0; end = search.length - 1; while (start <= end) { midPt = (start + end) / 2; if (search[midPt] == find) { return midPt; } else if (search[midPt] < find) { start = midPt + 1; } else { end = midPt - 1; } } return -1; }

You might also like