document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.util.Scanner;
  2. public class BubbleSort {
  3. static void bubbleSort(int[] arraytest) {
  4. int n = arraytest.length; //length of the array is initialized to the integer n
  5. int temp = 0; //A temporary variable called temp is declared as an integer and initialized to 0
  6. for(int i=0; i < n; i++){ // first for loop performs multiple iterations
  7. for(int j=1; j < (n-i); j++){
  8. if(arraytest[j-1] > arraytest[j]){ // if loop compares the adjacent numbers
  9. // swaps the numbers
  10. temp = arraytest[j-1]; // assigns the greater number to temp variable
  11. arraytest[j-1] = arraytest[j]; // shifts the lesser number to the previous position
  12. arraytest[j] = temp; // bigger number is then assigned to the right hand side
  13. }
  14. }
  15. }
  16. }
  17. public static void main(String[] args) {
  18. int arraytest[] ={23,16,3,42,75,536,61}; // defining the values of array
  19. System.out.println("Array Before Doing Bubble Sort");
  20. for(int i=0; i < arraytest.length; i++){ // for loop used to print the values of array
  21. System.out.print(arraytest[i] + " ");
  22. }
  23. System.out.println();
  24. bubbleSort(arraytest); // array elements are sorted using bubble sort function
  25. System.out.println("Array After Doing Bubble Sort");
  26. for(int i=0; i < arraytest.length; i++){
  27. System.out.print(arraytest[i] + " "); // for loop to print output values from array
  28. }
  29. }
  30. }
');