Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 92259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:09:32+00:00 2026-05-10T23:09:32+00:00

I’m looking for an algorithm to generate permutations of a set in such a

  • 0

I’m looking for an algorithm to generate permutations of a set in such a way that I could make a lazy list of them in Clojure. i.e. I’d like to iterate over a list of permutations where each permutation is not calculated until I request it, and all of the permutations don’t have to be stored in memory at once.

Alternatively I’m looking for an algorithm where given a certain set, it will return the ‘next’ permutation of that set, in such a way that repeatedly calling the function on its own output will cycle through all permutations of the original set, in some order (what the order is doesn’t matter).

Is there such an algorithm? Most of the permutation-generating algorithms I’ve seen tend to generate them all at once (usually recursively), which doesn’t scale to very large sets. An implementation in Clojure (or another functional language) would be helpful but I can figure it out from pseudocode.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T23:09:33+00:00Added an answer on May 10, 2026 at 11:09 pm

    Yes, there is a ‘next permutation’ algorithm, and it’s quite simple too. The C++ standard template library (STL) even has a function called next_permutation.

    The algorithm actually finds the next permutation — the lexicographically next one. The idea is this: suppose you are given a sequence, say ‘32541’. What is the next permutation?

    If you think about it, you’ll see that it is ‘34125’. And your thoughts were probably something this: In ‘32541’,

    • there is no way to keep the ’32’ fixed and find a later permutation in the ‘541’ part, because that permutation is already the last one for 5,4, and 1 — it is sorted in decreasing order.
    • So you’ll have to change the ‘2’ to something bigger — in fact, to the smallest number bigger than it in the ‘541’ part, namely 4.
    • Now, once you’ve decided that the permutation will start as ’34’, the rest of the numbers should be in increasing order, so the answer is ‘34125’.

    The algorithm is to implement precisely that line of reasoning:

    1. Find the longest ‘tail’ that is ordered in decreasing order. (The ‘541’ part.)
    2. Change the number just before the tail (the ‘2’) to the smallest number bigger than it in the tail (the 4).
    3. Sort the tail in increasing order.

    You can do (1.) efficiently by starting at the end and going backwards as long as the previous element is not smaller than the current element. You can do (2.) by just swapping the ‘4’ with the ‘2’, so you’ll have ‘34521’. Once you do this, you can avoid using a sorting algorithm for (3.), because the tail was, and is still (think about this), sorted in decreasing order, so it only needs to be reversed.

    The C++ code does precisely this (look at the source in /usr/include/c++/4.0.0/bits/stl_algo.h on your system, or see this article); it should be simple to translate it to your language: [Read ‘BidirectionalIterator’ as ‘pointer’, if you’re unfamiliar with C++ iterators. The code returns false if there is no next permutation, i.e. we are already in decreasing order.]

    template <class BidirectionalIterator> bool next_permutation(BidirectionalIterator first,                       BidirectionalIterator last) {     if (first == last) return false;     BidirectionalIterator i = first;     ++i;     if (i == last) return false;     i = last;     --i;     for(;;) {         BidirectionalIterator ii = i--;         if (*i <*ii) {             BidirectionalIterator j = last;             while (!(*i <*--j));             iter_swap(i, j);             reverse(ii, last);             return true;         }         if (i == first) {             reverse(first, last);             return false;         }     } } 

    It might seem that it can take O(n) time per permutation, but if you think about it more carefully, you can prove that it takes O(n!) time for all permutations in total, so only O(1) — constant time — per permutation.

    The good thing is that the algorithm works even when you have a sequence with repeated elements: with, say, ‘232254421’, it would find the tail as ‘54421’, swap the ‘2’ and ‘4’ (so ‘232454221’), reverse the rest, giving ‘232412245’, which is the next permutation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you running on OS 3.0? I saw the same… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer It looks like you need to register Apache::Session::Memcached with Apache::Session::Wrapper,… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer Use DATENAME or DATEPART: SELECT DATENAME(dw,GETDATE()) -- Friday SELECT DATEPART(dw,GETDATE())… May 12, 2026 at 1:19 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.