Java Program to sort array using bubble sort

  1. package com.instaceofjava; 

  2. public class BubbleSortExample{
  3.   
  4. int[] array={4,2,5,6,9,1};
  5.  
  6. int n = array.length;
  7. int k;
  8.  
  9. for (int m = n; m>= 0; m--) {
  10.  
  11. for (int j = 0; j < n-1; j++) {
  12.  
  13.    k = j + 1;
  14.  
  15.  if (array[j] > array[k]) {
  16.  
  17.        int temp;
  18.        temp = array[j];
  19.        array[j] = array[k];
  20.        array[k] = temp;
  21.  
  22. }
  23. }
  24.            
  25. for (int x = 0; x < array.length; x++) {
  26.  
  27. System.out.print(array[x] + ", ");
  28.  
  29.  }
  30.  
  31. System.out.println();
  32. }  
  33.  
  34. }
  35.  
  36. }



Output:

  1. 2, 4, 5, 6, 1, 9, 
  2. 2, 4, 5, 1, 6, 9, 
  3. 2, 4, 1, 5, 6, 9, 
  4. 2, 1, 4, 5, 6, 9, 
  5. 1, 2, 4, 5, 6, 9, 
  6. 1, 2, 4, 5, 6, 9, 
  7. 1, 2, 4, 5, 6, 9,



Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu