Java program to perform Bubble Sort on Strings

Publicado em: 27 Maio 2021
no canal de: Aman Tripathi
60
6

INSTAGRAM :   / javac_programming  
To perform bubble sort on Strings we need to compare adjacent Strings and if they are not in the order then we need to swap those strings, this process needs to be done until we reach at the end. This way all the strings will be sorted in an ascending order, this process of sorting is known as bubble sorting.

Bubble Sort on Strings example
In the following example we have stored the strings in a String array and we are using nested for loops to compare adjacent strings in the array, if they are not in order we are swapping them using a temporary string variable temp.

Here we are using compareTo() method to compare the adjacent Strings.

public class JavaExample {
public static void main(String []args) {
String str[] = { "Ajeet", "Steve", "Rick", "Becky", "Mohan"};
String temp;
System.out.println("Strings in sorted order:");
for (int j = 0; j _ str.length; j++) {
for (int i = j + 1; i _ str.length; i++) {
// comparing adjacent strings
if (str[i].compareTo(str[j]) _ 0) {
temp = str[j];
str[j] = str[i];
str[i] = temp;
}
}
System.out.println(str[j]);
}
}
}


Nesta página do site você pode assistir ao vídeo on-line Java program to perform Bubble Sort on Strings duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Aman Tripathi 27 Maio 2021, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 60 vezes e gostou 6 espectadores. Boa visualização!