3

I understand how to sort an array by ascending and descending order, but there is a specific pattern I'm trying to create. For example, I have an array in a random order. How would I sort this array in the pattern? "smallest, largest, second smallest, second largest, thrid smallest, third largest..." etc. Any ideas?

int[] pattern = {8, 6, 1, 2, 3, 80, 56};

//This is the start

public class Test2 {

    public static void main(String[] args) {
        int[] array = {1,4,2,6,9,3,65,77,33,22};
        for (int i = 0; i < array.length; i++) {
            System.out.print(" " + array[i]);
        }
        wackySort(array);

    }

    //This sorts the array    
    public static void wackySort(int[] nums) {
        int sign = 0;
        int temp = 0;
        int temp2 = 0;
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums.length -1; j++) {
                if (nums[j] > nums[j+1]) {
                    temp = nums[j];
                    nums[j] = nums[j+1];
                    nums[j+1] = temp;

                }
            }
        }
        System.out.println();
        int firstPointer = 0;
        int secondPointer = nums.length -1;
        int[] newarray = new int[nums.length];
        for (int i = 0; i < nums.length; i+=2) {
            newarray[i] = nums[firstPointer++];
            newarray[i] = nums[secondPointer--];
        }
        for (int i = 0; i < newarray.length; i++) {
            System.out.print(" " + newarray[i]);
        }
    }
}
4
  • My approach would be to use an array-list and then there would me millions of ways to sort. Commented Jul 2, 2013 at 3:31
  • 5
    1. Sort an array. 2. Take first, take last, take second, take n-1, etc. Commented Jul 2, 2013 at 3:33
  • I think this is the correct approach. I'm trying to figure out what a for loop would look like for that. Any help? Commented Jul 2, 2013 at 3:36
  • Follow @defaultlocale's suggestion It will be easiest to figure out the swapping logic by dealing even-length and odd-length arrays separately. Commented Jul 2, 2013 at 3:39

5 Answers 5

5

Seems like homework. so a easy hint

step 1: sort the array in ascending order

{1, 2, 3, 6, 8, 56, 80};

step 2: allocate a new array of same size

step 3 iterate through the first array, two counter. one points at first and another points at last. now in the new array assign the data of first counter then increase the counter by one. Next assign the data of last counter and decrease it by one.

    int firstPointer = 0;
    int secondPointer = nums.length - 1;
    int[] newarray = new int[nums.length];
    int i = 0;
    for (i = 0; i < nums.length-1; i += 2) {
        newarray[i] = nums[firstPointer++];
        newarray[i+1] = nums[secondPointer--];
    }
    if(i<nums.length-1)
        newarray[i] = nums[firstPointer++];
Sign up to request clarification or add additional context in comments.

8 Comments

I think this might be what I was looking for.
this is just a pseudo codes to illustrate the problem.. write the complete one and check
@Binka modified slightly my code.. now check. Use the same.. in the array you have not assigned output[i+1]
alsto I just pointed the concept.. you need to handle the situation when the arraylength is odd
Ok thanks, its getting close. It's doing it backwards and printing zzeros.
|
1

You could sort it first, then start at the second element, swap with last element, etc.

7 Comments

I think this is the correct approach. I'm trying to figure out what a for loop would look like for that. Any help?
You'd keep track of two indices. One starting at index 1, the other at array length - 1. Swap, then increment/decrement both by two and repeat until they cross.
increment the index by 2. no need to keep track of two indices
I don't think that particular swapping will do the job. For instance, the element at length/2 needs to end up at the end of the array, but there's no way your indexing would get that to happen.
Hmm yes duh. I guess you'd have to start at the middle then. I'll have to think about it for a bit...
|
1

Try this, using lists instead of arrays.

public <T extends Comparable<? super T>> List<T> interleave(List<T> list) {
    final int size = list.size();
    final List<T> sorted =  new ArrayList<T>(list);
    Collections.sort(sorted);  // Now, it's sorted.
    final List<T> reversed = new ArrayList<T>(sorted);
    Collections.reverse(reversed); // Now, it's reverse sorted.
    final List<T> interleaved = new ArrayList<>();
    for (int i= 0; i < size/2; ++i) {
        interleaved.add(sorted.get(i));
        interleaved.add(reversed.get(i));
    }
    if (size % 2 == 1) {
        interleaved.add(sorted.get(size/2 + 1));
    }
    return interleaved;
}

Comments

0

Maybe you can(or have to) sort the array first, then put the elements in your order. Implementing Comparable or Comparator so you can define how the elements in array is sorted. The following page may be help: enter link description here

Comments

0

This code does the wacky sort:

public static void wackySort(int[] nums) {
    Arrays.sort(nums);
    int size = nums.length;
    int[] result = new int[size];
    for (int i = 0; i < size / 2; i++) {
        result[i * 2] = nums[i];
        result[i * 2 + 1] = nums[size - 1 - i];
    }
    // one extra copy is required to handle both odd and even array sizes
    result[size - 1] = nums[size / 2];
    System.arraycopy(result, 0, nums, 0, size);
}

Here's some test code:

public static void main(String[] args) {
    int[] oddSize = { 8, 6, 1, 2, 3, 80, 56 };
    wackySort(oddSize);
    System.out.println(Arrays.toString(oddSize));
    int[] evenSize = { 8, 6, 1, 2, 3, 80, 56, 5 };
    wackySort(evenSize);
    System.out.println(Arrays.toString(evenSize));
}

Output:

[1, 80, 2, 56, 3, 8, 6]
[1, 80, 2, 56, 3, 8, 5, 6]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.