SlideShare ist ein Scribd-Unternehmen logo
1 von 5
Downloaden Sie, um offline zu lesen
Code:
import java.util.Random;
import java.util.Scanner;
class Test{
static void permute(int[] a, int k){
if (k == a.length){
for (int i = 0; i < a.length; i++){
System.out.print(a[i] + " ");
}
System.out.println();
}else{
for (int i = k; i < a.length; i++){
int temp = a[k];
a[k] = a[i];
a[i] = temp;
permute(a, k + 1);
temp = a[k];
a[k] = a[i];
a[i] = temp;
}
}
}
public static void main(String args[])
{
Random random = new Random();
Scanner sc = new Scanner(System.in);
System.out.print("Enter the length of list: ");
int N = sc.nextInt();
int[] sequence = new int[N];
for (int i = 1; i < N+1; i++)
sequence[i-1] = i;
System.out.println("The original sequence is: ");
for (int i = 0; i < N; i++)
System.out.print(sequence[i] + " ");
System.out.println(" The permuted sequences are as follows: ");
permute(sequence, 0);
sc.close();
}
}
Output:
Enter the length of list: 4
The original sequence is:
1 2 3 4
The permuted sequences are:
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 3 2
1 4 2 3
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 3 1
2 4 1 3
3 2 1 4
3 2 4 1
3 1 2 4
3 1 4 2
3 4 1 2
3 4 2 1
4 2 3 1
4 2 1 3
4 3 2 1
4 3 1 2
4 1 3 2
4 1 2 3
Solution
Code:
import java.util.Random;
import java.util.Scanner;
class Test{
static void permute(int[] a, int k){
if (k == a.length){
for (int i = 0; i < a.length; i++){
System.out.print(a[i] + " ");
}
System.out.println();
}else{
for (int i = k; i < a.length; i++){
int temp = a[k];
a[k] = a[i];
a[i] = temp;
permute(a, k + 1);
temp = a[k];
a[k] = a[i];
a[i] = temp;
}
}
}
public static void main(String args[])
{
Random random = new Random();
Scanner sc = new Scanner(System.in);
System.out.print("Enter the length of list: ");
int N = sc.nextInt();
int[] sequence = new int[N];
for (int i = 1; i < N+1; i++)
sequence[i-1] = i;
System.out.println("The original sequence is: ");
for (int i = 0; i < N; i++)
System.out.print(sequence[i] + " ");
System.out.println(" The permuted sequences are as follows: ");
permute(sequence, 0);
sc.close();
}
}
Output:
Enter the length of list: 4
The original sequence is:
1 2 3 4
The permuted sequences are:
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 3 2
1 4 2 3
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 3 1
2 4 1 3
3 2 1 4
3 2 4 1
3 1 2 4
3 1 4 2
3 4 1 2
3 4 2 1
4 2 3 1
4 2 1 3
4 3 2 1
4 3 1 2
4 1 3 2
4 1 2 3

Weitere Àhnliche Inhalte

Ähnlich wie Java program to print all permutations of a given list

java slip for bachelors of business administration.pdf
java slip for bachelors of business administration.pdfjava slip for bachelors of business administration.pdf
java slip for bachelors of business administration.pdfkokah57440
 
ă‚Żă‚€ă‚șăƒ—ăƒ­ă‚°ăƒ©ăƒ 
ă‚Żă‚€ă‚șăƒ—ăƒ­ă‚°ăƒ©ăƒ ă‚Żă‚€ă‚șăƒ—ăƒ­ă‚°ăƒ©ăƒ 
ă‚Żă‚€ă‚șăƒ—ăƒ­ă‚°ăƒ©ăƒ Minori Fukuda
 
FileName EX06_1java Programmer import ja.pdf
FileName EX06_1java Programmer  import ja.pdfFileName EX06_1java Programmer  import ja.pdf
FileName EX06_1java Programmer import ja.pdfactocomputer
 
