Choosing Java Sort Exchange does not work as desired

My task: to create a sort that will work with strings and integers.

Hello, I was wondering if anyone could tell me why my list will not be sorted, no matter what list I enter, it just goes back in the same order. I am new to StackOverflow and JavaProgramming in general. I regret any beginner mistakes that I am making right now.

public class selectionSort {
    public static void main(String[] args) {
        sort(args);
        printArray(args);
    }

    public static void sort(String[] array) {
        int outer = 0;
        while (outer < array.length - 1) {
            outer++;
            int minimumIndex = outer;
            int inner = outer + 1;
            while (inner < array.length) {
                inner++;
                if (inner < minimumIndex) {
                    minimumIndex = inner;
                }
            }
            //exchange
            String temp = array[minimumIndex];
            array[minimumIndex] = array[outer];
            array[outer] = temp;
        }
    }

    public static void printArray(String[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }
}
+4
source share
4 answers

You do not compare the value of the rows in the array:

            if (inner < minimumIndex) {
                minimumIndex = inner;
            }

Perhaps you need this (if you want to sort the strings by intString value ):

            if (Integer.parseInt(array[inner]) < Integer.parseInt(array[minimumIndex])) {
                minimumIndex = inner;
            }

Tested Code:

public static void sort(String[] array) {
    int outer = 0;
    while (outer < array.length - 1) {
        int minimumIndex = outer;
        int inner = outer + 1;
        while (inner < array.length) {
            if (Integer.parseInt(array[inner]) < Integer.parseInt(array[minimumIndex])) {
                minimumIndex = inner;
            }
            inner++;
        }
        //exchange
        String temp = array[minimumIndex];
        array[minimumIndex] = array[outer];
        array[outer] = temp;
        outer++;
    }
}

If you want to sort it by line:

            if (array[inner].compareTo(array[minimumIndex]) < 0) {
                minimumIndex = inner;
            }
+1

Java Array sort(). .

Arrays.sort(array);

,

//import Arrays module
import java.util.Arrays;

public class selectionSort {
    public static void main(String[] args) {
        sort(args);
        printArray(args);
    }

    public static void sort(String[] array) {
        //sort your array
        Arrays.sort(array);
    }
    public static void printArray(String[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }
}

, http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html

+1

, , ,

public class sorting {

    public static void main(String arg[])throws Exception{

        for(int s = 0; s <= arg.length - 1; s++){

            for(int k = 0; k <= arg.length - 2; k++){

                if(j[k] > j[k+1]){ //comparing array values

                    int temp = 0;
                    temp = j[k]; //storing value of array in temp variable 
                    j[k] = j[k+1]; //swaping values
                    j[k+1] = temp; //now storing temp value in array
                }    //end if block             
            }  // end inner loop    
        }
        //end outer loop
        for(int s = 0; s <= j.length - 1; s++){
            System.out.println(j[s]); //retrieving values of array in ascending order 
        }   
    }
}
+1

,

// 1. Find the minimum value in the list.
// 2. Swap the minimum value with the value in the first position.
// 3. move the insertion point for the minimum along one.
// 4. Repeat the steps above for the remainder of the list

public static void yoursort(String[]array) {
    int currentIndex = 0;
    while(currentIndex < array.length-1) {
        currentIndex++;
        int indexOfMinimum = currentIndex;           
        int candidateMinIndex = currentIndex + 1;
        while(candidateMinIndex < array.length) {
            candidateMinIndex++;
            if(candidateMinIndex < indexOfMinimum) {
                indexOfMinimum = candidateMinIndex;
            }
        }
        swap(array, currentIndex, indexOfMinimum);
    }
}

public static void mysort(String[]array) {
    int currentIndex = 0;
    while(currentIndex < array.length-1) { // dont need to do the last one
        int indexOfMinimum = findIndexOfMinimum(array, currentIndex);
        swap(array, currentIndex, indexOfMinimum);
        currentIndex++;
    }
}

protected static int findIndexOfMinimum(String[] array, int startIndex) {
    int indexOfMinimum = startIndex; // start off asumming next is minimum
    int candidateIndex = startIndex + 1; // can shift candidate along to second element.
    while(candidateIndex < array.length) {  
        // compare candidate and minimum
        if ( array[candidateIndex].compareTo(array[indexOfMinimum]) < 0 ) {
            // ie array[candidateIndex] before/smaller array[indexOfMinimum] 
            indexOfMinimum = candidateIndex;        
        }                    
        candidateIndex++;
    }
    return indexOfMinimum;
}

protected static void swap(String[] array, int toIndex, int fromIndex) {
    if ( toIndex == fromIndex ) {
        // fast return if swapping with self.
        return;
    }

    String temp = array[toIndex];
    array[toIndex] = array[fromIndex];
    array[toIndex] = temp;
}

, . , , .

+1

Source: https://habr.com/ru/post/1612176/


All Articles