DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Programming Problem: return every possible order of an array.  (Read 6,181 times)

alivingspirit

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 167
    • Programming School - Stage 1 Medal
    • Programming School - Deluxe Bonus Medal
I'm looking for a simple algorithm that will give me every possible order of an array regardless of size.
For instance: An array of three elements [ 1, 2, 3 ] will give me [ 1, 2, 3 ], [ 1, 3, 2 ], [ 2, 1, 3 ], [ 2, 3, 1 ], [ 3, 1, 2 ] and [ 3, 2, 1 ]. It seems like there should be a pretty simple mathematical solution for this but I can't seem to figure it out or find it anywhere. :(

f0dder

  • [Well, THAT escalated quickly!]
  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
    • Participated in NANY Challenge Once
Re: Programming Problem: return every possible order of an array.
« Reply #1 on: March 23, 2009, 12:02 AM »
C++, <algorithm>, std::next_permutation ? :)
- carpe noctem

housetier

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 1,321
    • Gathering Badge
Re: Programming Problem: return every possible order of an array.
« Reply #2 on: March 23, 2009, 12:04 AM »
Wikipedia's page about permutations has example code. I find them quite simple :)

alivingspirit

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 167
    • Programming School - Stage 1 Medal
    • Programming School - Deluxe Bonus Medal
Re: Programming Problem: return every possible order of an array.
« Reply #3 on: March 23, 2009, 12:14 AM »
Cool. Thanks guys.  :)

Original address: https://www.donationcoder.com/forum/index.php?topic=17564.0