import java.util.Scanner; public class Palin { public static v.pdf
import java.util.Scanner; public class Palin { public static v.pdfimport java.util.Scanner; public class Palin { public static v.pdf
import java.util.Scanner; public class Palin { public static v.pdfLAMJM
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptxKimVeeL
 
CountStringCharacters.javaimport java.util.Scanner; public cla.pdf
CountStringCharacters.javaimport java.util.Scanner; public cla.pdfCountStringCharacters.javaimport java.util.Scanner; public cla.pdf
CountStringCharacters.javaimport java.util.Scanner; public cla.pdfpremsrivastva8
 
Java practice programs for beginners
Java practice programs for beginnersJava practice programs for beginners
Java practice programs for beginnersishan0019
 
Demonstrating bully algorithm in java
Demonstrating bully algorithm in javaDemonstrating bully algorithm in java
Demonstrating bully algorithm in javaNagireddy Dwarampudi
 
import java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfimport java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfoptokunal1
 
CODEimport java.util.; public class test { public static voi.pdf
CODEimport java.util.; public class test { public static voi.pdfCODEimport java.util.; public class test { public static voi.pdf
CODEimport java.util.; public class test { public static voi.pdfanurag1231
 
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdfSumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdfankkitextailes
 
Huraira_waris_Assgnment_4.docx
Huraira_waris_Assgnment_4.docxHuraira_waris_Assgnment_4.docx
Huraira_waris_Assgnment_4.docxhurairawarisarain23
 
import java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdf
import java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdfimport java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdf
import java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdfadhityalapcare
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple ProgramsUpender Upr
 
Core java pract_sem iii
Core java pract_sem iiiCore java pract_sem iii
Core java pract_sem iiiNiraj Bharambe
 
Sam wd programs
Sam wd programsSam wd programs
Sam wd programsSoumya Behera
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...MaruMengesha
 
JAVA.Q4 Create a Time class. This class will represent a point in.pdf
JAVA.Q4 Create a Time class. This class will represent a point in.pdfJAVA.Q4 Create a Time class. This class will represent a point in.pdf
JAVA.Q4 Create a Time class. This class will represent a point in.pdfkarymadelaneyrenne19
 

Ähnlich wie Java program to print all permutations of a given list (19)

java slip for bachelors of business administration.pdf
java slip for bachelors of business administration.pdfjava slip for bachelors of business administration.pdf
java slip for bachelors of business administration.pdf
 
ă‚Żă‚€ă‚șăƒ—ăƒ­ă‚°ăƒ©ăƒ 
ă‚Żă‚€ă‚șăƒ—ăƒ­ă‚°ăƒ©ăƒ ă‚Żă‚€ă‚șăƒ—ăƒ­ă‚°ăƒ©ăƒ 
ă‚Żă‚€ă‚șăƒ—ăƒ­ă‚°ăƒ©ăƒ 
 
FileName EX06_1java Programmer import ja.pdf
FileName EX06_1java Programmer  import ja.pdfFileName EX06_1java Programmer  import ja.pdf
FileName EX06_1java Programmer import ja.pdf
 
import java.util.Scanner; public class Palin { public static v.pdf
import java.util.Scanner; public class Palin { public static v.pdfimport java.util.Scanner; public class Palin { public static v.pdf
import java.util.Scanner; public class Palin { public static v.pdf
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptx
 
CountStringCharacters.javaimport java.util.Scanner; public cla.pdf
CountStringCharacters.javaimport java.util.Scanner; public cla.pdfCountStringCharacters.javaimport java.util.Scanner; public cla.pdf
CountStringCharacters.javaimport java.util.Scanner; public cla.pdf
 
Java practice programs for beginners
Java practice programs for beginnersJava practice programs for beginners
Java practice programs for beginners
 
Demonstrating bully algorithm in java
Demonstrating bully algorithm in javaDemonstrating bully algorithm in java
Demonstrating bully algorithm in java
 
import java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfimport java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdf
 
CODEimport java.util.; public class test { public static voi.pdf
CODEimport java.util.; public class test { public static voi.pdfCODEimport java.util.; public class test { public static voi.pdf
CODEimport java.util.; public class test { public static voi.pdf
 
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdfSumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
 
Oot practical
Oot practicalOot practical
Oot practical
 
Huraira_waris_Assgnment_4.docx
Huraira_waris_Assgnment_4.docxHuraira_waris_Assgnment_4.docx
Huraira_waris_Assgnment_4.docx
 
import java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdf
import java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdfimport java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdf
import java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdf
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
 
Core java pract_sem iii
Core java pract_sem iiiCore java pract_sem iii
Core java pract_sem iii
 
Sam wd programs
Sam wd programsSam wd programs
Sam wd programs
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
 
JAVA.Q4 Create a Time class. This class will represent a point in.pdf
JAVA.Q4 Create a Time class. This class will represent a point in.pdfJAVA.Q4 Create a Time class. This class will represent a point in.pdf
JAVA.Q4 Create a Time class. This class will represent a point in.pdf
 

Mehr von anupamselection

1. D2. A3. D4.DSolution1. D2. A3. D4.D.pdf
1. D2. A3. D4.DSolution1. D2. A3. D4.D.pdf1. D2. A3. D4.DSolution1. D2. A3. D4.D.pdf
1. D2. A3. D4.DSolution1. D2. A3. D4.D.pdfanupamselection
 
1) pH is alkalotic and it is caused due to combined (mixed) alkalosi.pdf
1) pH is alkalotic and it is caused due to combined (mixed) alkalosi.pdf1) pH is alkalotic and it is caused due to combined (mixed) alkalosi.pdf
1) pH is alkalotic and it is caused due to combined (mixed) alkalosi.pdfanupamselection
 
1) Economical for transport of goods and services2) Reducing traff.pdf
1) Economical for transport of goods and services2) Reducing traff.pdf1) Economical for transport of goods and services2) Reducing traff.pdf
1) Economical for transport of goods and services2) Reducing traff.pdfanupamselection
 
There are some general processes which you need to follow while crea.pdf
There are some general processes which you need to follow while crea.pdfThere are some general processes which you need to follow while crea.pdf
There are some general processes which you need to follow while crea.pdfanupamselection
 
All of the above.Solution All of the above..pdf
 All of the above.Solution All of the above..pdf All of the above.Solution All of the above..pdf
All of the above.Solution All of the above..pdfanupamselection
 
Ques-1A keystone species is defined as a species, which has an ex.pdf
Ques-1A keystone species is defined as a species, which has an ex.pdfQues-1A keystone species is defined as a species, which has an ex.pdf
Ques-1A keystone species is defined as a species, which has an ex.pdfanupamselection
 
PO42- is produced when CaPO4 is dissolved. Adding acid to the soluti.pdf
PO42- is produced when CaPO4 is dissolved. Adding acid to the soluti.pdfPO42- is produced when CaPO4 is dissolved. Adding acid to the soluti.pdf
PO42- is produced when CaPO4 is dissolved. Adding acid to the soluti.pdfanupamselection
 
picture(figure) is not clearSolutionpicture(figure) is not cle.pdf
picture(figure) is not clearSolutionpicture(figure) is not cle.pdfpicture(figure) is not clearSolutionpicture(figure) is not cle.pdf
picture(figure) is not clearSolutionpicture(figure) is not cle.pdfanupamselection
 
package length; A Length is an object that has a length and .pdf
package length; A Length is an object that has a length and .pdfpackage length; A Length is an object that has a length and .pdf
package length; A Length is an object that has a length and .pdfanupamselection
 
Name used on OSIName Used in TCPIP networksLayer 1(Physical)L.pdf
Name used on OSIName Used in TCPIP networksLayer 1(Physical)L.pdfName used on OSIName Used in TCPIP networksLayer 1(Physical)L.pdf
Name used on OSIName Used in TCPIP networksLayer 1(Physical)L.pdfanupamselection
 
Monthly interest rate=1212=1Present value(PV) of monthly payment.pdf
Monthly interest rate=1212=1Present value(PV) of monthly payment.pdfMonthly interest rate=1212=1Present value(PV) of monthly payment.pdf
Monthly interest rate=1212=1Present value(PV) of monthly payment.pdfanupamselection
 
Lab1.javaimport java.util.Scanner;package public class Lab1 .pdf
Lab1.javaimport java.util.Scanner;package public class Lab1 .pdfLab1.javaimport java.util.Scanner;package public class Lab1 .pdf
Lab1.javaimport java.util.Scanner;package public class Lab1 .pdfanupamselection
 
Interest coverage=EBITinterest expensewhich is equal to=(40020.pdf
Interest coverage=EBITinterest expensewhich is equal to=(40020.pdfInterest coverage=EBITinterest expensewhich is equal to=(40020.pdf
Interest coverage=EBITinterest expensewhich is equal to=(40020.pdfanupamselection
 
In this module, you will learn some basics about operating in Object.pdf
In this module, you will learn some basics about operating in Object.pdfIn this module, you will learn some basics about operating in Object.pdf
In this module, you will learn some basics about operating in Object.pdfanupamselection
 
y = 12 x=0 z=0 .pdf
                     y = 12 x=0 z=0                                  .pdf                     y = 12 x=0 z=0                                  .pdf
y = 12 x=0 z=0 .pdfanupamselection
 
When comparing the R groups of these to amino aci.pdf
                     When comparing the R groups of these to amino aci.pdf                     When comparing the R groups of these to amino aci.pdf
When comparing the R groups of these to amino aci.pdfanupamselection
 
Malonic Acid Water Soluble Methyl Alcohol Solu.pdf
                     Malonic Acid  Water Soluble Methyl Alcohol Solu.pdf                     Malonic Acid  Water Soluble Methyl Alcohol Solu.pdf
Malonic Acid Water Soluble Methyl Alcohol Solu.pdfanupamselection
 
It is true that ionization energy is required to .pdf
                     It is true that ionization energy is required to .pdf                     It is true that ionization energy is required to .pdf
It is true that ionization energy is required to .pdfanupamselection
 
Ionic compounds form when the electronegativity d.pdf
                     Ionic compounds form when the electronegativity d.pdf                     Ionic compounds form when the electronegativity d.pdf
Ionic compounds form when the electronegativity d.pdfanupamselection
 
If R is reflexive, then xRy means yRx; but then, .pdf
                     If R is reflexive, then xRy means yRx; but then, .pdf                     If R is reflexive, then xRy means yRx; but then, .pdf
If R is reflexive, then xRy means yRx; but then, .pdfanupamselection
 

Mehr von anupamselection (20)

1. D2. A3. D4.DSolution1. D2. A3. D4.D.pdf
1. D2. A3. D4.DSolution1. D2. A3. D4.D.pdf1. D2. A3. D4.DSolution1. D2. A3. D4.D.pdf
1. D2. A3. D4.DSolution1. D2. A3. D4.D.pdf
 
1) pH is alkalotic and it is caused due to combined (mixed) alkalosi.pdf
1) pH is alkalotic and it is caused due to combined (mixed) alkalosi.pdf1) pH is alkalotic and it is caused due to combined (mixed) alkalosi.pdf
1) pH is alkalotic and it is caused due to combined (mixed) alkalosi.pdf
 
1) Economical for transport of goods and services2) Reducing traff.pdf
1) Economical for transport of goods and services2) Reducing traff.pdf1) Economical for transport of goods and services2) Reducing traff.pdf
1) Economical for transport of goods and services2) Reducing traff.pdf
 
