forked from jean/GDP-Uebungen
Added bubble sort algorithm
This commit is contained in:
parent
888399464e
commit
65f3c0e022
@ -12,7 +12,7 @@ public class Aufgabe2 {
|
||||
while(true){
|
||||
userInterface();
|
||||
System.out.print("\n\n Check anoter string? [y/N]: ");
|
||||
if(scanner.nextLine().charAt(0)!='y') break;
|
||||
if(scanner.nextLine()!="y") break;
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
@ -23,6 +23,7 @@ public class Aufgabe2 {
|
||||
stringAsCharArr = normalize(stringAsCharArr);
|
||||
char[] usedCharArr = usedChars(stringAsCharArr);
|
||||
int[] charAmountArr = countChars(usedCharArr ,stringAsCharArr);
|
||||
sort(charAmountArr,usedCharArr);
|
||||
histogramm(20,usedCharArr, charAmountArr);
|
||||
}
|
||||
|
||||
@ -36,7 +37,7 @@ public class Aufgabe2 {
|
||||
int max = maximum(charAmount);
|
||||
for(int i=0;i<availableChars.length;i++) {
|
||||
System.out.print("\""+availableChars[i]+"\": ");
|
||||
if(max>width)
|
||||
if(max>width) // scale if max width is exceeded
|
||||
for(int j=0;j<scale(charAmount[i],max,width);j++) System.out.print("#");
|
||||
else
|
||||
for(int j=0;j<charAmount[i];j++) System.out.print("#");
|
||||
@ -48,7 +49,21 @@ public class Aufgabe2 {
|
||||
double calc = (double) value/oldMax*newMax;
|
||||
return (int) calc;
|
||||
}
|
||||
|
||||
// Bubble Sort
|
||||
public static void sort(int[] amounts, char[] chars){
|
||||
for(int i=amounts.length-1;i>0; i--)
|
||||
for(int j=0;j<i;j++)
|
||||
if(amounts[j]<amounts[j+1])
|
||||
swapItems(amounts,chars,j,j+1);
|
||||
}
|
||||
public static void swapItems(int[] amounts, char[] chars, int index1, int index2){
|
||||
char memC = chars[index1];
|
||||
int memI = amounts[index1];
|
||||
chars[index1] = chars[index2];
|
||||
amounts[index1] = amounts[index2];
|
||||
chars[index2] = memC;
|
||||
amounts[index2] = memI;
|
||||
}
|
||||
public static int maximum(int[] inputArray){
|
||||
int largestNumber = inputArray[0];
|
||||
for(int i=1;i<inputArray.length;i++)
|
||||
|
Loading…
x
Reference in New Issue
Block a user