Hi there guys,

I seem to be having a problem with my java code.

Scenario;

I have an array, this is generated according to the user; entering 200; will give him 200 numbers. These numbers are supposed to be semi sorted. To achieve this i created a random array, then sorted it.


But now the problem is; its supposed to be semi sorted; for example;
My code generates me an array of size 8. [1,2,3,4,5,6,7,8]... NO PROBLEM HERE!


PROBLEM HERE --> What the code should ideally do? -- User puts in the amount of numbers he wants to move around. E.g. 2, this means he wants 2 random numbers to be moved randomly in the array.

So, code should pick two random number from that array, and place it randomly anywhere. for example, it picks [..,7,], and it places it randomly in front of [..3], so array should now be; [1,2,7,3,4,5,6,8]... then it picks [,5] and randomly insert it in first index [5,1,2,7,3,4,5,6,8]... THIS IS SEMI SORTED..

PROBLEM - I do not understand how to move it in front of random element. All my code does is swaps the elements, so we get [1,2,7,4,5,6,3,8] after first iteration. I do not want to swap. I need help moving random numbers picked up in array moving randomly.

Problem lies in the code below, this is wrong implementation;

MY code implementation (part of it), can someone correct me

Arrays.sort(array); // Sort the array. 
System.out.println("Original sorted:" + Arrays.toString(array));

 for(j=0; j<numElementsChange; j++) 
{
    	int temp = random.nextInt(array.length);
	int swap = random.nextInt(array.length);
		    	
  	temp = array[swap];
    	array[swap] = array[j];
    	array[j] = temp; 

}
System.out.println("Pre sorted:" + Arrays.toString(array));

Edited 4 Years Ago by usmansajid: n/a

I do not understand how to move it in front of random element

Before you write code, you need to come up with an algorithm or design describing what the code will do.
What do you have for a design? If you were to do it manually with a row of playing cards, how would you do it?

Perhaps you could copy the value you're going to move, then move the items that will be behind it over one index, then write the value back into the array?

The problem is you are just swapping the numbers. if you want to insert 7 in front of 3 you need to need to move the numbers in 3 - 6 up one in the array i.e.
6->7
5->6
4->5
3->4

and then put 7 in position 3.

note that you have to move the numbers in decending order for this work.

ChrisPadgham sort of has the right idea.
What you actually want to do is (in the first example),
swap the 7 with the preceeding element in the array (in a loop),
until you get in front of the 3.
You will also have to account for going in the other direction as well.

yes guys, trying my best to figure this out. But it aint getting nowhere...

Can anyone provide a little code to test?

Try writing down the steps to insert an element into an array.
Each step should be short and simple.
If you have the array: {1,2,3,...} and want to insert a 4 after the 2 what are the steps?

hxf642 - Since you know you are going to place the 7 before 3 there is no need to ripple it down as you suggest. It will work but it is unnecessary processing. It is faster and less code just to move 3-6 up one spot and insert 7 where 3 was.

This article has been dead for over six months. Start a new discussion instead.