2

I've been trying to work this out:

Say I have an array:

int[] n = {0, 0, -1, 1, 0, 1, 1, -1, 1};

I need to be able to sort through the array and if there is a zero with a non zero preceding it, then they should be swapped.

For example: 0, 0, -1, 1, 0, 1, 1, -1, 1

will become: 0, 0, -1, 0, 1, 1, 1, -1, 1

I have been trying to do it using a for loop and if statements with no luck. Any tips?

5
  • 5
    "Any tips?": Yes -- please show us what you've tried. How else are we to guess what you may be doing wrong? How else will we know what misconceptions you may have that need to be cleared? Commented Sep 29, 2012 at 13:43
  • 4
    Great. Here come the spoon feeders. Why not first wait til we see what the OP has tried first? Commented Sep 29, 2012 at 13:47
  • They weren't exactly spoon feeding. The problem i had was not a replica of the question i asked, i just needed help on something similar, that one if statement and the the temp variable holder was a big help in me figuring out my actual problem! I do see why you have that particular attitude though hover. Thanks to everyone that helped, now i just need to figure out why it's getting an out of bounds exception. Commented Sep 29, 2012 at 14:34
  • Is it too much though to ask you to show your work? I would hope not. Commented Sep 29, 2012 at 15:05
  • 2
    Not at all, i probably should of done so. I will in the future. Commented Sep 29, 2012 at 15:43

5 Answers 5

3

Try this:

for (int i = 1 ; i < n.length ; i++)
    if (n[i] == 0 && n[i - 1] != 0) {
        int tmp = n[i - 1];
        n[i - 1] = n[i];
        n[i] = tmp;
    }

You were right in thinking that you would need a for loop with an if statement in its body. All we're doing here is looping through the array starting at element 1. We then check if the element we're currently on is 0 and the previous element is not 0: i.e. if (n[i] == 0 && n[i - 1] != 0). If this condition is true, we swap these two elements.

Sign up to request clarification or add additional context in comments.

Comments

2
for(int i=0; i < length; i++)
{
    if(i > 0 && arr[i] == 0 && arr[i-1] != 0)
    {
        int temp = arr[i-1];
        arr[i-1] = arr[i];
        arr[i] = temp;
    }
}

Should work.

2 Comments

Why not start your for loop at i=1, since your if clause has i > 0 in the check?
True, I could do that. Yet it doesn't render the algorithm incorrect.
0

Without using bit twiddling, you'll need a temporary variable to swap two objects. Example:

int[] n = {...};
int temp = n[3]; // swaps n[3] and n[4]
n[3] = n[4];
n[4] = temp;

You could stick something like this inside your loop to accomplish what you described.

4 Comments

To the downvoter: I understand why you did this. However, I did not post a full solution, I just posted a simple example of how two variables are often swapped, with a suggestion of how to use a similar technique.
I don't see the issue with even posting the full solution as long as it coupled with some explanation. Read this (specifically the comments).
I quote Tim Post: "If you don't want a fully vetted, complete and testable answer, Stack Overflow is not the place to ask."
Yes I know - I was just speaking in general because you mentioned "I did not post a full solution" indicating that doing so would be reason for a downvote.
0
public class Swaparray 
{
     public static void main(String []args)
     {
        int [] arr={0,1,2,3,4,5,6,7};
         Swaparray.displayArray(arr);
         for(int i=1;i<arr.length;i++)
         {
         while(arr[i]%2!=0)
         {
        int tmp = arr[i - 1];
        arr[i - 1] = arr[i];
        arr[i] = tmp;
        }
        }
        Swaparray.displayArray(arr);
                     }
   static void displayArray(int[]arr)
   {
       System.out.print("Array elements are: ");
       for(int i=0;i<arr.length;i++) {
           System.out.print(arr[i]+" "); }
       System.out.println();
       }
}

o/p Array elements are: 0 1 2 3 4 5 6 7 Array elements are: 1 0 3 2 5 4 7 6

swapping two consecutive elements in array

Comments

0
 int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 int length = arr.length;
 int temp;
 for (int i = 0; i < length/2; i++)
   {
    temp = arr[i];
    arr[i] = arr[length - i - 1];
    arr[length - i - 1] = temp;
   }

 for (int element:arr)
  {
    System.out.println (element);
  }

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.