File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
CodingBlocks Training/Day12 Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ package Lecture12 ;
2
+
3
+ public class PermutationWithSwap {
4
+
5
+ public static void main (String [] args ) {
6
+
7
+ char [] arr = { 'a' , 'b' , 'c' };
8
+ int front = 0 , back = arr .length - 1 ;
9
+ printPermutation (arr , front , back );
10
+ }
11
+
12
+ public static void printPermutation (char [] arr , int front , int back ) {
13
+ if (front > back ) {
14
+ display (arr );
15
+ }
16
+ for (int i = front ; i < arr .length ; i ++) {
17
+ swap (arr , front , i );
18
+ printPermutation (arr , front + 1 , back );
19
+ swap (arr , front , i );
20
+ }
21
+ }
22
+
23
+ public static void display (char [] arr ) {
24
+ for (int i = 0 ; i < arr .length ; i ++) {
25
+ System .out .print (arr [i ]);
26
+ }
27
+ System .out .println ();
28
+ }
29
+
30
+ public static void swap (char [] arr , int index1 , int index2 ) {
31
+ char temp = arr [index1 ];
32
+ arr [index1 ] = arr [index2 ];
33
+ arr [index2 ] = temp ;
34
+ }
35
+ }
36
+
37
+
38
+ /* output:
39
+ abc
40
+ acb
41
+ bac
42
+ bca
43
+ cab
44
+ cba
45
+ */
You can’t perform that action at this time.
0 commit comments