Sort integer array using Bubble Sort in java

  1. package com.instaceofjava;
  2.  
  3. public class Bubblesort {
  4.  
  5. public static void main(String[] args) {  
  6.  
  7. int a[]={23,5,6,66,1};
  8.  
  9. int n=a.length;
  10.  
  11.         int temp=0;
  12.  
  13. for(int i=0;i<n;i++){
  14.  
  15. for(int j=1;j<(n-i);j++){
  16.  
  17. if(a[j-1]>a[j]){
  18.    temp=a[j];
  19.   a[j]=a[j-1];
  20.   a[j-1]=temp;
  21.  
  22.   }  
  23.  
  24. }
  25.  
  26. }
  27.  
  28. for(int k=0;k<n;k++){
  29. System.out.println(a[k]);


  30. }



Output:
  1. 1,
  2. 5
  3. 6
  4. 23
  5. 66


How to find largest element in an array with index and value using array?


  • Lets see an example program to get largest element in an integer array. and also find second largest element in the same array.


  1. package com.instanceofjava; 
  2. public class Array {
  3.  
  4. public static void main(String[] args) {
  5.  
  6. int arr[]={1,120,56,78,87};
  7. int largest=arr[0];
  8. int smallest=arr[0];
  9. int small=0;
  10. int index=0;
  11.  
  12. for(int i=1;i<arr.length;i++){
  13.  
  14. if(arr[i]>largest){
  15.  
  16. largest=arr[i];
  17. index=i;
  18.  
  19. }
  20. else if(smallest>arr[i]){
  21.  
  22. smallest=arr[i];
  23. small=i;
  24.  
  25. }
  26. }
  27.  
  28. System.out.println(largest);
  29. System.out.println(index);
  30. System.out.println(smallest);
  31. System.out.println(small);
  32.  

  33. }


Output:
  1. 120
  2. 1
  3. 87
  4. 4


How to Add elements to hash map and Display?


  1. package com.instaceofjava; 
  2.  
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6. import java.util.Map.Entry;
  7.  
  8. public class Mapiterator {
  9.  
  10. public static void main(String[] args) {
  11.  
  12. HashMap map=new HashMap();
  13.  
  14. map.put(1, "indhu");
  15. map.put("d", "sindhu");
  16. map.put("3", "swathi");
  17.  
  18. if(!map.isEmpty()){
  19.  
  20. Iterator it=map.entrySet().iterator();
  21.  
  22. while(it.hasNext()){
  23.  
  24. Map.Entry obj=(Entry) it.next();
  25. System.out.println(obj.getValue());
  26.  
  27. }
  28.  
  29. }
  30.  
  31. }
  32.  
  33. }


Output:

  1. swathi 
  2. indhu
  3. sindhu

Count the number of occurrences of a character in a String?

  1. package com.instaceofjava; 
  2.  
  3. public class StringCount {
  4.  
  5. public static void main(String[] args)
  6. {
  7.  
  8. String str = "abcdcabcdacbdadbca";
  9.  
  10.     String findStr = "b";
  11.     int lastIndex = 0;
  12.     int count = 0;
  13.  
  14.     while (lastIndex != -1) {
  15.  
  16.     lastIndex = str.indexOf(findStr, lastIndex);
  17.  
  18.     if (lastIndex != -1) {
  19.     count++;
  20.     lastIndex += findStr.length();
  21.  
  22.     }
  23.     }
  24.     System.out.println(count);
  25. }
  26.  
  27. }


Output:
  1. 4

Sorting string without using string Methods?


  1. package com.Instanceofjava; 
  2. public class SortString {
  3.  
  4. public static void main(String[] args) {
  5.  
  6. String original = "edcba";
  7. int j=0;
  8. char temp=0;
  9.  
  10.   char[] chars = original.toCharArray();
  11.  
  12.   for (int i = 0; i <chars.length; i++) {
  13.  
  14.       for ( j = 0; j < chars.length; j++) {
  15.  
  16.        if(chars[j]>chars[i]){
  17.             temp=chars[i];
  18.            chars[i]=chars[j];
  19.            chars[j]=temp;
  20.        }
  21.  
  22.    }  
  23.  
  24. }
  25.  
  26. for(int k=0;k<chars.length;k++){
  27. System.out.println(chars[k]);
  28. }
  29.  
  30. }
  31.  
  32. }





Output:
  1. abcde


Sort the string using String Methods?

  1. package com.instanceofjava; 
  2.  
  3. public class SortString {
  4.  
  5. public static void main(String[] args) { 
  6.  
  7.   String original = "edcba";
  8.  
  9.   char[] chars = original.toCharArray();
  10.  
  11.   Arrays.sort(chars);
  12.  
  13.   String sorted = new String(chars);
  14.   System.out.println(sorted);

  15. }





OutPut:
  1. abcde



Fibonacci series using recursion in java

  1. package com.instaceofjavaforus;
      public class FibanacciRecursive {
    public void fibanci(int n1,int n2){
    int sum=0;
    if(n1==0){
    System.out.println(n1+"\n"+n2);
    }
    sum=n1+n2;
    if(sum<=100){
    System.out.println(sum);
    n1=n2;
    n2=sum;

    fibanci(n1,n2);

    }
    }

    public static void main(String[] args) {

          FibanacciRecursive fb=new FibanacciRecursive();
          fb.fibanci(0,1);
    }

    }
    Output:
    0
    1
    1
    2
    3
    5
    8
    13
    21
    34
    55
    89
Select Menu