There are some general processes which you need to follow while crea.pdf
There are some general processes which you need to follow while crea.pdfThere are some general processes which you need to follow while crea.pdf
There are some general processes which you need to follow while crea.pdf
 
All of the above.Solution All of the above..pdf
 All of the above.Solution All of the above..pdf All of the above.Solution All of the above..pdf
All of the above.Solution All of the above..pdf
 
Ques-1A keystone species is defined as a species, which has an ex.pdf
Ques-1A keystone species is defined as a species, which has an ex.pdfQues-1A keystone species is defined as a species, which has an ex.pdf
Ques-1A keystone species is defined as a species, which has an ex.pdf
 
PO42- is produced when CaPO4 is dissolved. Adding acid to the soluti.pdf
PO42- is produced when CaPO4 is dissolved. Adding acid to the soluti.pdfPO42- is produced when CaPO4 is dissolved. Adding acid to the soluti.pdf
PO42- is produced when CaPO4 is dissolved. Adding acid to the soluti.pdf
 
picture(figure) is not clearSolutionpicture(figure) is not cle.pdf
picture(figure) is not clearSolutionpicture(figure) is not cle.pdfpicture(figure) is not clearSolutionpicture(figure) is not cle.pdf
picture(figure) is not clearSolutionpicture(figure) is not cle.pdf
 
