Answers for "bubble sort string java"

12

bubble sort java

public class BubbleSortExample {  
    static void bubbleSort(int[] arr) {  
        int n = arr.length;  
        int temp = 0;  
         for(int i=0; i < n; i++){  
                 for(int j=1; j < (n-i); j++){  
                          if(arr[j-1] > arr[j]){  
                                 //swap elements  
                                 temp = arr[j-1];  
                                 arr[j-1] = arr[j];  
                                 arr[j] = temp;  
                         }  
                          
                 }  
         }  
  
}
Posted by: Guest on September-17-2020
3

bubble sort java

public class DemoBubbleSort
{
   void bubbleSort(int[] arrNum)
   {
      int num = arrNum.length;
      for(int a = 0; a < num - 1; a++)
      {
         for(int b = 0; b < num - a - 1; b++)
         {
            if(arrNum[b] > arrNum[b + 1])
            {
               int temp = arrNum[b];
               arrNum[b] = arrNum[b + 1];
               arrNum[b + 1] = temp;
            }
         }
      }
   }
   void printingArray(int[] arrNum)
   {
      int number = arrNum.length;
      for(int a = 0; a < number; ++a)
      {
         System.out.print(arrNum[a] + " ");
         System.out.println();
      }
   }
   public static void main(String[] args)
   {
      DemoBubbleSort bs = new DemoBubbleSort();
      int[] arrSort = {65, 35, 25, 15, 23, 14, 95};
      bs.bubbleSort(arrSort);
      System.out.println("After sorting array: ");
      bs.printingArray(arrSort);
   }
}
Posted by: Guest on October-23-2020
0

how to bubblesort a string array in java

public class BubbleSort {

	
	static int MAX = 100;
	
	public static void sortStrings (String[] arr, int n) {
		
		String temp;
		
		//Sorting strings using bubble sort
		for (int j = 0; j < n; j++) {
			for (int i = j + 1; i < n; i++) {
				if (arr[j].compareTo(arr[i]) > 0) {
					temp = arr[j];
					arr[j] = arr[i];
					arr[i] = temp;
				}
			}
		}
	}
	
	public static void main(String[] args) {
		
		String[] arr = { "right","subdued","trick","crayon","punishment","silk","describe","royal","prevent","slope" };
		
		int n = arr.length;
		sortStrings(arr, n);
		System.out.println("Strings in sorted are: ");
		for (int i = 0; i < n; i++)
			System.out.println("String " + (i + 1) + " is " + arr[i]);
	}


}
Posted by: Guest on May-17-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language