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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21168716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:05:23  来源:igfitidea点击:

Sort an array such a way that First half should be in ascending order Second half should be in descending order in java

javasorting

提问by Kiran

I have searched a lot in google but I didnt get any kind of solution I could use. Suppose input of array is:

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

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

then output must be like this:

那么输出必须是这样的:

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

by using Basic Java .

通过使用基本 Java 。

回答by Susie

  • 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}
  • 你的数组:{3,1,2,4,9,8,7,6,5,10}
  • 按升序排序:{1,2,3,4,5,6,7,8,9,10}
  • 将此数组分成两个半数组:{1,2,3,4,5}{6,7,8,9,10}
  • 按降序对第二个数组进行排序或将其倒序:{10, 9,8,7,6}
  • 将第二个数组添加到第一个数组 & 你得到:{1,2,3,4,5,10,9,8,7,6}

回答by Neeraj Krishna

You can use the java features to do this too ... Use

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

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

But the elements need to be objects ... The comparator needs to be changed while sorting the first half and second half of the array.

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

回答by Bhavik Ambani

Please find the below code

请找到以下代码

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));
    }

}

Output

输出

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

回答by Marko Topolnik

This would be the minimal code which uses an array of primitiveints:

这将是使用原始整数数组的最少代码:

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; }

If you're not ashamed of using wrapper objects, then this is unbeatable:

如果您不以使用包装器对象为耻,那么这是无与伦比的:

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));

回答by mdl

Should be a bit simpler than manually reversing each item in the second half.

应该比在后半部分手动反转每个项目要简单一些。

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]

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

回答by Apetrei Ionut

IPSOS isn't it?

益普索不是吗?

    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] + " ");
    }

回答by Debabrata Bardhan

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])

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

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

output: 1 3 5 7 9 10 8 6 4 2 (uniform acceding and descending )

输出: 1 3 5 7 9 10 8 6 4 2(统一加减)

回答by Yogesh Deshmukh

I hope this piece of code will help:

我希望这段代码会有所帮助:

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]);

}

回答by RAJU BAIN

    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]);
}

}
}