package length; A Length is an object that has a length and .pdf
package length; A Length is an object that has a length and .pdfpackage length; A Length is an object that has a length and .pdf
package length; A Length is an object that has a length and .pdf
 
Name used on OSIName Used in TCPIP networksLayer 1(Physical)L.pdf
Name used on OSIName Used in TCPIP networksLayer 1(Physical)L.pdfName used on OSIName Used in TCPIP networksLayer 1(Physical)L.pdf
Name used on OSIName Used in TCPIP networksLayer 1(Physical)L.pdf
 
Monthly interest rate=1212=1Present value(PV) of monthly payment.pdf
Monthly interest rate=1212=1Present value(PV) of monthly payment.pdfMonthly interest rate=1212=1Present value(PV) of monthly payment.pdf
Monthly interest rate=1212=1Present value(PV) of monthly payment.pdf
 
Lab1.javaimport java.util.Scanner;package public class Lab1 .pdf
Lab1.javaimport java.util.Scanner;package public class Lab1 .pdfLab1.javaimport java.util.Scanner;package public class Lab1 .pdf
Lab1.javaimport java.util.Scanner;package public class Lab1 .pdf
 
Interest coverage=EBITinterest expensewhich is equal to=(40020.pdf
Interest coverage=EBITinterest expensewhich is equal to=(40020.pdfInterest coverage=EBITinterest expensewhich is equal to=(40020.pdf
Interest coverage=EBITinterest expensewhich is equal to=(40020.pdf
 
In this module, you will learn some basics about operating in Object.pdf
In this module, you will learn some basics about operating in Object.pdfIn this module, you will learn some basics about operating in Object.pdf
In this module, you will learn some basics about operating in Object.pdf
 
y = 12 x=0 z=0 .pdf
                     y = 12 x=0 z=0                                  .pdf                     y = 12 x=0 z=0                                  .pdf
y = 12 x=0 z=0 .pdf
 
When comparing the R groups of these to amino aci.pdf
                     When comparing the R groups of these to amino aci.pdf                     When comparing the R groups of these to amino aci.pdf
When comparing the R groups of these to amino aci.pdf
 
Malonic Acid Water Soluble Methyl Alcohol Solu.pdf
                     Malonic Acid  Water Soluble Methyl Alcohol Solu.pdf                     Malonic Acid  Water Soluble Methyl Alcohol Solu.pdf
Malonic Acid Water Soluble Methyl Alcohol Solu.pdf
 
It is true that ionization energy is required to .pdf
                     It is true that ionization energy is required to .pdf                     It is true that ionization energy is required to .pdf
It is true that ionization energy is required to .pdf
 
Ionic compounds form when the electronegativity d.pdf
                     Ionic compounds form when the electronegativity d.pdf                     Ionic compounds form when the electronegativity d.pdf
Ionic compounds form when the electronegativity d.pdf
 
If R is reflexive, then xRy means yRx; but then, .pdf
                     If R is reflexive, then xRy means yRx; but then, .pdf                     If R is reflexive, then xRy means yRx; but then, .pdf
If R is reflexive, then xRy means yRx; but then, .pdf
 

KĂŒrzlich hochgeladen

Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
USPSÂź Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPSÂź Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPSÂź Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPSÂź Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
call girls in Kamla Market (DELHI) 🔝 >àŒ’9953330565🔝 genuine Escort Service đŸ”âœ”ïžâœ”ïž
call girls in Kamla Market (DELHI) 🔝 >àŒ’9953330565🔝 genuine Escort Service đŸ”âœ”ïžâœ”ïžcall girls in Kamla Market (DELHI) 🔝 >àŒ’9953330565🔝 genuine Escort Service đŸ”âœ”ïžâœ”ïž
call girls in Kamla Market (DELHI) 🔝 >àŒ’9953330565🔝 genuine Escort Service đŸ”âœ”ïžâœ”ïž9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
HỌC TỐT TIáșŸNG ANH 11 THEO CHÆŻÆ NG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIáșŸT - Cáșą NĂ...
HỌC TỐT TIáșŸNG ANH 11 THEO CHÆŻÆ NG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIáșŸT - Cáșą NĂ...HỌC TỐT TIáșŸNG ANH 11 THEO CHÆŻÆ NG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIáșŸT - Cáșą NĂ...
HỌC TỐT TIáșŸNG ANH 11 THEO CHÆŻÆ NG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIáșŸT - Cáșą NĂ...Nguyen Thanh Tu Collection
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 

KĂŒrzlich hochgeladen (20)

Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
USPSÂź Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPSÂź Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPSÂź Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPSÂź Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
call girls in Kamla Market (DELHI) 🔝 >àŒ’9953330565🔝 genuine Escort Service đŸ”âœ”ïžâœ”ïž
call girls in Kamla Market (DELHI) 🔝 >àŒ’9953330565🔝 genuine Escort Service đŸ”âœ”ïžâœ”ïžcall girls in Kamla Market (DELHI) 🔝 >àŒ’9953330565🔝 genuine Escort Service đŸ”âœ”ïžâœ”ïž
call girls in Kamla Market (DELHI) 🔝 >àŒ’9953330565🔝 genuine Escort Service đŸ”âœ”ïžâœ”ïž
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
HỌC TỐT TIáșŸNG ANH 11 THEO CHÆŻÆ NG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIáșŸT - Cáșą NĂ...
HỌC TỐT TIáșŸNG ANH 11 THEO CHÆŻÆ NG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIáșŸT - Cáșą NĂ...HỌC TỐT TIáșŸNG ANH 11 THEO CHÆŻÆ NG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIáșŸT - Cáșą NĂ...
HỌC TỐT TIáșŸNG ANH 11 THEO CHÆŻÆ NG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIáșŸT - Cáșą NĂ...
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 

Java program to print all permutations of a given list

  • 1. Code: import java.util.Random; import java.util.Scanner; class Test{ static void permute(int[] a, int k){ if (k == a.length){ for (int i = 0; i < a.length; i++){ System.out.print(a[i] + " "); } System.out.println(); }else{ for (int i = k; i < a.length; i++){ int temp = a[k]; a[k] = a[i]; a[i] = temp; permute(a, k + 1); temp = a[k]; a[k] = a[i]; a[i] = temp; } } } public static void main(String args[]) { Random random = new Random(); Scanner sc = new Scanner(System.in); System.out.print("Enter the length of list: "); int N = sc.nextInt(); int[] sequence = new int[N]; for (int i = 1; i < N+1; i++)
  • 2. sequence[i-1] = i; System.out.println("The original sequence is: "); for (int i = 0; i < N; i++) System.out.print(sequence[i] + " "); System.out.println(" The permuted sequences are as follows: "); permute(sequence, 0); sc.close(); } } Output: Enter the length of list: 4 The original sequence is: 1 2 3 4 The permuted sequences are: 1 2 3 4 1 2 4 3 1 3 2 4 1 3 4 2 1 4 3 2 1 4 2 3 2 1 3 4 2 1 4 3 2 3 1 4 2 3 4 1 2 4 3 1 2 4 1 3 3 2 1 4 3 2 4 1 3 1 2 4 3 1 4 2 3 4 1 2 3 4 2 1 4 2 3 1
  • 3. 4 2 1 3 4 3 2 1 4 3 1 2 4 1 3 2 4 1 2 3 Solution Code: import java.util.Random; import java.util.Scanner; class Test{ static void permute(int[] a, int k){ if (k == a.length){ for (int i = 0; i < a.length; i++){ System.out.print(a[i] + " "); } System.out.println(); }else{ for (int i = k; i < a.length; i++){ int temp = a[k]; a[k] = a[i]; a[i] = temp; permute(a, k + 1); temp = a[k]; a[k] = a[i]; a[i] = temp; } } } public static void main(String args[]) {
  • 4. Random random = new Random(); Scanner sc = new Scanner(System.in); System.out.print("Enter the length of list: "); int N = sc.nextInt(); int[] sequence = new int[N]; for (int i = 1; i < N+1; i++) sequence[i-1] = i; System.out.println("The original sequence is: "); for (int i = 0; i < N; i++) System.out.print(sequence[i] + " "); System.out.println(" The permuted sequences are as follows: "); permute(sequence, 0); sc.close(); } } Output: Enter the length of list: 4 The original sequence is: 1 2 3 4 The permuted sequences are: 1 2 3 4 1 2 4 3 1 3 2 4 1 3 4 2 1 4 3 2 1 4 2 3 2 1 3 4 2 1 4 3 2 3 1 4 2 3 4 1 2 4 3 1 2 4 1 3
  • 5. 3 2 1 4 3 2 4 1 3 1 2 4 3 1 4 2 3 4 1 2 3 4 2 1 4 2 3 1 4 2 1 3 4 3 2 1 4 3 1 2 4 1 3 2 4 1 2 3