Kiran
Kiran

Reputation: 199

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

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 .

Upvotes: 2

Views: 14871

Answers (9)

RAJU BAIN
RAJU BAIN

Reputation: 1

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

}
}

Upvotes: 0

Susie
Susie

Reputation: 5138

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

Upvotes: 4

Debabrata Bardhan
Debabrata Bardhan

Reputation: 47

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

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

Upvotes: 0

Yogesh Deshmukh
Yogesh Deshmukh

Reputation: 1

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

}

Upvotes: -1

Apetrei Ionut
Apetrei Ionut

Reputation: 323

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

Upvotes: 0

Marko Topolnik
Marko Topolnik

Reputation: 200158

This would be the minimal code which uses an array of primitive 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; }

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

Upvotes: 2

mdl
mdl

Reputation: 426

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]

Upvotes: 0

Bhavik Ambani
Bhavik Ambani

Reputation: 6657

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]

Upvotes: 1

Neeraj Krishna
Neeraj Krishna

Reputation: 1615

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

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.

Upvotes: 0

Related Questions