Deletion algorithm for array - java

Deletion(int array[ ], int delete, int position)
So I have this method, see above, the first parameter is the name of the array, the second is the value I want deleted, and the third is the index of which it can be found. For example:
Deletion(list,50,4);
In this case, the array I chose is called list, the value I want deleted is 50, and the index of which the value is located is 4.
If we were to use Deletion(list,50,4), this method is supposed to replace the 50 at the index of 4 with the element at the index of 5 and the element at index of 5 is replaced with the element at index of 6 and so on.
If this is the array list: 90 76 55 48 50 69 66 52 87 56 89 91 86 73 50 68 52 75 54 45
The output should be: 90 76 55 48 69 66 52 87 56 89 91 86 73 50 68 52 75 54 45 45
However,I can only get it to replace the 50 but not any of the other numbers.
This is the output I get: 90 76 55 48 69 69 66 52 87 56 89 91 86 73 50 68 52 75 54 45
The 50 is replaced with the 69 but everything else stays the same, I need the other elements to "shift" to the left.
Here is my code so far for the Deletion method:
for (int i=array.length-1; i<position; i ++)
array[i] = array[ i+1];
array[position] = array[position+1]
The length of the list is 20, subtract 1, so i = 19. I know 19 is not less than 4 so it skips the for loop, but if I made it i > position, I get this error:
java.lang.ArrayIndexOutOfBoundsException: 20
So this is where I'm stuck. Please help!

You need to redefine the value of i so that it is initialized as the the int value of position (so that your deletion/replacement process can start there).
for (int i = position; i < array.length - 2; i++)
{
array[i] = array[i+1];
}
Your i.max_value should be array.length-2 so that i will incrementally increase until the end of the array. It shouldn't be -1 since you are essentially deleting the last index of your array (as the value has nothing)

You'd better be using System.arraycopy instead on implementing your own algorithm for moving array elements. Like this:
System.arraycopy(array, position+1, array, position, array.length-position);
Also see this question: Delete item from array and shrink array

Related

Array Shuffle (With same Start and end Value)

I'm currently working on a Travelling salesman problem and i'm having trouble generating random paths with the same start and end value
This is my current path(path of cities visited)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 38 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 14 39 40 41 42 43 44 45 46 47 48 49 50 51 0
See how the last node arrives back at 0(TSP logic)
Now i'm trying to shuffle this array randomly because random start points generate better results then others However I know how to shuffle the array using Collections but that would shuffle every value randomly
Basically i'm trying to create a method to shuffle an array randomly WITH THE EXCEPTION THE START and END VALUES must be the same and every number must be distinct 0-51
This is my current code, it basically shuffles the array and sets the last index to the first index
static void shuffleArray(int[] ar)
{
// If running on Java 6 or older, use `new Random()` on RHS here
Random rnd = new Random();
for (int i = ar.length - 1; i > 0; i--)
{
int index = rnd.nextInt(i + 1);
// Simple swap
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
ar[ar.length-1]=ar[0];
}
however this is giving me duplicate values for some reason
Could you not remove the start and end position, shuffle the array, and then add the start and end position again?

Remember Old Random Numbers? Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In order to prevent duplicates in my random number generator, I want to make an if statement that tests if my random number (x) is the same as any other previous values of x. If it is, then it returns and restarts the loop.
How do I have my code remember all of the previous values of x?
Pseudocode:
if (x == previous value of x) {
return;
}
Explanation: For example, you can make a list that contains 8 numbers and after that shuffle the list in order to be randomized what you want.
Code:
Integer[] array = new Integer[100];
for (int i = 0; i < 100; i++) {
array[i] = i+1;
}
List<Integer> list = Arrays.asList(array);
Collections.shuffle(list);
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
}
Output:
88 25 24 15 92 87 14 100 68 98 91 32 28 22 74 37 10 90 44 77 94 52 34
86 72 99 46 71 36 26 69 55 82 79 19 78 60 49 70 85 33 66 45 57 61 9 51
76 3 42 16 29 84 93 58 2 53 97 6 17 13 43 12 56 35 96 83 40 38 59 50 5
41 65 11 23 73 4 47 89 1 81 21 80 63 48 20 27 64 54 18 75 30 62 39 31
7 8 67 9
Use a list to record the previous values of x. List are better than arrays for this purpose, because you don't need to resize them if you want to generate more random numbers.
List<int> randomNumberList = new ArrayList<int>();
randomNumberList.add(randomNumberThatYouGenerated);
then just use an if:
if (randomNumberList.contains(randomNumberThatYouGenerated)) {
//code
}
Use a array to record the previous value of x..... and check if the currently value inside that array..

