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.