In: Computer Science
Your code must print all the steps in the output as an Eg>
When you run your Merge sort code the sequence of output should be:
1) Enter input sequence
2) Show n/2 division of the input sequence
3) keep showing n/2 division of your sequence until you get one attribute
4) Sort first two numbers
5) Make a stack of 4 by merging 2 * 2 and sort them
6) keep showing merging and sorting until you show the final merging of last two stacks and sort them.
Similarly, show all the steps for Bubble sort.
Mostly need Bubble in Java, please.v
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;
}
}
}
}
public static void main(String[] args) {
int arr[] ={3,60,35,2,45,320,5};
System.out.println("Array Before Bubble Sort");
for(int i=0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
bubbleSort(arr);//sorting array elements using bubble sort
System.out.println("Array After Bubble Sort");
for(int i=0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
}
}