All pastes #399700 Raw Edit

Something

public text v1 · immutable
·published 2007-03-18 04:45 UTC
rendered paste body
public static void insert(int[] values, int num) throws RuntimeException
	{
		//First, create a copy
		copy = new int[values.length];
		for (int i = 0; i<values.length; i++)
		{
			copy[i] = values[i];
		}
		
		int indexToInsert = 0;
		if (values[values.length-1] < num) { //If there is something to replace

			//Find index to insert the new value
			for (int i = 0; i < values.length; i++)
			{
				if (num >= values[i])
				{
					indexToInsert = i;
				break;
				}
			}
		
			//Move everything else after that one unit to the right
			//(omit last one, as required in the labtest)
			for (int i = (indexToInsert); i<(values.length-1); i++)
			{
				values[i+1] = copy[i];
			}
		
			//Now that there is space, insert the number
			values[indexToInsert] = num;
		
		}
	}