
R/sir, firstly thanks to help me so much. but now it can sort string very well but in case of integers as it treats integers here as string, it sorting like this: i/p: 12 14 11 3 sorted value: 11 12 13 3 this is the problem here......
import java.util.*; public class StringArraySelectionSort {
public static void main(String[] args)throws Exception{
Scanner input=new Scanner(System.in);
System.out.println("Enter string: ");
String[] array = new String[5];
for(int i=0;i<5;i++){
array[i]=input.next();
}
selectionSort(array);
for(int i = 0; i < array.length; i++){
System.out.print(array[i]+" ");
}
}
static String[] selectionSort(String[] array)
{
for (int i = 1; i < array.length; i++) {
int s = i-1;
for (int j = i; j < array.length; j++) {
if (array[j].compareTo(array[s]) < 0) {
s = j;
}
}
String temp = array[i-1];
array[i-1] = array[s];
array[s] = temp;
}
return array;
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.