Finding Permutations�Easier and Faster


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


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    
1(23) 123
1(32) 132

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
1(234) 1234
1(342) 1342
1(423) 1423
1(243) 1243
1(432) 1432
1(324) 1324


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.


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

About the Author

Srinivasan Muthuswamy Sivaraman

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



  • Impressed

    Posted by Shivinder Singh Narr on 03/12/2013 11:59am

    Hey, I have implemented your algorithm of permutation. It creates all permutations of a string. It was a great idea. If you observe, it's time complexity is very less than traditional methods. I am saying this because I have used inbuilt permutation function in python. I analysed the time and observed that This algorithm implemented in C, requires less time than other methods. According to my program, it's time complexity is O(n^4). If you have anything to share about much more faster algorithm than this, please reply to my email address.

  • backlinks

    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


  • 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 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;

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Complex hybrid environments can make it difficult to track interdependencies, increasing the risk of disrupting critical business services. In this white paper by EMA, you'll learn how application discovery and dependency mapping can help you: Meet granular targets for availability, cost, and time-to-revenue for cloud services. Accelerate mean time to repair (MTTR) while communicating better with stakeholders. Manage even the most complex hybrid environments more efficiently and effectively Understand the …

  • Rocket Mobile® for IBM i is an enterprise mobile application development and deployment platform for customers who rely on the IBM i system. Rocket Mobile for IBM i enables customers to leave proven applications in-place and rapidly repurpose them into new managed and secure mobile applications. Fast, easy creation of mobile, web, and hybrid mobile applications that deploy to any iOS, Android, or Windows mobile phone or tablet Built-in integration for seamless repurposing of existing IBM i applications …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date