Hi, I know this forum doesn’t really focus on Java, but If I were to ask this on stack overflow I have a sense it would’ve been downvoted into oblivion. So I turn here.
I’m trying to write a basic sorting algo but I cant get it working. I want to swap the value of 101 into the first place of 104. So sort from Lowest to Highest. Can someone review this and tell me where I went wrong/ what I forgot/ how to fix it? Also trying to avoid built in methods as I want to practice with algorithm’s.
import java.util.Arrays;
public class sortString {
public static void main (String[]args)
{
String word = "hello"; // initizlize word
char array[] = word.toCharArray(); // create array of characters with length of word so 5
int values[] = new int[array.length];
for(int index = 0; index < array.length; ++index)
{
values[index] = array[index]; // gives all the chars numerical values
int lowestValue = values[0]; // first index in array value is 104
for(int j = 1; j < values.length; ++j)
{
if(values[j] < lowestValue) // if value[1] < value[0]
{
lowestValue = values[j]; // the value[0] = the value at value[1]
values[j] = lowestValue;
System.out.println(lowestValue);
}
}
}
// assign that value to beginning of array
// if values[1] < values[0] switch them
System.out.println(Arrays.toString(values) + "\n");