Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
So I have to write code for an insertion sort that will sort an array of random ints, the array is already set up and working okay and everything but my sort is not, heres what i have:
for(int i =1; i< numberSort.length-1;i++){
int temp = numberSort[i];
int j = i-1;
while((j >= 0) && (numberSort[j]>temp)){
numberSort[j+1] = numberSort[j];
j = j-1;
}
numberSort[j+1] = temp;
}
}
It seems to me that that should work, however it does not, it moves the numbers around from their original position but does not order them in ascending order. Thanks for any help you may be able to offer.
This code works for me:
public static void main(String[] args) {
int[] numberSort = {22,7,2, 5, 7, 1, 2, 9,33,55,12,1,0};
for (int i = 1; i < numberSort.length; i++) {
int temp = numberSort[i];
int j = i - 1;
while ((j >= 0) && (numberSort[j] > temp)) {
numberSort[j + 1] = numberSort[j];
j = j - 1;
}
numberSort[j + 1] = temp;
}
for (int i = 0; i < numberSort.length; i++) {
System.out.println(numberSort[i]);
}
}
Gives output:
0
1
1
2
2
5
7
7
9
12
22
33
55
Related
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 2 years ago.
Improve this question
Assume I have one dimensional array
int[] arr = new int[]{1,2,3,4,5,6)
and I need transfer all data from this arr in two-dimensional array
int[][]array = new int[2][1];
bellow code:
for (int i = 0; i <array.length ; i++) {
for (int j = 0; j <array[j].length ; j++) {
array[i][j] = arr[i];
}
}
return result:
[[1], [2]]
I need:
[[1,2,3], [4,5,6]]
How to achieve it?
Your 2d array is too small. To get the output you want, it needs to have a shape of 2 by 3.
int[][] array = new int[2][3];
You will also need to change the loop that builds the array. It has to keep track of the index for the original array separately from the indices of the 2d array.
int indexForArr = 0;
for (int i = 0; i <array.length ; i++) {
for (int j = 0; j <array[j].length ; j++) {
array[i][j] = arr[indexForArr];
indexForArr = indexForArr + 1;
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
if Take an array of size 10 and take user inputs.how can i Traverse the array and each time you encounter '6' or '7', replace them with 60 and 70 respectively in java??
Maybe you can try this,
int[] input = {1,2,3,4,5,6,7,8,9};
for(int i = 0; i < input.length; i++)
if(input[i]==6 || input[i]==7)
input[i]*=10;
//Printing
for(int i : input)
System.out.println(i);
Here is a simplistic approach. Loop through the array and check each positions value. If it's a 6 or 7 replace as expected.
public static void main(String[] args){
int[] array = {1,2,3,4,5,6,7,8,9};
for(int i = 0; i < array.length; i++){
if(array[i] == 6){
array[i] = 60;
}
else if(array[i] == 7){
array[i] = 70;
}
}
for(int i : array){
System.out.println(i);
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
If I have an array of zeros [0,0,0] how do I insert a given value to the first index that is valued 0.
So first off the value 3 will go into index 0 giving me [3,0,0]
Then the second value 8 will go into index 1 giving me [3,8,0].
How do I go about this?
You can edit the values in an array like this
int[] values = new int[]{0, 0, 0};
values[0] = 3;
values[1] = 8;
or if you really want to target the first index that is 0
for(int i = 0 ; i < values.length ; i++) {
if(values[i]==0){ // do stuff; break; }
}
int[] values = new int[]{0, 0, 0};
replaceFirstZeroValue( 3, values);
replaceFirstZeroValue( 8, values);
public void replaceFirstZeroValue(int value, int[] array){
for(int i = 0; i < array.length; i++){
if(array[i] == 0){
array[i] = value;
break;
}
}
}
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 7 years ago.
Improve this question
I'm having trouble figuring out why my j won't print out with the current boolean statement. Its only if I put != will it then print 100,000-1 indices.
public static void main(String[] args) {
//array size and name
double[] largearr;
largearr = new double[100000];
double index = 0.00149;
int j = 0;
//population of array and the printout of the elements
for(int i = 0; i < 100000; i++) {
Arrays.fill(largearr, index);
index += 0.00149;
// System.out.println(largearr[i] + " ");
}
for(j = 0; j < largearr.length; j++) {
if(largearr[j] == 58.00122999995376) {
System.out.println("j");
}
}
}
Because you don't have that value in any position of your largerarr. The first for loop in your code fills all the elements of largearr array with the newly incremented index value, and it does it 100000 times: at the end you'll have largearr filled with the last iteration's value of index, that surely isn't 58.00122999995376. So the second for loop doesn't enter the if check for any value and the program quits without printing anything.
If you could explain what are you trying to achieve maybe we could help you better.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Given a number N and an array ? How to distribute N among the array as the following example:
N = 5 and array of size 4;
[5,0,0,0]
[0,5,0,0]
[0,0,5,0]
[0,0,0,5]
[4,1,0,0]
[4,0,1,0]
[4,0,0,1]
[1,4,0,0]
[0,4,1,0]
[0,4,0,1]
[1,0,4,0]
[0,1,4,0]
[0,0,4,1]
.
..
..
[2,1,2,0]
..
..
...
and so on
The important thing is that the sum of whole array is N!
I want to implement a hill climbing algorithm so I want to generate all successors, then hill climbing can choose from the list.
Use recursion or don't if you hate it or have long arrays. You want to fill in arr. You have filled beginning (0..index-1) and you want to fill the end of array with the rest N.
Call (arr has length 5): fill(arr, 5, 0);
private static void fill(int[] arr, int N, int index) {
if (arr == null) {
return;
}
if (N == 0) {
for (; index < arr.length; index++) {
arr[index] = 0;
}
} else if (index == arr.length - 1) {
arr[index] = N;
} else {
for (int j = N; j >= 0; j--) {
arr[index] = j;
fill(arr, N - j, index + 1);
}
}
if (index >= arr.length - 1) {
// use your array
System.out.println(Arrays.toString(arr));
}
}