Created
August 17, 2012 14:43
-
-
Save els-pnw/3379334 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
public class SortStrings { | |
/** | |
* We'll do a basic bubble sort,. | |
* @param array | |
*/ | |
public static void MySort(String array[]) { | |
String tempWord; | |
int n = array.length; | |
for(int i = 0; i < n; i++) { | |
for(int j = 1; j < (n-i); j++) { | |
if (array[j-1].compareTo(array[j]) > 0) { | |
tempWord = array[j-1]; | |
array[j-1] = array[j]; | |
array[j] = tempWord; | |
} | |
} | |
} | |
} | |
/** | |
* Our main will prompt for number strings the user will enter | |
* then prompt for the strings. | |
* @param argv | |
* @throws IOException | |
*/ | |
public static void main(String [] argv) throws IOException { | |
BufferedReader stdin = | |
new BufferedReader(new InputStreamReader(System.in), 1); | |
// Lets determine the number of strings we'll sort | |
System.out.print("How many strings would you like to enter? "); | |
// I know, it's an odd place to do so, but we're going to | |
// init some variables here. | |
int numberOf = Integer.parseInt(stdin.readLine()); | |
String[] sortableStrings = new String[numberOf]; | |
// We're building an array now... | |
for (int i = 0; i < numberOf; i++) { | |
int x = i + 1; | |
System.out.print("Enter string " + x + ": "); | |
String s = stdin.readLine(); | |
sortableStrings[i] = s; | |
} | |
// The array is built, lets call our sort logic | |
// and return the sorted output. | |
MySort(sortableStrings); | |
for(int i = 0; i < sortableStrings.length; i++) { | |
System.out.print(sortableStrings[i] + " "); | |
} | |
System.out.println(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment