through bubble in java code example

Example 1: 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);
   }
}

Example 2: bubble sort integers

for (int i = 0; i < n-1; i++) 
            for (int j = 0; j < n-i-1; j++) 
                if (arr[j] > arr[j+1]) 
                { 
                    // swap arr[j+1] and arr[i] 
                    int temp = arr[j]; 
                    arr[j] = arr[j+1]; 
                    arr[j+1] = temp; 
                }

Tags:

C Example