Showing posts with label Basic Java Programs. Show all posts
Showing posts with label Basic Java Programs. Show all posts

Tuesday, 25 August 2015

Java Practice 11

Write a program to implement Selection sort?

  1. public class MySelectionSort {

  2. public static int[] doSelectionSort(int[] arr){

  3. for (int i = 0; i < arr.length - 1; i++)
  4. {
  5. int index = i;
  6. for (int j = i + 1; j < arr.length; j++)
  7. if (arr[j] < arr[index])
  8. index = j;

  9. int smallerNumber = arr[index]; 
  10. arr[index] = arr[i];
  11. arr[i] = smallerNumber;
  12. }
  13. return arr;
  14. }

  15. public static void main(String a[]){

  16. int[] arr1 = {10,34,2,56,7,67,88,42};
  17. int[] arr2 = doSelectionSort(arr1);
  18. for(int i:arr2){
  19. System.out.print(i);
  20. System.out.print(", ");
  21. }
  22. }
  23. }

Java Practice 10

Write a program to implement Insertion sort?

  1. public class InsertionSort
  2.  {
  3. public static void main(String a[])
  4. {
  5. int[] arr1 = {10,34,2,56,7,67,88,42};
  6. int[] arr2 = doInsertionSort(arr1);
  7. for(int i:arr2){
  8. System.out.print(i);
  9. System.out.print(", ");
  10. }
  11. }

  12. public static int[] doInsertionSort(int[] input){

  13. int temp;
  14. for (int i = 1; i < input.length; i++) {
  15. for(int j = i ; j > 0 ; j--){
  16. if(input[j] < input[j-1]){
  17. temp = input[j];
  18. input[j] = input[j-1];
  19. input[j-1] = temp;
  20. }
  21. }
  22. }
  23. return input;
  24. }
  25. }

Java Practice 9

Write a program to implement quick sort?
  1. public class QuickSort
  2.  {
  3. private int array[];
  4. private int length;

  5. public void sort(int[] inputArr) {

  6. if (inputArr == null || inputArr.length == 0) {
  7. return;
  8. }
  9. this.array = inputArr;
  10. length = inputArr.length;
  11. quickSort(0, length - 1);
  12. }

  13. private void quickSort(int lowerIndex, int higherIndex) {

  14. int i = lowerIndex;
  15. int j = higherIndex;
  16. // calculate pivot number, I am taking pivot as middle index number
  17. int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
  18. // Divide into two arrays
  19. while (i <= j) {
  20. /**
  21. * In each iteration, we will identify a number from left side which
  22. * is greater then the pivot value, and also we will identify a number
  23. * from right side which is less then the pivot value. Once the search
  24. * is done, then we exchange both numbers.
  25. */
  26. while (array[i] < pivot) {
  27. i++;
  28. }
  29. while (array[j] > pivot) {
  30. j--;
  31. }
  32. if (i <= j) {
  33. exchangeNumbers(i, j);
  34. //move index to next position on both sides
  35. i++;
  36. j--;
  37. }
  38. }
  39. // call quickSort() method recursively
  40. if (lowerIndex < j)
  41. quickSort(lowerIndex, j);
  42. if (i < higherIndex)
  43. quickSort(i, higherIndex);
  44. }

  45. private void exchangeNumbers(int i, int j) {
  46. int temp = array[i];
  47. array[i] = array[j];
  48. array[j] = temp;
  49. }

  50. public static void main(String a[]){

  51. QuickSort sorter = new QuickSort();
  52. int[] input = {24,2,45,20,56,75,2,56,99,53,12};
  53. sorter.sort(input);
  54. for(int i:input){
  55. System.out.print(i);
  56. System.out.print(" ");
  57. }
  58. }
  59. }

Java Practice 8

Write a program for merge sort?
  1. public class MergeSort {
  2. private int[] array;
  3. private int[] tempMergArr;
  4. private int length;
  5. public static void main(String a[]){
  6. int[] inputArr = {45,23,11,89,77,98,4,28,65,43};
  7. MergeSort mms = new MergeSort();
  8. mms.sort(inputArr);
  9. for(int i:inputArr){
  10. System.out.print(i);
  11. System.out.print(" ");
  12. }
  13. }
  14. public void sort(int inputArr[]) 
  15. {
  16. this.array = inputArr;
  17. this.length = inputArr.length;
  18. this.tempMergArr = new int[length];
  19. doMergeSort(0, length - 1);
  20. }
  21. private void doMergeSort(int lowerIndex, int higherIndex) {

  22. if (lowerIndex < higherIndex) {
  23. int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
  24. // Below step sorts the left side of the array
  25. doMergeSort(lowerIndex, middle);
  26. // Below step sorts the right side of the array
  27. doMergeSort(middle + 1, higherIndex);
  28. // Now merge both sides
  29. mergeParts(lowerIndex, middle, higherIndex);
  30. }
  31. }
  32. private void mergeParts(int lowerIndex, int middle, int higherIndex) 
  33. {
  34. for (int i = lowerIndex; i <= higherIndex; i++) {
  35. tempMergArr[i] = array[i];
  36. }
  37. int i = lowerIndex;
  38. int j = middle + 1;
  39. int k = lowerIndex;
  40. while (i <= middle && j <= higherIndex) {
  41. if (tempMergArr[i] <= tempMergArr[j]) {
  42. array[k] = tempMergArr[i];
  43. i++;
  44. } else {
  45. array[k] = tempMergArr[j];
  46. j++;
  47. }
  48. k++;
  49. }
  50. while (i <= middle) {
  51. array[k] = tempMergArr[i];
  52. k++;
  53. i++;
  54. }

  55. }
  56. }

