Bubble Sorting A String Array in Ascending And Descending Order

In my previous article, I provided an example of how you can do integer array sorting using bubble sort. In this article, I have provided a solution on how to do String array sort.

This program:

  • Sorts a string array in ascending order
  • Sorts a string array in descending order
  • Uses String’s in-built compareTo method to compare to entities.

package com.icodejava.blog.datastructure;

/**
 * @author Kushal Paudyal
 * www.icodejava.com
 * Created On -  Feb 18, 2014
 * Last Modified On - Feb 18, 2014
 */
public class BubbleSortString {
	
	public static void main(String args []) {
		String [] unsortedArray = {"M","F","B","E","Z","F","D","Q","R","S","H","A"};
		printArray("Unsorted Array:", unsortedArray);
		
		String [] sortedArray = bubbleSortAscending(unsortedArray);
		
		printArray("Sorted Array (Ascending):", sortedArray);
		
		sortedArray = bubbleSortDescending(unsortedArray);
		
		printArray("Sorted Array (Descending):", sortedArray);
		
		
	}

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

		for (int index = unsortedArray.length - 1; index > 0; index--) {
			for (int bubbleIndex = 0; bubbleIndex < index; bubbleIndex++) {
				/**
				 * using String's compareTo method to see if one element is
				 * bigger than the other.
				 */
				if (unsortedArray[index].compareTo(unsortedArray[bubbleIndex]) > 0) {
					swap(unsortedArray, index, bubbleIndex);
				}

			}
		}

		return unsortedArray; // which is now sorted
	}

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

		for (int index = unsortedArray.length - 1; index > 0; index--) {
			for (int bubbleIndex = 0; bubbleIndex < index; bubbleIndex++) {
				/**
				 * using String's compareTo method to see if one element is
				 * bigger than the other.
				 */
				if (unsortedArray[index].compareTo(unsortedArray[bubbleIndex]) < 0) {
					swap(unsortedArray, index, bubbleIndex);
				}

			}
		}

		return unsortedArray; // which is now sorted
	}

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

	}
	
	private static void printArray(String string, String[] unsortedArray) {

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

		System.out.println();

	}
}

The out of running above program:

Unsorted Array: M,F,B,E,Z,F,D,Q,R,S,H,A
Sorted Array (Ascending): A,B,D,E,F,F,H,M,Q,R,S,Z
Sorted Array (Descending): Z,S,R,Q,M,H,F,F,E,D,B,A

Tagged , , , , , , . Bookmark the permalink.

One Response to Bubble Sorting A String Array in Ascending And Descending Order

  1. Ririn says:

    Thanks buat script codenya, membantu saya mengerkajan tugas saya

Leave a Reply

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