Click here to Skip to main content
12,396,230 members (68,674 online)
Click here to Skip to main content


25 bookmarked

Implementing Permutation Variations

, 14 Jul 2004 CPOL
Several enhanced permutation algorithms created in iterative or recursive solution.
// ============================================================================
// File:    Permutate.cpp - Main test of three character permutations
// Author:  Zuoliu Ding, May 2004
// Example: Permutation of characters in the string abc:
//          abc acb bac bca cab cba
// ============================================================================
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <time.h>
using namespace std;

// Three Permutation method prototypes
vector<string> StlPermutation(const char*, bool bOrdered=true);
vector<string> RecPermutation(const char*, bool bRepeated=true);
vector<string> example_02(const char* a);
//vector<string> StlPermutation(const char*);//=true);
//vector<string> RecPermutation(const char*);//=true);

// ============================================================================
// Function:    main
// Description: Test permutation functions.
// ============================================================================
void main()
   char         szSel[8];
   char          sz[32]; // "abcdefghijk"; // 

   while (true) 
      cout << "Enter the first Letter to Select a Permutation Method:\n";
      cout << "-----------------------------------------------------------------------\n";
      cout << "S)TL   ";
      cout << "STL-N)o-Ordered   ";
      cout << "R)ecursion   ";
      cout << "Re-U)nrepeated   ";
//      cout << "I)teration,Enh  ";
      cout << "P)hil's   ";
      cout << "E)xit\n->";

      cin.getline(szSel, 8);
      *szSel = toupper(*szSel);
      if (*szSel =='E') return;

      if (*szSel !='R' && *szSel !='U' && *szSel !='S' && *szSel !='P' && *szSel !='N') 
         cout << "Unrecognized Selection - Try again...\n\n";

      cout << "Enter a String for Permutation: ";
      cin.getline(sz, 32);
      cout << "Waiting ... ";

      vector<string> vStr;
      time_t t1, t2;

      vStr =(*szSel =='R')? RecPermutation(sz): 
            (*szSel =='U')? RecPermutation(sz, false): 
            (*szSel =='S')? StlPermutation(sz):
            (*szSel =='N')? StlPermutation(sz, false): example_02(sz);
//            (*szSel =='I')? EnhPermutationIteration(sz): StlPermutation(sz);


      cout << "\nTotal " << vStr.size() <<" permutations generated in result.txt.\n";

      if (strlen(sz)<5)
         for (int i=0; i< (int)vStr.size(); i++) 
            if (i) cout <<", ";
            cout << vStr[i];
         cout << endl;

      cout << "The time: " << long(t2-t1) <<" seconds.\n";

      if (strlen(sz)<11)
         cout << "File saving ... ";

         ofstream fout("result.txt");   // Output file for all solutions
         fout <<((*szSel =='R')? "Recursive Permutation, Allow Repeated:": 
                 (*szSel =='U')? "Recursive Permutation, Not Repeated:": 
                 (*szSel =='S')? "STL, next_permutation, Ordered:":
                 (*szSel =='N')? "STL, next_permutation, Not Ordered:":
                                 "Iteration, Philip Paul's Example:") <<endl;

         for (int i=0; i< (int)vStr.size(); i++) 
            if (i) fout <<", ";
            fout << vStr[i];
         fout << endl;

      cout << endl << endl;

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Zuoliu Ding
Software Developer
United States United States
An Adjunct Faculty and Software Developer in Los Angeles and Orange County, CA

* Typical articles published in Dr. Dobb’s Journal and Windows Developer Magazine:

- A Silent Component Update for Internet Explorer
- Silent Application Update
- An MDI-Style Web Browser and Load Spy Monitor
- Implementing Wireless Print for WinNT/Win2K
- Multi-State Checkbox Tree Views
- A Generic Tool Tip Class
- An Easy Way to Add Tool Tips to Any MFC Control

- More from Google...

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 15 Jul 2004
Article Copyright 2004 by Zuoliu Ding
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid