permutation algorithm in java
I was trying to permutate using java and i noticed my code works for strings but the same algorithm does not work correctly on Arrays
here's the code for strings
public static void Recurse(String s,int i)
{
for(int j=i;j<(s.length());j++)
{
s=Swap(s,i,j);
if(i<(s.length()-1))
{
Recurse(s,i+1);
}
if(i==(s.length()-1))
{
Display(s);
}
}
}
public static String Swap(String newString,int i,int j)
{
char newChar=newString.toCharArray();
char temp=newChar[i];
newChar[i]=newChar[j];
newChar[j]=temp;
return String.valueOf(newChar);
}
public static void Display(String s)
{
System.out.println(s);
}
public static void main(String args)
{
String i=new String("123");
Recurse(i,0);
}
output:
123
132
213
231
312
321
works fine.
then here is the code for arrays
public static void Recurse(int newArray,int i)
{
for(int j=i;j<newArray.length;j++)
{
Swap(newArray,i,j);
if(i<(newArray.length-1))
{
Recurse(newArray,i+1);
}
if(i==(newArray.length-1))
{
Display(newArray);
}
}
}
public static void Display(int Array)
{
for(int i: Array)
{
System.out.print(i);
}
System.out.println();
}
public static void main(String args)
{
Recurse(new int {1,2,3},0);
}
output
123
132
312
321
123
132
Does not work correctly,So i was wondering what went wrong here.
java arrays string permutation
|
show 1 more comment
I was trying to permutate using java and i noticed my code works for strings but the same algorithm does not work correctly on Arrays
here's the code for strings
public static void Recurse(String s,int i)
{
for(int j=i;j<(s.length());j++)
{
s=Swap(s,i,j);
if(i<(s.length()-1))
{
Recurse(s,i+1);
}
if(i==(s.length()-1))
{
Display(s);
}
}
}
public static String Swap(String newString,int i,int j)
{
char newChar=newString.toCharArray();
char temp=newChar[i];
newChar[i]=newChar[j];
newChar[j]=temp;
return String.valueOf(newChar);
}
public static void Display(String s)
{
System.out.println(s);
}
public static void main(String args)
{
String i=new String("123");
Recurse(i,0);
}
output:
123
132
213
231
312
321
works fine.
then here is the code for arrays
public static void Recurse(int newArray,int i)
{
for(int j=i;j<newArray.length;j++)
{
Swap(newArray,i,j);
if(i<(newArray.length-1))
{
Recurse(newArray,i+1);
}
if(i==(newArray.length-1))
{
Display(newArray);
}
}
}
public static void Display(int Array)
{
for(int i: Array)
{
System.out.print(i);
}
System.out.println();
}
public static void main(String args)
{
Recurse(new int {1,2,3},0);
}
output
123
132
312
321
123
132
Does not work correctly,So i was wondering what went wrong here.
java arrays string permutation
1
Could you post the code for theSwap
method?
– GBlodgett
Nov 20 '18 at 17:30
i have reedited the code
– user9169047
Nov 20 '18 at 17:33
Are you using the sameSwap
method for yourArray
version?
– GBlodgett
Nov 20 '18 at 17:36
no,the array was swapped using temp variables,but strings are immutable so i had to convert to charArray then return a new String object to swap the string.
– user9169047
Nov 20 '18 at 17:41
You should avoid the String -> char -> String conversion. Just operate on char and to the from/to String conversion and the beginning/end.
– Robert
Nov 20 '18 at 17:52
|
show 1 more comment
I was trying to permutate using java and i noticed my code works for strings but the same algorithm does not work correctly on Arrays
here's the code for strings
public static void Recurse(String s,int i)
{
for(int j=i;j<(s.length());j++)
{
s=Swap(s,i,j);
if(i<(s.length()-1))
{
Recurse(s,i+1);
}
if(i==(s.length()-1))
{
Display(s);
}
}
}
public static String Swap(String newString,int i,int j)
{
char newChar=newString.toCharArray();
char temp=newChar[i];
newChar[i]=newChar[j];
newChar[j]=temp;
return String.valueOf(newChar);
}
public static void Display(String s)
{
System.out.println(s);
}
public static void main(String args)
{
String i=new String("123");
Recurse(i,0);
}
output:
123
132
213
231
312
321
works fine.
then here is the code for arrays
public static void Recurse(int newArray,int i)
{
for(int j=i;j<newArray.length;j++)
{
Swap(newArray,i,j);
if(i<(newArray.length-1))
{
Recurse(newArray,i+1);
}
if(i==(newArray.length-1))
{
Display(newArray);
}
}
}
public static void Display(int Array)
{
for(int i: Array)
{
System.out.print(i);
}
System.out.println();
}
public static void main(String args)
{
Recurse(new int {1,2,3},0);
}
output
123
132
312
321
123
132
Does not work correctly,So i was wondering what went wrong here.
java arrays string permutation
I was trying to permutate using java and i noticed my code works for strings but the same algorithm does not work correctly on Arrays
here's the code for strings
public static void Recurse(String s,int i)
{
for(int j=i;j<(s.length());j++)
{
s=Swap(s,i,j);
if(i<(s.length()-1))
{
Recurse(s,i+1);
}
if(i==(s.length()-1))
{
Display(s);
}
}
}
public static String Swap(String newString,int i,int j)
{
char newChar=newString.toCharArray();
char temp=newChar[i];
newChar[i]=newChar[j];
newChar[j]=temp;
return String.valueOf(newChar);
}
public static void Display(String s)
{
System.out.println(s);
}
public static void main(String args)
{
String i=new String("123");
Recurse(i,0);
}
output:
123
132
213
231
312
321
works fine.
then here is the code for arrays
public static void Recurse(int newArray,int i)
{
for(int j=i;j<newArray.length;j++)
{
Swap(newArray,i,j);
if(i<(newArray.length-1))
{
Recurse(newArray,i+1);
}
if(i==(newArray.length-1))
{
Display(newArray);
}
}
}
public static void Display(int Array)
{
for(int i: Array)
{
System.out.print(i);
}
System.out.println();
}
public static void main(String args)
{
Recurse(new int {1,2,3},0);
}
output
123
132
312
321
123
132
Does not work correctly,So i was wondering what went wrong here.
java arrays string permutation
java arrays string permutation
edited Nov 20 '18 at 17:37
user9169047
asked Nov 20 '18 at 17:18
user9169047user9169047
183
183
1
Could you post the code for theSwap
method?
– GBlodgett
Nov 20 '18 at 17:30
i have reedited the code
– user9169047
Nov 20 '18 at 17:33
Are you using the sameSwap
method for yourArray
version?
– GBlodgett
Nov 20 '18 at 17:36
no,the array was swapped using temp variables,but strings are immutable so i had to convert to charArray then return a new String object to swap the string.
– user9169047
Nov 20 '18 at 17:41
You should avoid the String -> char -> String conversion. Just operate on char and to the from/to String conversion and the beginning/end.
– Robert
Nov 20 '18 at 17:52
|
show 1 more comment
1
Could you post the code for theSwap
method?
– GBlodgett
Nov 20 '18 at 17:30
i have reedited the code
– user9169047
Nov 20 '18 at 17:33
Are you using the sameSwap
method for yourArray
version?
– GBlodgett
Nov 20 '18 at 17:36
no,the array was swapped using temp variables,but strings are immutable so i had to convert to charArray then return a new String object to swap the string.
– user9169047
Nov 20 '18 at 17:41
You should avoid the String -> char -> String conversion. Just operate on char and to the from/to String conversion and the beginning/end.
– Robert
Nov 20 '18 at 17:52
1
1
Could you post the code for the
Swap
method?– GBlodgett
Nov 20 '18 at 17:30
Could you post the code for the
Swap
method?– GBlodgett
Nov 20 '18 at 17:30
i have reedited the code
– user9169047
Nov 20 '18 at 17:33
i have reedited the code
– user9169047
Nov 20 '18 at 17:33
Are you using the same
Swap
method for your Array
version?– GBlodgett
Nov 20 '18 at 17:36
Are you using the same
Swap
method for your Array
version?– GBlodgett
Nov 20 '18 at 17:36
no,the array was swapped using temp variables,but strings are immutable so i had to convert to charArray then return a new String object to swap the string.
– user9169047
Nov 20 '18 at 17:41
no,the array was swapped using temp variables,but strings are immutable so i had to convert to charArray then return a new String object to swap the string.
– user9169047
Nov 20 '18 at 17:41
You should avoid the String -> char -> String conversion. Just operate on char and to the from/to String conversion and the beginning/end.
– Robert
Nov 20 '18 at 17:52
You should avoid the String -> char -> String conversion. Just operate on char and to the from/to String conversion and the beginning/end.
– Robert
Nov 20 '18 at 17:52
|
show 1 more comment
2 Answers
2
active
oldest
votes
The output seems to be correct with below code:
public static void recurse(int newArray, int i) {
for (int j = i; j < newArray.length; j++) {
newArray = swap(newArray, i, j);
if (i < (newArray.length - 1)) {
recurse(newArray, i + 1);
}
if (i == (newArray.length - 1)) {
display(newArray);
}
}
}
private static int swap(int a, int i, int j) {
int newArray = a.clone();
int temp = newArray[i];
newArray[i] = newArray[j];
newArray[j] = temp;
return newArray;
}
public static void display(int Array) {
for (int i : Array) {
System.out.print(i);
}
System.out.println();
}
public static void main(String args) {
recurse(new int{1, 2, 3}, 0);
}
the output is :
123
132
213
231
312
321
I just made few changes in swap
methods code, I made it similar to the one which you used in Strings swap
method.
Thanks ,this works ,but what exactly is the difference between this swap function and the other one that is making them produce different output
– user9169047
Nov 20 '18 at 18:27
@user9169047 Array is a referenceType, on passing it to the method the actual values in it will be changed. and the logic to permute numbers needs the array to be in its earlier state.
– eatSleepCode
Nov 20 '18 at 18:31
add a comment |
Strings are immutable, so when Recurse(String s,int i)
calls Recurse(s,i+1);
, the value of s
remains unchanged.
Arrays are mutable, so when Recurse(int newArray,int i)
calls Recurse(newArray,i+1);
, the value of newArray
is changed.
That is why they behave differently.
Note: When I say "the value of" I'm referring to the value of the object, not the value of the reference variable itself.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398231%2fpermutation-algorithm-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The output seems to be correct with below code:
public static void recurse(int newArray, int i) {
for (int j = i; j < newArray.length; j++) {
newArray = swap(newArray, i, j);
if (i < (newArray.length - 1)) {
recurse(newArray, i + 1);
}
if (i == (newArray.length - 1)) {
display(newArray);
}
}
}
private static int swap(int a, int i, int j) {
int newArray = a.clone();
int temp = newArray[i];
newArray[i] = newArray[j];
newArray[j] = temp;
return newArray;
}
public static void display(int Array) {
for (int i : Array) {
System.out.print(i);
}
System.out.println();
}
public static void main(String args) {
recurse(new int{1, 2, 3}, 0);
}
the output is :
123
132
213
231
312
321
I just made few changes in swap
methods code, I made it similar to the one which you used in Strings swap
method.
Thanks ,this works ,but what exactly is the difference between this swap function and the other one that is making them produce different output
– user9169047
Nov 20 '18 at 18:27
@user9169047 Array is a referenceType, on passing it to the method the actual values in it will be changed. and the logic to permute numbers needs the array to be in its earlier state.
– eatSleepCode
Nov 20 '18 at 18:31
add a comment |
The output seems to be correct with below code:
public static void recurse(int newArray, int i) {
for (int j = i; j < newArray.length; j++) {
newArray = swap(newArray, i, j);
if (i < (newArray.length - 1)) {
recurse(newArray, i + 1);
}
if (i == (newArray.length - 1)) {
display(newArray);
}
}
}
private static int swap(int a, int i, int j) {
int newArray = a.clone();
int temp = newArray[i];
newArray[i] = newArray[j];
newArray[j] = temp;
return newArray;
}
public static void display(int Array) {
for (int i : Array) {
System.out.print(i);
}
System.out.println();
}
public static void main(String args) {
recurse(new int{1, 2, 3}, 0);
}
the output is :
123
132
213
231
312
321
I just made few changes in swap
methods code, I made it similar to the one which you used in Strings swap
method.
Thanks ,this works ,but what exactly is the difference between this swap function and the other one that is making them produce different output
– user9169047
Nov 20 '18 at 18:27
@user9169047 Array is a referenceType, on passing it to the method the actual values in it will be changed. and the logic to permute numbers needs the array to be in its earlier state.
– eatSleepCode
Nov 20 '18 at 18:31
add a comment |
The output seems to be correct with below code:
public static void recurse(int newArray, int i) {
for (int j = i; j < newArray.length; j++) {
newArray = swap(newArray, i, j);
if (i < (newArray.length - 1)) {
recurse(newArray, i + 1);
}
if (i == (newArray.length - 1)) {
display(newArray);
}
}
}
private static int swap(int a, int i, int j) {
int newArray = a.clone();
int temp = newArray[i];
newArray[i] = newArray[j];
newArray[j] = temp;
return newArray;
}
public static void display(int Array) {
for (int i : Array) {
System.out.print(i);
}
System.out.println();
}
public static void main(String args) {
recurse(new int{1, 2, 3}, 0);
}
the output is :
123
132
213
231
312
321
I just made few changes in swap
methods code, I made it similar to the one which you used in Strings swap
method.
The output seems to be correct with below code:
public static void recurse(int newArray, int i) {
for (int j = i; j < newArray.length; j++) {
newArray = swap(newArray, i, j);
if (i < (newArray.length - 1)) {
recurse(newArray, i + 1);
}
if (i == (newArray.length - 1)) {
display(newArray);
}
}
}
private static int swap(int a, int i, int j) {
int newArray = a.clone();
int temp = newArray[i];
newArray[i] = newArray[j];
newArray[j] = temp;
return newArray;
}
public static void display(int Array) {
for (int i : Array) {
System.out.print(i);
}
System.out.println();
}
public static void main(String args) {
recurse(new int{1, 2, 3}, 0);
}
the output is :
123
132
213
231
312
321
I just made few changes in swap
methods code, I made it similar to the one which you used in Strings swap
method.
answered Nov 20 '18 at 17:43


