You are on page 1of 4
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents. Get (https://www.programiz.com/learn-java?utm_campaign=programiz- App homepage&utm_source=programiz-website-java-app-popup) www.domain-name.com Maye 1 In this example, we will learn to execute bubble sort algorithm in Java. To understand this example, you should have the knowledge of the following Java programming (/java-programming) topics: » Java Methods (/java-programming/methods) * Java for Loo: (/java-programming/for-loop) » Java Arrays (/java-programming/arrays) Example: Java Program to Implement Bubble Sort Algorithm Thank you for printing our content at www.domain-name.com. Please check back soon for new contents. Get (https://www.programiz.com/learn-java2utm_campaign=programiz- App homepage&utm_source=programiz-website-java-app-popup) www.domain-name.com // create an object of scanner // to take input from the user Scanner input = new Scanner (System. in); // method to perform bubble sort void bubbleSort(int array[]) { int size = array.length; // for ascending or descending sort System. out .println("Choose Sorting Order:"); System.out.println("1 for Ascending \n2 for Descending"); int sortOrder = input.nextInt(); // run loops two times // first loop access each element of the array for (int i = 0; i < size - 1; i++) // second loop performs the comparison in each iteration for (int j = 0; j < size - i - 1; j++) // sort the array in ascending order if (sortrder == 1) { . Output 1 Choose Sorting Order 1 for Ascending 2 for Descending 1 Sorted Array [-9, -2, 0, 11, 45) In this case, we have entered 1 as input. Hence, the program sort the array in ascending order. Output 2 Thank you for printing our content at www.domain-name.com. Please check back soon for new contents. Get (https://www.programiz.com/learn-java?utm_campaign=programiz- App homepage&utm_source=programiz-website-java-app-popup) www.domain-name.com In this case, we have entered 2 as input. Hence, the program sort the array in descending order. If you want to learn more about the bubble sort algorithm, visit Bubble Sort Algorithm (/dsa/bubble-sort), Note: We have used the Java Scanner Class (/java-programming/scanner) to take input from the user. ‘Share on: lhttps://www.facebook.com/sharer/sharer.php? @fhttes: ‘twitter.com/intent/tweet? ushttps://www.programiz.com/java- text=Check%20this%20amazing%2¢ programming/examples/bubble-sort) programming/examples/bubble-sort Did you find this article helpful? Related Examples Implement Quick Sort Algorithm Thank you for printing our content at www.domain-name.com. Please check back soon for new contents. Get (https://www.programiz.com/learn-java?utm_campaign=programiz- App homepage&utm_source=programiz-website-java-app-popup) www.domain-name.com Java Example Sort Elements in Lexicographical Order (Dictionary Order) (Gava-programming/examples/lexicographical-order-words) Jeve Example Implement Binary Search Algorithm (/java-programming/examples/binary-search)

You might also like