How Do I Sort Array Elements Using Bubble Sort Algorithm ?

How Bubble Sort is implemented in Java ?.
How Bubble Sort is implemented in Java ?. Bubble Sort is a special sorting technique, which sorts elements by making multiple passes. While making each pass neighboring elements are compared in pairs. Generally the order is important, if the comparison comes out in decreasing order the numbers are swapped. If the order comes out in ascending order the values remain unchanged. It is called as Bubble Sort technique because the large value in each pass sinks to bottom while the smaller value bubbles out to top. After first successful pass the largest elements becomes the last element and after second
pass, second largest elements becomes the second last element. Bubble Sort is
implemented in Java by below small program : package com.hubberspot.sort.example; public class BubbleSort { public static void main(String[] args) { int[] numbers = {3, 4, 5, 6, 7, 2, -3, 4, 16, 14}; sort(numbers); for (int i = 0; i < numbers.length; i++) System.out.print(numbers[i] + " "); } public static void sort(int[] list) { boolean nextPass = true; for (int k = 1; k < list.length && nextPass; k++) { nextPass = false; for (int i = 0; i < list.length - k; i++) { if (list[i] > list[i + 1]) { int temp = list[i]; list[i] = list[i + 1]; list[i + 1] = temp; nextPass = true; } } } } }

For more of such hot topics read my other posts here :

Visit my new website

Article Written By jontymagicman

I am DInesh Varyani.

Last updated on 26-07-2016 98 0

Please login to comment on this post.
There are no comments yet.
Variables And Data Types In Java Programming Language.
How To Convert Decimal Number To Hexadecimal Number In A Java Program