Bubble Sort App With User Input In Android Studio
I am new to android programming and I tried to do bubble sort by inputting numbers in one EditText and the sorted numbers will be outputted on the textview. The program has stopped
Solution 1:
you don't have initialized num. use following code
publicvoidBubbleSort() {
Spannable spn = Input.getText();
num = newint[spn.length()];
int count = 0;
for (int i = 0; i < spn.length(); i++){
if((spn.charAt(i)+"").matches(".*\\d.*")){
num[i] = Integer.parseInt(""+spn.charAt(i));
count++;
}
}
for (i = 0; i < count; i++) {
for (j = i + 1; j < count; j++) {
if (num[i] > num[j]) {
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
String result = "";
for (int i = 0; i < num.length; i++){
result += num[i] + " ";
}
Result.setText(result);
}
Post a Comment for "Bubble Sort App With User Input In Android Studio"