Bubble Sorting An Integer Array In Ascending and Descending Order

Bubble Sort Working

Bubble Sort Definition:
Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.

Complexity:
Worst Case: O(n^2) – reverse ordered list
Best Case: O(n) – fully sorted list

Note:

  • Bubble Sort is not a good solution if you have a large number of items to be sorted. This is due to the time Complexity.
  • Bubble Sort can however be used to quickly find if a list is already sorted.

Below is my java implementation of Bubble Sort algorithm. I have provided methods to do sorting in both ascending and descending order. This program sorts an integer array, although it can also be extended to do sorting for doubles and floats.

package com.icodejava.blog.datastructure;

/**
 * @author Kushal Paudyal
 * www.icodejava.com
 * Created On - Feb 18, 2014
 * Last Modified On - Feb 18, 2014
 */
public class BubbleSortInteger {

	public static void main(String args[]) {
		int[] unsortedArray = { 6, 5, 2, 5, 6, 3, 4, 2, 7, 8, 9, 0 };
		printArray("Unsorted Array:", unsortedArray);

		int[] sortedArray = bubbleSortAscending(unsortedArray);

		printArray("Sorted Array (Ascending):", sortedArray);

		sortedArray = bubbleSortDescending(unsortedArray);

		printArray("Sorted Array (Descending):", sortedArray);

	}

	private static int[] bubbleSortDescending(int[] unsortedArray) {

		for (int index = unsortedArray.length - 1; index > 0; index--) {
			for (int bubbleIndex = 0; bubbleIndex < index; bubbleIndex++) {

				if (unsortedArray[index] > unsortedArray[bubbleIndex]) {
					swap(unsortedArray, index, bubbleIndex);
				}

			}
		}

		return unsortedArray; // which is now sorted
	}

	private static int[] bubbleSortAscending(int[] unsortedArray) {

		for (int index = unsortedArray.length - 1; index > 0; index--) {
			for (int bubbleIndex = 0; bubbleIndex < index; bubbleIndex++) {

				if (unsortedArray[index] < unsortedArray[bubbleIndex]) {
					swap(unsortedArray, index, bubbleIndex);
				}

			}
		}

		return unsortedArray; // which is now sorted
	}

	private static void swap(int[] unsortedArray, int firstIndex, int secondIndex) {
		if (unsortedArray == null || firstIndex < 0 || firstIndex > unsortedArray.length
				|| secondIndex < 0 || secondIndex > unsortedArray.length) {
			return;
		}
		int tempInteger = unsortedArray[firstIndex];
		unsortedArray[firstIndex] = unsortedArray[secondIndex];
		unsortedArray[secondIndex] = tempInteger;

	}

	private static void printArray(String string, int[] array) {

		if (array != null && array.length > 0) {
			System.out.print(string + " ");
			for (int i = 0; i < array.length; i++) {
				System.out.print((i > 0 ? "," : "") + array[i]);
			}
		}

		System.out.println();

	}

}

The out of running above program:

Unsorted Array: 6,5,2,5,6,3,4,2,7,8,9,0
Sorted Array (Ascending): 0,2,2,3,4,5,5,6,6,7,8,9
Sorted Array (Descending): 9,8,7,6,6,5,5,4,3,2,2,0

  1. Implementing a Simple LIFO Stack in Java using LinkedList
  2. Implementing a Simple FIFO Queue in Java Using Linked List
  3. In Place Matrix (2D Array) Clockwise and Counterclockwise Rotation – Java Implementation
  4. Matrix (2D Array) Clockwise and Counterclockwise Rotation with Extra Buffer – Java Implementation
  5. Array Max Integer Finder (With Big O Analysis)
  6. A Basic Implementation of Binary Tree in Java
  7. A Basic Implementation of Generic Tree in Java
  8. Basic example of implementing Singly Linked List in Java
  9. How to Add and Remove nodes in an unsorted Linked List
  10. Rotating a two dimensional integer array In-Place and using extra memory
  11. How to reverse a Singly Linked List iteratively and recursively
  12. Sorted Circular Linked List Implementation And Insert Operation
  13. Finding Mean Value Of An Integer Array In Java
  14. How to intersect two sets in Java using java.util.Set data structure.
  15. Bubble Sorting An Integer Array In Ascending and Descending Order
  16. How to split strings and separate the words with spaces
  17. How to easily sort characters in a String?
Tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *