Skip to content

Instantly share code, notes, and snippets.

Created December 19, 2017 12:50
Show Gist options
  • Save anonymous/d7529e4ed2427269aab6a54b2e3eaeba to your computer and use it in GitHub Desktop.
Save anonymous/d7529e4ed2427269aab6a54b2e3eaeba to your computer and use it in GitHub Desktop.
import java.util.Scanner;
class bubble {
public static void main(String []args) {
Scanner scan = new Scanner(System.in);
System.out.println("number of elements: ");
int n = scan.nextInt();
String array[] = new String[n];
System.out.println("Enter " + n + "elements");
for (int i=0; i<n; i++){
array[i] = scan.next();
}
for (int i=0; i<n-1;i++) {
for (int j=0; j<n-i-1;j++) {
if (array[j].compareToIgnoreCase(array[j+1]) > 0 )/* for descending order use < 0 */
{
String temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
System.out.println("\nsorted array: ");
for (int i=0; i<n;i++) {
System.out.println(array[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment