import java.util.*;
public class Java_CH9_HW1
{
public static void main(String[] args)
{
// TODO 自動產生的方法 Stub
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
int size1 = 0;
//s = args[0];
// "statistics";
size1 = s.length();
System.out.println("Bubblesort, size = " + size1);
char[] r = new char[size1];
s.getChars(0, size1, r, 0);
char_BubbleSort.size = size1;
char_BubbleSort.bubbleSort(r);
char_BubbleSort.printArray(r);
System.out.println(char_BubbleSort.getString(r));
}
}
class char_BubbleSort
{
// swap: interchange inside array
static int size = 0;
static void swap(char[] a, int i, int j)
{
char t = a[i];
a[i] = a[j];
a[j] = t;
}
// bubbleSort: very short code, but inefficient
static void bubbleSort(char[] a)
{
for (;;)
{
boolean sorted = true;
// a.length - 1
for (int i = 0; i < size - 1; i++)
if ((int) a[i] > (int) a[i + 1])
{
sorted = false;
swap(a, i, i + 1);
}
if (sorted)
break;
}
}
static void printArray(char[] a)
{
for (int i = 0; i < size ; i++)
{
if (i % 4 == 0)
System.out.println();
System.out.print(a[i] + " \t");
}
System.out.println();
}
static String getString(char[] a)
{
String str = "";
for (int i = 0; i < a.length; i++)
if (str.indexOf(a[i]) == -1)
str += a[i];
return str;
}
}
留言
張貼留言