Tuesday, December 21, 2021

String sorting using bubble sort

                String input = "geeksforgeeks";

char[] inputAr = input.toCharArray();

char temp;

for (int i = 0; i < inputAr.length ; i++) {// no of pass controller

for (int j = 0; j < inputAr.length -i- 1; j++) {

if (inputAr[j] > inputAr[j+1]) { //comparing with adjacent character.

temp = inputAr[j];

inputAr[j] = inputAr[j+1];

inputAr[j+1] = temp;

}

}

}

for (int i = 0; i < inputAr.length ; i++) {

System.out.println(inputAr[i]);

}

No comments:

Post a Comment

Abstract factory pattern

When single task can be done by multiple groups/family of objects and decision is taken at the runtime.