Load a file into java and make an array from the data

I'm running into a problem whenever I try to take the data from a file and make an array and an int out of that data.
For this specific problem, we're given 17 data points in this style:
13 24 35 46 57 68 79 80 32 43 54 65 76 87 98 27
438
What I'm trying to do is make an array from the first row, and make the second row's single variable into an int.
How would I do this in java?
(for the problem, the second variable is what we check our results against, so I do want it as an int or atleast in it's own array)
You can use this :
String line = "13 24 35 46 57 68 79 80 32 43 54 65 76 87 98 27";
String[] array = line.split(" ");
If you want to use this numbers for counting, you need to change them to integer.
int[] intarray = new int[array.length];
for (int i = 0; i < array.length; i++) {
intarray[i] = Integer.valueOf(array[i]);
}

Problem with Project Euler problem 18 [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
I'm currently trying to solve problem 18 of project Euler. The goal is:
By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.
3
7 4
2 4 6
8 5 9 3
That is, 3 + 7 + 4 + 9 = 23.
Find the maximum total from top to bottom of the triangle below:
75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
I tried to solve it with the following algorithm:
public static void main(String[] args) throws NumberFormatException, IOException {
int[][] values = readFile();
int depth = values.length - 2;
while ( depth >= 0) {
for (int j = 0; j < depth; j++) {
values[depth][j] += Math.max( values[depth+1][j], values[depth+1][j+1]);
}
depth += -1;
}
values[0][0] += Math.max(values[1][0], values[1][1]);
System.out.println("The maximum path sum is: " + values[0][0]);
}
The array values contains all the numbers from the triangle where values[0][0] is the element in the top row and values[n][n] is the last element in the last row.
The algorithm uses the approach that if for example I start in the last row and have the choice between 04 + 63 and 62 + 63, the sum of the field in which 63 was will be set to 125 because this is the highest amount. This algorithm works for the small triangle, but for the big triangle it seems to fail. I'm not sure why and would appreciate every hint.
I believe that the line:
for (int j = 0; j < depth; j++) {
should be
for (int j = 0; j <= depth; j++) {
because right now you aren't visiting the last element on each row. Of course, then you don't need the line
values[0][0] += Math.max(values[1][0], values[1][1]);
because it is already done in the loop.
I don't know the correct algorithm, but there's an easy proof of #Johns comment on the question, that the best local choice doesn't necessarily lead to the best global solution.
Consider this (extreme) example:
1
1 0
1 0 1000
1 0 0 0
1 0 0 0 0
Given your algorithm, you'd obviously go down the very left of the path and never read the 1000 that must be on the best path.
This may not be the best solution, but what if for each iteration you kept track of the sum to that point. Then when you go to the last row the max value would be your answer.

Java: Simple Combination of a set of element of k order

I have a set of numbers {'1','13','25','32','49',...}, i want to calculate all possible combinations of this numbers of order k.
Esample1:
set = {'1','5','23','41,'54','63'};
k = 4;
Output1:
1 5 23 41
1 5 23 54
1 5 23 63
1 5 41 54
1 5 41 63
1 5 54 63
1 23 41 54
1 23 41 63
1 23 54 63
1 41 54 63
5 23 41 54
5 23 41 63
5 23 54 63
5 41 54 63
23 41 54 63
Example2:
set = {'a','v','f','z'};
k=3;
Output2:
a v f
a v z
a f z
v f z
in Java plaese.
Thank you!
You should be able to find an appropriate algorithm in D.Knuth's The Art of Computer Programming, Volume 4, fascicle 3 - Generating All Combinations, which can be downloaded from his website.

Categories