Java Practice 7

Write a program for bubble sort?

  1. import java.util.Scanner;
  2.  
  3. class BubbleSort {
  4. public static void main(String []args) {
  5. int n, c, d, swap;
  6. Scanner in = new Scanner(System.in);
  7.  
  8. System.out.println("Input number of integers to sort");
  9. n = in.nextInt();
  10.  
  11. int array[] = new int[n];
  12.  
  13. System.out.println("Enter " + n + " integers");
  14.  
  15. for (c = 0; c < n; c++)
  16. array[c] = in.nextInt();
  17.  
  18. for (c = 0; c < ( n - 1 ); c++) {
  19. for (d = 0; d < n - c - 1; d++) {
  20. if (array[d] > array[d+1]) /* For descending order use < */
  21. {
  22. swap = array[d];
  23. array[d] = array[d+1];
  24. array[d+1] = swap;
  25. }
  26. }
  27. }
  28.  
  29. System.out.println("Sorted list of numbers");
  30.  
  31. for (c = 0; c < n; c++)
  32. System.out.println(array[c]);
  33. }
  34. }

Java Practice 6

Write a program for Binary Search?(input must be in ascending order)

  1. import java.util.Scanner;
  2.  
  3. class BinarySearch
  4. {
  5. public static void main(String args[])
  6. {
  7. int c, first, last, middle, n, search, array[];
  8.  
  9. Scanner in = new Scanner(System.in);
  10. System.out.println("Enter number of elements");
  11. n = in.nextInt();
  12. array = new int[n];
  13.  
  14. System.out.println("Enter " + n + " integers");
  15.  
  16.  
  17. for (c = 0; c < n; c++)
  18. array[c] = in.nextInt();
  19.  
  20. System.out.println("Enter value to find");
  21. search = in.nextInt();
  22.  
  23. first = 0;
  24. last = n - 1;
  25. middle = (first + last)/2;
  26.  
  27. while( first <= last )
  28. {
  29. if ( array[middle] < search )
  30. first = middle + 1;
  31. else if ( array[middle] == search )
  32. {
  33. System.out.println(search + " found at location " + (middle + 1) + ".");
  34. break;
  35. }
  36. else
  37. last = middle - 1;
  38.  
  39. middle = (first + last)/2;
  40. }
  41. if ( first > last )
  42. System.out.println(search + " is not present in the list.\n");
  43. }
  44. }

Java Practice 5

Write a program to print Alphabets?

  1. class Alphabets
  2. {
  3. public static void main(String args[])
  4. {
  5. char ch;
  6.  
  7. for( ch = 'a' ; ch <= 'z' ; ch++ )
  8. System.out.println(ch);
  9. }
  10. }

Java Practice 4

Write a program using If and else conditions?


  1. class Condition {
  2. public static void main(String[] args) {
  3. boolean learning = true;
  4.  
  5. if (learning) {
  6. System.out.println("Java programmer");
  7. }
  8. else {
  9. System.out.println("What are you doing here?");
  10. }
  11. }
  12. }

Java Practice 3

Write a program to print integers?

  1. class Integers {
  2. public static void main(String[] arguments) {
  3. int c; //declaring a variable
  4.  
  5. /* Using for loop to repeat instruction execution */
  6.  
  7. for (c = 1; c <= 10; c++) {
  8. System.out.println(c);
  9. }
  10. }
  11. }

Java Practice 2


Write a java program to check palindrome number.


  1. import java.util.*;
  2.  
  3. class Palindrome
  4. {
  5. public static void main(String args[])
  6. {
  7. String original, reverse = "";
  8. Scanner in = new Scanner(System.in);
  9.  
  10. System.out.println("Enter a string to check if it is a palindrome");
  11. original = in.nextLine();
  12.  
  13. int length = original.length();
  14.  
  15. for ( int i = length - 1; i >= 0; i-- )
  16. reverse = reverse + original.charAt(i);
  17.  
  18. if (original.equals(reverse))
  19. System.out.println("Entered string is a palindrome.");
  20. else
  21. System.out.println("Entered string is not a palindrome.");
  22.  
  23. }
  24. }

Java Practice


Write a program to check the given number is a prime number or not?
  1. package basics.java.com; 
  2. public class MyPrimeNumCheck 
  3. {
  4.  public boolean isPrimeNumber(int number)
  5. { 
  6. for(int i=2; i<=number/2; i++)
  7. {
  8.  if(number % i == 0)
  9. {
  10.  return false; 
  11. }
  12.  } 
  13. return true; 
  14. } 
  15. public static void main(String a[])
  16. { 
  17. MyPrimeNumCheck mpc = new MyPrimeNumCheck();
  18. System.out.println("Is17primenumber?"+mpc.isPrimeNumber(17));
  19. System.out.println("Is 19 prime number? "+mpc.isPrimeNumber(19));
  20. System.out.println("Is 15 prime number? "+mpc.isPrimeNumber(15));
  21.  }
  22.  }