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 8205731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:22:23+00:00 2026-06-07T08:22:23+00:00

I have found this code the internet to computer all possible Permutations for a

  • 0

I have found this code the internet to computer all possible Permutations for a given Vector ..

import java.util.Vector;

class Permute {
    static int count = 0;

    public static void permute(Vector unvisited, Vector visited) {
    if ( unvisited.isEmpty() ) {
        System.out.println("Permutation: "+visited);
        count++;
    }
    else { 
        //System.out.println("Trace: "+visited+" "+unvisited);
        int l = unvisited.size();
        for(int i = 0; i<l; i++) {
        String next = String.valueOf(unvisited.remove(i));
        visited.add(next);
        permute(unvisited,visited);
        unvisited.add(i,next);
        visited.remove(next);
        }
    }
    }

    public static void main(String[] args) {    
    Vector objects = new Vector();

    objects.add(1);
    objects.add(5);
    objects.add(8);

    permute(objects, new Vector() );
        System.out.println(count+" Permutationen gefunden");
    }
}

Bu I have a small problem in understanding the code and the flow of the instructions .
What I misses is when these two lines are called

    unvisited.add(i,next);
    visited.remove(next);

As I see there is a recursion permute(..) function before reaching them !

  • 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. Editorial Team
    Editorial Team
    2026-06-07T08:22:26+00:00Added an answer on June 7, 2026 at 8:22 am

    First try to understand what the permute() function does. It juggles two vectors: unvisited and visited. In the execution of permute(), the function will take an element from unvisited and move it to visited. Think of it as “building” a permutation.

    For example:

    Unvisited: 1, 5, 8
    visited: 
    

    we move an element from unvisited to visited

    Unvisited:  5, 8
    visited: 1
    

    Now to build the rest of the permutations that start with 1, we need to find the permutations of unvisited, or {5,8}. So we recursively call permute() to find the rest.

    So what does

    unvisited.add(i,next);
    visited.remove(next);
    

    do ?

    As each permutation is being built, the function is modifying a unified/common/shared source of data: the vectors. They are constantly being modified as elements are shifted from unvisited to visited. However, we need to reset these vectors to find the rest of the permutations. In the example above, we need to put 1 back to unvisited find the permutations that start with 5, or 8. So we:

    unvisited.add(i,next); //add it back to unvisited in its original position 
    

    and

    visited.remove(next); //remove it from visited.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found this line of MatLab code on the internet that displays a
Hi i have problem with this code, i found it on the internet and
this is the code that i have found in the internet for reading the
I have been looking all over the Internet for this and have found similar,
I have found this code for reverse geocoding: var point = new GLatLng (lat[1],long[1]);
I have found this project on Codeplex. http://www.codeplex.com/ProjNET I need to integrate this code
I want to force the download of an image. I have found this code:
I have found this line of code in a game that I study int
I have found this piece of code in the Haskell sendfile package: http://patch-tag.com/r/mae/sendfile/snapshot/current/content/pretty/src/Network/Socket/SendFile/Linux.hsc --
I have this code which I have found from google. It's a adapter for

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.