Skip to content

Instantly share code, notes, and snippets.

@jimmy087
Created May 4, 2013 06:14
Show Gist options
  • Save jimmy087/5516440 to your computer and use it in GitHub Desktop.
Save jimmy087/5516440 to your computer and use it in GitHub Desktop.
A method for bubble sort
public class Exercise9_9 {
public static void main(String[] args) {
String [] s1 = {"a", "b", "e", "f", "g", "d", "c", "h"};
sort(s1);
for (int i = 0; i < s1.length; i++) {
if (i < s1.length - 1) {
System.out.print(s1[i] + ", ");
}
else {
System.out.print(s1[i] + ".");
}
}
}
// A method for bubble sort.
public static void sort(String[] s1) {
int i;
boolean flag = true;
String temp;
while (flag) {
flag = false;
for (i = 0; i < s1.length - 1; i++) {
if (s1[i].compareToIgnoreCase(s1[i + 1]) > 0) {
temp = s1 [i];
s1[i] = s1[i + 1];
s1[i + 1] = temp;
flag = true;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment