Program to Implement Bottom-Up approach in Java

Below is the implementation of the Bottom-Up approach in Java:

Java




// Java Program to implement BottomUp Approach
import java.util.*;
  
public class BottomUpApproach {
  
    // function to swap elements
    public static void swap(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
  
    // function to perform Bubble Sort
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            boolean swap = false;
            for (int j = 0; j < array.length - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    swap(array, j, j + 1);
                    swap = true;
                }
            }
            if (!swap) {
                break;
            }
        }
    }
  
    // main function to test above functions
    public static void main(String[] args) {
        int[] array = {6, 5, 3, 1, 8, 7, 2, 4};
        // call the Bubble Sort function
        bubbleSort(array);
        // print the sorted array
        for (int i : array) {
            System.out.print(i + " ");
        }
    }
}


Output

1 2 3 4 5 6 7 8 



Output:

Explanation of the above Program:

In the above example,

  • Bubble sort algorithm is implemented.
  • At the beginning itself the smaller components are built.
  • For swapping and comparing two functions are implemented.
  • So, in an iterative mode the array sorting can be reused.

What is Top-Down and Bottom-Up Approach in Java?

In Java, Top-down and bottom-up are two different approaches to building a comprehensive Software development pattern. They are applicable in various contexts and programming languages, including Java. Let us explore how they differ from each other with appropriate examples.

Similar Reads

Top-Down Approach

In the top-down approach, it starts with the high-level overview where it focuses on overall functionality. And then it breaks down the larger problems into smaller bits, more detailed components. Before processing into the implementation part, it focuses on defining the interfaces and relationships between these detailed components. This approach mainly works on creating recursive function calls to solve the sub-modules. It focuses more on emphasizing the larger module at the beginning and later it refines that broader part into refined components. The primary advantage of the top-down approach is that it helps in understanding the overall functionality and structure easily....

Program to Implement Top-Down approach in Java

Below is the implementation of Top-Down Approach in Java:...

Bottom-Up Approach

...

Program to Implement Bottom-Up approach in Java

The Bottom-up Approach starts with the implementation of lower-level overview. It starts building smaller modules/components and combine them to get the bigger picture of the whole functionality in the end. This approach focuses more on the smaller modules first and all the system complexities are merged once the smaller modules are done with implementation. As this approach starts with smaller modules the communication between these modules is necessary. The Bottom-up approach is well suited for java programming language as it is object oriented the communication between the classes is required. In testing, implementation of this approach helps in enhancing the integration of modules more efficiently....

Conclusion

Below is the implementation of the Bottom-Up approach in Java:...