对数组进行排序,使得前半部分应按升序排列。后半部分应按java中的降序排列

时间:2014-01-16 17:28:08

标签: java sorting

我在谷歌搜索了很多,但我没有得到任何我可以使用的解决方案。假设数组的输入是:

 {3,1,2,4,9,8,7,6,5,10} 

然后输出必须是这样的:

 {1,2,3,4,5,10,9,8,7,6} 

使用Basic Java。

9 个答案:

答案 0 :(得分:4)

Your array:  {3,1,2,4,9,8,7,6,5,10}
Sort it in ascending order: {1,2,3,4,5,6,7,8,9,10}
Break this array into two half arrays:  {1,2,3,4,5}{6,7,8,9,10}
Sort the second array in descending order or reverse it: {10, 9,8,7,6}
Add the second array to the first array & you get: {1,2,3,4,5,10,9,8,7,6}

答案 1 :(得分:2)

这将是使用原语 ints数组的最小代码:

static final int[] xs = {3,1,2,4,9,8,7,6,5,10};
static void sortAndReverse() {
  Arrays.sort(xs);
  for (int i = xs.length/2; i < dest(i); i++) {
    int tmp = xs[i]; xs[i] = xs[dest(i)]; xs[dest(i)] = tmp;
  }
  System.out.println(Arrays.toString(xs));
}
static int dest(int i) { return 3*xs.length/2-i-1; }

如果您对使用包装器对象并不感到羞耻,那么这是无与伦比的:

final Integer[] xs = {3,1,2,4,9,8,7,6,5,10};
final List<Integer> list = Arrays.asList(xs);
Collections.sort(list);
Collections.reverse(list.subList(list.size()/2, list.size()));
System.out.println(Arrays.toString(xs));

答案 2 :(得分:1)

请找到以下代码

import java.util.Arrays;

public class fre {

    public static void main(String[] args) {
        int[] vals = { 3, 1, 2, 4, 9, 8, 7, 6, 5, 10 };

        Arrays.sort(vals); // Sorts the basic first array
        int[] vals2 = Arrays.copyOfRange(vals, vals.length / 2, vals.length); // Gets the las values of the arrays i.e. it devies the array in multiple same part and another array is created

            // Below loop will reverse the second array
        for (int i = 0; i < vals2.length / 2; i++) {
            int temp = vals2[i];
            vals2[i] = vals2[vals2.length - 1 - i];
            vals2[vals2.length - 1 - i] = temp;
        }

        vals = Arrays.copyOfRange(vals, 0, vals.length / 2);
            // Final array array1and2  will be created where we will append first array with second array
        int[] array1and2 = new int[vals.length + vals2.length];
        System.arraycopy(vals, 0, array1and2, 0, vals.length);
        System.arraycopy(vals2, 0, array1and2, vals.length, vals2.length);
            // Prints the final result array
        System.out.println(Arrays.toString(array1and2));
    }

}

<强>输出

[1, 2, 3, 4, 5, 10, 9, 8, 7, 6]

答案 3 :(得分:0)

您也可以使用java功能执行此操作...使用

public static <T> void sort(T[] a,
        int fromIndex,
        int toIndex,
        Comparator<? super T> c)

但元素需要是对象......在对数组的前半部分和后半部分进行排序时,需要更改比较器。

答案 4 :(得分:0)

比手动翻转下半场的每个项目要简单一些。

Integer [] array = { 3,1,2,4,9,8,7,6,5,10 };
Arrays.sort(array);
Arrays.sort(array, array.length/2, array.length, new Comparator<Integer>(){
    @Override
    public int compare(Integer o1, Integer o2)
    {
        return -o1.compareTo(o2);
    }
});
System.out.println(Arrays.toString(array));

[1, 2, 3, 4, 5, 10, 9, 8, 7, 6]

答案 5 :(得分:0)

IPSOS不是吗?

    int m;
    if(array.length%2==0) 
        m=array.length/2;
    else
        m=(array.length+1)/2;

    for(int i=0; i<array.length; ++i){
        if(i<m){
            int min = i;
            for(int j=i+1; j<m;++j){

                if(array[min]>array[j]){
                    min=j;
                }
                int tem = array[i];
                array[i]=array[min];
                array[min]=tem;
            }
        }
        else {
            int max = i;
            for(int k=i+1; k<array.length; ++k){
                if(array[max]<array[k]){
                    max=k;
                }
                int te = array[i];
                array[i]=array[max];
                array[max]=te;
            }
        }           
    }
    for(int i=0;i<array.length;++i){
        System.out.print(array[i] + " ");
    }

答案 6 :(得分:0)

1. Sort the array input_Array[] 
2. j = lenght(input_Array)-1
3. loop i = lenght(input_Array)/2 to j
      swap(input_Array[i] , input_Array[j-i])

输入:3,1,2,4,9,8,7,6,5,10

输出:1 3 5 7 9 10 8 6 4 2(统一加入和降序)<​​/ p>

答案 7 :(得分:0)

    public class AscendingDecending {

    public static void main(String[]args) {
        int a[]= {2,3,2,5,7,5,6,3};
        int i,j,temp;

        //Traverse the element of array
        System.out.println("Input:");
        for(i=0; i<a.length; i++) {
            System.out.print("  "+ a[i]);
        }

        //lets move for ascending function
        System.out.println("");
        System.out.println("Output:");

        //Create a Swap Function for sorting
        for(i=0; i<a.length; i++) {
            for(j=i+1; j<a.length; j++) {
                if(a[i]>a[j]) {
                    temp= a[i];
                    a[i]= a[j];
                    a[j]= temp;
                }
            }
        }

    // Now the input is in sorted order
    for(i=0; i<a.length/2; i++) {
        System.out.print(" "+ a[i]);
    }

    //For Descending
    for(i=0; i<a.length; i++) {
        for(int j=i+1; j<a.length; j++) {
            if(a[i]<a[j]) {
                temp= a[i];
                a[i]= a[j];
                a[j]= temp;
            }
        }
    }

// Now the input is in sorted order
System.out.println(" ");
for(i=0; i<a.length/2; i++) {
    System.out.print(" "+ a[i]);
}

}
}

答案 8 :(得分:-1)

我希望这段代码能够提供帮助:

static void printarray(int[] arr, int len)
{
    Arrays.sort(arr);
    for (int i = 0; i < len / 2; i++) 
        System.out.println(arr[i]);
    for (int j = len - 1; j >= len / 2; j--)
    System.out.println(arr[j]);

}
相关问题