Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 排列 int[]sortedArray=新int[array.length]; int firstIndex=0,lastIndex=array.length-1; for(int i=0;i_Java_Arrays_Sorting - Fatal编程技术网

Java 排列 int[]sortedArray=新int[array.length]; int firstIndex=0,lastIndex=array.length-1; for(int i=0;i

Java 排列 int[]sortedArray=新int[array.length]; int firstIndex=0,lastIndex=array.length-1; for(int i=0;i,java,arrays,sorting,Java,Arrays,Sorting,排列 int[]sortedArray=新int[array.length]; int firstIndex=0,lastIndex=array.length-1; for(int i=0;i

排列 int[]sortedArray=新int[array.length]; int firstIndex=0,lastIndex=array.length-1; for(int i=0;i对于初学者来说,这可能是最好的方法。高级程序员可能会考虑对数组进行一次排序,然后重新排列元素。它应该有用。。。但逻辑是复杂的。我认为重新安排元素对于一个公司来说更为复杂beginner@StephenCThat我刚才就是这么说的。不是吗?
public class Foo {

    public static void main(String[] args) {
        // Take your original array
        int[] arr = { 1, 4, 5, 10, 6, 8, 3, 9 };
        // Use the Arrays sort method to sort it into ascending order (note this mutates the array instance)
        Arrays.sort(arr);
        // Create a new array of the same length
        int[] minMaxSorted = new int[arr.length];
        // Iterate through the array (from the left and right at the same time)
        for (int i = 0, min = 0, max = arr.length - 1; i < arr.length; i += 2, min++, max--) {
            // the next minimum goes into minMaxSorted[i]
            minMaxSorted[i] = arr[min];
            // the next maximum goes into minMaxSorted[i + 1] ... but
            // guard against index out of bounds for odd number arrays
            if (i + 1 < minMaxSorted.length) {
                minMaxSorted[i + 1] = arr[max];
            }
        }
        System.out.println(Arrays.toString(minMaxSorted));
    }
}
/**
 * Demonstrates an option for sorting an int[] array as requested,
 * by keeping a list of the array indices that has been sorted, and searching
 * for the next min / max.
 * This code is not optimal nor robust. It serves a demo for this option only.
 *
 */
public class AltSort  {

    //list of array elements that were sorted
    static Set<Integer> indexSorted ;

    public static void main (String[] args) throws java.lang.Exception      {
        //test case
        int[] array = new int[]{7,22,4,67,5,11,-9,23,48, 3, 73, 1, 10};
        System.out.println(Arrays.toString(altSort2(array)));

        //test case
        array = new int[]{ 1, 4, 5, 10, 6, 8, 3, 9 };
        System.out.println(Arrays.toString(altSort2(array)));
    }

    private static int[] altSort2(int[] array) {

        if((array == null) || (array.length == 0)) {
            System.err.println("Empty or null array can not be sorted.");
        }

        //returned array
        int[] sortedArray = new int[array.length];

        //flag indicating wether to look for min or max
        boolean lookForMin = true;

        int index = 0;
        while(index < array.length) {

            if(lookForMin) {
                sortedArray[index] = lookForArrayMin(array);

            }else {
                sortedArray[index] = lookForArrayMax(array);
            }

            index++;
            //alternate look for min / look for max
            lookForMin = ! lookForMin;
        }

        return sortedArray;
    }

    private static int lookForArrayMin(int[] array) {

        int minValue = Integer.MAX_VALUE;
        int minValueIndex = 0;

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

            //if array[i] is min and was not sorted before, keep it as min
            if( (array[i]< minValue) && ! indexSorted.contains(i) ) {

                minValue = array[i]; //keep min
                minValueIndex = i;   //keep min index
            }
        }

        //add the index to the list of sorted indices
        indexSorted.add(minValueIndex);
        return minValue;
    }

    private static int lookForArrayMax(int[] array) {

        int maxValue = Integer.MIN_VALUE; //max value
        int maxValueIndex = 0;   //index of max value

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

            //if array[i] is max and was not sorted before, keep it as max
            if( (array[i] > maxValue) && ! indexSorted.contains(i)) {
                maxValue = array[i]; //keep max
                maxValueIndex = i;   //keep max index
            }
        }

        //add the index to the list of sorted indices
        indexSorted.add(maxValueIndex);
        return maxValue;
    }
}
public class AltSort  {

    //list of array elements that were sorted
    static Set<Integer> indexSorted = new HashSet<Integer>();

    public static void main (String[] args) throws java.lang.Exception

    {
        //test case
        int[] array = new int[]{7,22,4,67,5,11,-9,23,48, 3, 73, 1, 10};
        System.out.println(Arrays.toString(altSort(array)));

        //test case
        array = new int[]{ 1, 4, 5, 10, 6, 8, 3, 9 };
        System.out.println(Arrays.toString(altSort(array)));
    }

    private static int[] altSort(int[] array) {

        if((array == null) || (array.length == 0)) {
            System.err.println("Empty or null array can not be sorted.");
        }

        Arrays.sort(array);

        //returned array
        int[] sortedArray = new int[array.length];
        int firstIndex = 0, lastIndex = array.length-1;

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

            if((i%2) == 0) { //even indices

                sortedArray[i] = array[firstIndex++];
            }
            else {
                sortedArray[i] = array[lastIndex --];
            }
        }

        return sortedArray;
    }
}