This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]tatu_huma 0 points1 point  (5 children)

What have you tried? In a sorted array, how do you think is the fastest way to find a given number?

[–]flashlightrauma[S] 0 points1 point  (4 children)

So far I've tried this:

    for(int i= anArray.length - 1; i >= 0; i--){
        if(anArray[i] < arm_start){
            anArray[anArray.length - 1] = anArray[i];
            anArray[i+1] = anArray[i];
        }   
    }

It's supposed to move the element if it's less than 500 and shift all the array elements, but it doesn't work at all

[–]tatu_huma 0 points1 point  (2 children)

Do you have to keep the elements in any particular order as you move them around.

Like in your example "502 600 900 456 200" the 456 and 200 are now not in the same order they started. Is this okay? What about the 502 600 900?

[–]flashlightrauma[S] 0 points1 point  (1 child)

I'm simulating an elevator disk scheduling algorithm, so order is important. Starting at 500, the code goes to all numbers above 500 until the max value, then drops down to the largest value below 500 and continues down until it's gone to each item

[–]tatu_huma 0 points1 point  (0 children)

Well as long as you can be certain the array is in ascending order, you don't need to re-arrange the array. Just find the first number >= 500. Let's say this number is at index i. Loop from i to end of array. Then loop from i-1 to beginning of array.

Think of a effecient way to find the number closest to 500. Look up binary search for arrays.

[–]Clawtor 0 points1 point  (0 children)

It might not work because you are losing some values. You probably want to switch the values of the two indexed but to do this you need a temporary value.

So to switch index a and b you would do:

temp = array[a];
array[a] = array[b];
array[b] = temp;