eatSleepCodeeatSleepCode
2,26132463
2,26132463
Thanks ,this works ,but what exactly is the difference between this swap function and the other one that is making them produce different output
– user9169047
Nov 20 '18 at 18:27
@user9169047 Array is a referenceType, on passing it to the method the actual values in it will be changed. and the logic to permute numbers needs the array to be in its earlier state.
– eatSleepCode
Nov 20 '18 at 18:31
add a comment |
Thanks ,this works ,but what exactly is the difference between this swap function and the other one that is making them produce different output
– user9169047
Nov 20 '18 at 18:27
@user9169047 Array is a referenceType, on passing it to the method the actual values in it will be changed. and the logic to permute numbers needs the array to be in its earlier state.
– eatSleepCode
Nov 20 '18 at 18:31
Thanks ,this works ,but what exactly is the difference between this swap function and the other one that is making them produce different output
– user9169047
Nov 20 '18 at 18:27
Thanks ,this works ,but what exactly is the difference between this swap function and the other one that is making them produce different output
– user9169047
Nov 20 '18 at 18:27
@user9169047 Array is a referenceType, on passing it to the method the actual values in it will be changed. and the logic to permute numbers needs the array to be in its earlier state.
– eatSleepCode
Nov 20 '18 at 18:31
@user9169047 Array is a referenceType, on passing it to the method the actual values in it will be changed. and the logic to permute numbers needs the array to be in its earlier state.
– eatSleepCode
Nov 20 '18 at 18:31
add a comment |
Strings are immutable, so when Recurse(String s,int i)
calls Recurse(s,i+1);
, the value of s
remains unchanged.
Arrays are mutable, so when Recurse(int newArray,int i)
calls Recurse(newArray,i+1);
, the value of newArray
is changed.
That is why they behave differently.
Note: When I say "the value of" I'm referring to the value of the object, not the value of the reference variable itself.
add a comment |
Strings are immutable, so when Recurse(String s,int i)
calls Recurse(s,i+1);
, the value of s
remains unchanged.
Arrays are mutable, so when Recurse(int newArray,int i)
calls Recurse(newArray,i+1);
, the value of newArray
is changed.
That is why they behave differently.
Note: When I say "the value of" I'm referring to the value of the object, not the value of the reference variable itself.
add a comment |
Strings are immutable, so when Recurse(String s,int i)
calls Recurse(s,i+1);
, the value of s
remains unchanged.
Arrays are mutable, so when Recurse(int newArray,int i)
calls Recurse(newArray,i+1);
, the value of newArray
is changed.
That is why they behave differently.
Note: When I say "the value of" I'm referring to the value of the object, not the value of the reference variable itself.
Strings are immutable, so when Recurse(String s,int i)
calls Recurse(s,i+1);
, the value of s
remains unchanged.
Arrays are mutable, so when Recurse(int newArray,int i)
calls Recurse(newArray,i+1);
, the value of newArray
is changed.
That is why they behave differently.
Note: When I say "the value of" I'm referring to the value of the object, not the value of the reference variable itself.
answered Nov 20 '18 at 17:54


AndreasAndreas
76.3k463123
76.3k463123
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398231%2fpermutation-algorithm-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Could you post the code for the
Swap
method?– GBlodgett
Nov 20 '18 at 17:30
i have reedited the code
– user9169047
Nov 20 '18 at 17:33
Are you using the same
Swap
method for yourArray
version?– GBlodgett
Nov 20 '18 at 17:36
no,the array was swapped using temp variables,but strings are immutable so i had to convert to charArray then return a new String object to swap the string.
– user9169047
Nov 20 '18 at 17:41
You should avoid the String -> char -> String conversion. Just operate on char and to the from/to String conversion and the beginning/end.
– Robert
Nov 20 '18 at 17:52