Bubble Shorting in StringArray
Previous | Home | Next |
Following Program for Bubble Shorting in the StringArray(Increasing and Decreasing order).
/*
* Save as a ShoringStringArray.java
* Program for BubbleShoting in the StringArray(Increasing and Decreasing order)
*/
package r4r.co.in;
public class ShoringStringArray {
//Declare some Element into array
private static String array[] = {
"Aa", "Happen", "Bb", "Clock", "Ca", "Evil", "Habbit", "of", "Add"};
public static void main(String[] args) {
{
//Shortion of an Array in Increasing order
System.out.println("After Shorting,Array in Increasing order:");
for (int j = 0; j < array.length; j++) {
for (int i = j + 1; i < array.length; i++) {
/*
* Compare two string and fill the element
* into a new string "t"
*/
if (array[i].compareTo(array[j]) < 0) {
String t = array[j];
array[j] = array[i];
array[i] = t;
}
}
//Dispaly the Element
System.out.println(array[j]);
}
}
{
//Shortion of an Array in Decreasing order
System.out.println("\nAfter Shorting,Array in Decreasing order:");
for (int j = array.length - 1; j >= 0; j--) {
for (int i = j - 0; i < array.length; i++) {
if (array[i].compareTo(array[j]) < 0) {
String t = array[j];
array[j] = array[i];
array[i] = t;
}
}
//Display the Element
System.out.println(array[j]);
}
}
}
}
After Shorting,Array in Increasing order:
Aa
Add
Bb
Ca
Clock
Evil
Habbit
Happen
ofAfter Shorting,Array in Decreasing order:
of
Happen
Habbit
Evil
Clock
Ca
Bb
Add
Aa
Previous | Home | Next |