# Finding Permutations�Easier and Faster

### WEBINAR:On-Demand

Application Security Testing: An Integral Part of DevOps

### Introduction

This article explains the technique of finding permutations in a simple and fast manner. It also provides the source code for the same.

### Explanation

For a given string of length N, there are actually N! Permutations. The technique we apply here is to find the unique of those permutations and display them.

Let's take this string: 123. If we rotate this string in a circular manner, we get 231 and 312. The point here is, if we find 1 string of the N! Permutations, we can produce N permutations by rotating it circularly. This reduces the time, which is proportional to N. The greater the value of N, the more the algorithm is optimized.

In short, the algorithm finds a number and rotates the number circularly N times to get N numbers.

Lets apply the algorithm. Lets take string {12} of length 2 (N=2). The unique pattern is 12 and if we rotate we get 21.

Let's take the string {123} of length 3 (N=3). The unique patterns are as follows:

1(23): 123 is unique pattern resulting in (123)
1(32): Rotate the pattern (23) u get another pattern (32) resulting in a pattern (132)
(231): By rotating the pattern (123)
(312): By rotating the pattern (123)
(321): By rotating the pattern (132)
(213): By rotating the pattern (132)

The output patterns are:

 Level 1 Level 2 Permutations 123 1(23) 123 231 312 1(32) 132 321 213

If we apply the same algorithm for the string {1234} of length 4 (N=4), the patterns are as below:

 Level 1 Level 2 Level 3 Permutations 1234 12(34) 1(234) 1234 2341 3412 4123 1(342) 1342 3421 4213 2134 1(423) 1423 4231 2314 3142 12(43) 1(243) 1243 2431 4312 3124 1(432) 1432 4321 3214 2143 1(324) 1324 3241 2413 4132

### Comparison

The traditional algorithm goes and finds all the permutations where, as in this algorithm, we find only the unique permutation and produce the other permutations by rotating the original permutation in a circular manner.

### Conclusion

The algorithm finds only the unique numbers and finds the other (N) permutations by rotating the original permutation circularly. Hence, it increases the performance.

#### Srinivasan Muthuswamy Sivaraman

Working on Java/C++/C/Windows/Linux technologies Hobbies: playing with numbers.

Posted by backlinks on 09/05/2012 11:17pm

Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You obviously know what youre talking about, why throw away your intelligence on just posting videos to your blog when you could be giving us something informative to read?

• #### try

Posted by try on 07/13/2012 01:41pm

try...

• #### solution

Posted by naveen kumar on 07/13/2012 01:39pm

Ex : 1234 rotate once right ... 4123,3412,2341,1234. reverse these numbers.. 3214,2143,1432,4321. Fix first digit of original number and rotate rest to right we get 1423... apply same algo. here. 3142,2314,4231,1423. reverse these number... 2413,4132,1324,3241. Rotate last time 1342 (Total rotations length-1 times).. 2134,4213,3421,1342.. reverse.. 4312,3124,1243,2431. Final Answere 24 values : 4123,3412,2341,1234. 3214,2143,1432,4321. 3142,2314,4231,1423. 2413,4132,1324,3241. 2134,4213,3421,1342. 4312,3124,1243,2431.

• #### good algo

Posted by mail_suka@rediffmail.com on 07/15/2004 11:24am

hi, nice algo. btw, here is a interesting prob for u. how many ways, we can arrange 1 to n (n can be 10, 100 or 1000, any number) so that no number comes in its original position? (meaning 2 shud not appear in 2nd position, after arrangement). consider space, time comlpexity when u design the algo. regards;

• #### Good one

Posted by kandukondein on 07/10/2004 02:28pm

Hey Thats a good one. A good tutorial for beginners. Regards;

• You must have javascript enabled in order to post comments.

## Top White Papers and Webcasts

• As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
• The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
• Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

## Most Popular Programming Stories

• There have been no articles posted today.