Sunday, July 8, 2007

Write a C program generate permutations.

Iterative C program


#include < stdio.h >
#define SIZE 3
int main(char *argv[],int argc)
{
char list[3]={'a','b','c'};
int i,j,k;

for(i=0;i < SIZE;i++)
for(j=0;j < SIZE;j++)
for(k=0;k < SIZE;k++)
if(i!=j && j!=k && i!=k)
printf("%c%c%c\n",list[i],list[j],list[k]);

return(0);
}



Recursive C program


#include < stdio.h >
#define N 5


int main(char *argv[],int argc)
{
char list[5]={'a','b','c','d','e'};
permute(list,0,N);
return(0);
}


void permute(char list[],int k, int m)
{
int i;
char temp;

if(k==m)
{
/* PRINT A FROM k to m! */
for(i=0;i < N;i++){printf("%c",list[i]);}
printf("\n");
}
else
{
for(i=k;i < m;i++)
{
/* swap(a[i],a[m-1]); */
temp=list[i];
list[i]=list[m-1];
list[m-1]=temp;

permute(list,k,m-1);

/* swap(a[m-1],a[i]); */

temp=list[m-1];
list[m-1]=list[i];
list[i]=temp;
}
}
}

54 comments:

Anonymous said...

even though this site is useful the contents are not original

Anonymous said...

The recursive answer has some extraneous stuff in it. You'll note that the 2nd param to permute() will always be 0. So, you really don't need it. Then the check to print it out will be if ( m == 0 )

Unknown said...

how to write a c++ program to generate all the permutations of a number using stack.

kingsley said...

Right code in C++(Kingsley Sithole):

#include "stdafx.h"
#include
#include

using namespace std;

void ReverseString(char *left, char *right)
{
char text;
while (left < right)
{
text = *left;
*left = *right;
*right = text;
++left;
--right;
}
}

void ReverseWords(char *Reverse)
{

char *start = Reverse;
char *stop = Reverse;
while (*stop != '\0')
++stop;
--stop;

ReverseString(start, stop);

while (*start != '\0')
{

for ( ; *start != '\0' && *start == ' '; start++);

for (stop = start; *stop != '\0' && *stop != ' '; stop++);
--stop;
ReverseString(start, stop);
start = ++stop;
}
}
int main (int argc, char * const argv[]) {

char Sentence[200] ;
cout<<"Enter Sentence:"<<endl;
cin.getline(&Sentence[0],'0');
cout << "Before Reversal = '" << Sentence<< "'" << endl;
ReverseWords(Sentence);
cout << "After Reversal = '" << Sentence<< "'" << endl;

return 0;
}

Gauz said...

Code in Java

public class Test{
public static void main(String []args)
{
permute("dog");
}

private static void permute(String str)
{
int length = str.length();
boolean used[] = new boolean[length];
StringBuffer out = new StringBuffer();
char[] in = str.toCharArray();

doPermute(in, out, used, length, 0);
}

private static void doPermute(char[] in, StringBuffer out, boolean []used, int length, int level)
{
if(level == length)
{
System.out.println(out.toString());
return;
}

for(int i = 0; i < length; i++)
{
if(used[i])
continue;
out.append(in[i]);
used[i] = true;
doPermute(in, out, used, length, level+1);
used[i] = false;
out.setLength(out.length()-1);
}
}
}

Anonymous said...

void perm(char *str,int start, int end){

int i;
char temp[256];
strcpy(temp,str);
if(start == end){
printf("%s\n",str);
}else{
for(i = start; i <= end; i++){
if(str[i] != str[start])
str[i]^= str[start]^= str[i]^=str[start];
perm(str,start+1,end);
}
strcpy(str,temp);
}
}

subburajr said...

the one which I tried
oid perm(char *str,int start, int end){

int i;
char temp[256];
strcpy(temp,str);
if(start == end){
printf("%s\n",str);
}else{
for(i = start; i <= end; i++){
if(str[i] != str[start])
str[i]^= str[start]^= str[i]^=str[start];
perm(str,start+1,end);
}
strcpy(str,temp);
}
}

Jasonjin said...

Can anyone explain the algorithm of this recursive permutation?

Anonymous said...

Watches are more expensive than the rest of these items, so you may want to give a watch on a special occasion like Valentine Day, an anniversary, a birthday, or Christmas The material in the jerseys is not this old stuff from time long ago If North Korea wearing white face of the tragedy is just one example, that England and the South Korean team returned with a white one after another was murdered In truth, 44 members of the group through the years are put into the Baseball Hall of Fame An reliable shirt can also be verified by its serial variety and the type of substance applied
NFL video games appeal to numerous spectators every single supporting one of the teams around the field and parading about in their colors and NFL jerseys emblazoned with the logo of their team There are numerous websites that will help you get acquainted with fantasy football The team will be very thankful and reciprocate your deep appreciation toward the game and the playersSince the Colts drafted Manning in 1998, the team has the highest conversion rate on 3rd down (44 You will find several types that are offered, most of them should be borne for those individuals able yet others are directed particularly for ladies And there is a feeling that the Seahawks hope he gets down to their pick at 25
This two men are alike in their sports rank, and they have widespread interests: left and comeback All fixed, the No All fixed, the NoDoes the School offer Mortgage education courses that are accessible 24/7 and 100% onlineMake sure your NMLS classes are truly 100% online Hence they could notice set placed video game blog titles in your house, is definitely amazing for making a try to complete for the world Private institutes facilitate this very need and provide courses in your convenient timeIn 2002 Taylor led the NFL and tied the Dolphin team record for sacks with 18

[url=http://www.redskinsnflprostore.com/alfred-morris-jersey-elite-c-6_55.html]alfred morris redskins jersey[/url]
[url=http://www.redskinsnflprostore.com/ryan-kerrigan-jersey-elite-c-6_25.html]ryan kerrigan redskins jersey[/url]
[url=http://www.officialtexansfanstore.com/brian-cushing-jersey-cheap]Womens Brian Cushing Jersey[/url]

Anonymous said...

order an [URL=http://e--store.com/]coach outlet online[/URL] and check coupon code available vNsSlJRO [URL=http://e--store.com/ ] http://e--store.com/ [/URL]

Anonymous said...

Hola! I've been following your weblog for some time now and finally got the courage to go ahead and give you a shout out from Huffman Texas! Just wanted to say keep up the excellent job!
Feel free to surf my blog post - opportunity

Anonymous said...

Hi www.blogger.com owner, are you interested in links exchange? Reply to my email
[url=http://www.youtube.com/watch?v=xikJ5Z55nRU]Unlock Nokia Lumia 920[/url]
[url=http://www.youtube.com/watch?v=zPGeNetKo3o]Unlock Motorola Electrify M[/url]
[url=http://www.youtube.com/watch?v=nRet0JZSr00]Unlock Sony Xperia V[/url]

Anonymous said...

How to unlock your mobile for free
[url=http://www.youtube.com/watch?v=uoFgtDEs-PM]Unlock Nokia 6220 Classic[/url]
[url=http://www.youtube.com/watch?v=6XEO63rZynM]Unlock Nokia 6120[/url]
[url=http://www.youtube.com/watch?v=HvulYWEHJB4]Unlock Nokia 3555[/url]

Anonymous said...

A lot of quality articles i see on your www.blogger.com
[url=http://www.youtube.com/watch?v=2lFx0PVIOZM] Unlock Nokia 6600 Fold[/url]
[url=http://www.youtube.com/watch?v=VrzEBsSUua4] Unlock Nokia 8600 Luna[/url]
[url=http://www.youtube.com/watch?v=G_Kjh-i67qk] Unlock Nokia C6 00[/url]

Anonymous said...

buy a pqOAybJe [URL=http://www.louis--vuitton--online--shop.org/]boutique louis vuitton[/URL] for promotion code lvjkROWX [URL=http://www.louis--vuitton--online--shop.org/ ] http://www.louis--vuitton--online--shop.org/ [/URL]

Anonymous said...

best for you YZUomxrq [URL=http://www.replica-handbags2013.com/]replica handbag[/URL] with low price QYvMdsLw [URL=http://www.replica-handbags2013.com/ ] http://www.replica-handbags2013.com/ [/URL]

Anonymous said...

you definitely love TLQDyuPH [URL=http://www.burberry-outlet2013.com/]burberry coat[/URL] , for special offer yJwHoDsW [URL=http://www.burberry-outlet2013.com/ ] http://www.burberry-outlet2013.com/ [/URL]

Anonymous said...

top [url=http://www.c-online-casino.co.uk/]uk bonus casino[/url] check the latest [url=http://www.realcazinoz.com/]online casinos[/url] manumitted no deposit hand-out at the chief [url=http://www.baywatchcasino.com/]redeem casino
[/url].

Anonymous said...

cheap nike air max kbhavjoq cheap nike free run qrwvwoju cheap nike shoes mtrpfpgw nike air max vzbktgvm nike free run vbytzpyo nike shoes online zynyvoyr

Anonymous said...

burberry sale zlmjnacf burberry outlet byttjsnl burberry bags fvqzptzt burberry handbags mgjusppa

Anonymous said...

cheap nike shoes khqbdvpn nike factory qivrzoxz nike online store opmravcc nike outlet online udeunblh nike outlet store nkmoffvv nike outlet mcvhijwb nike running shoes xmqvhlhw nike shoes hzknctjs nike store xyspgdfp

Anonymous said...

ghd hair straightener romcotqa ghd australia kvwfdvxi ghd hair straighteners jlftmzxk

Anonymous said...

Τhаnk you a lοt for sharing this ωith аll
of us you actually realіze ωhаt уou're talking about! Bookmarked. Please also seek advice from my web site =). We could have a hyperlink trade agreement between us

Feel free to surf to my homepage - Chemietoilette

Anonymous said...

Ηello there! This post couldn't be written any better! Reading this post reminds me of my previous room mate! He always kept talking about this. I will forward this post to him. Pretty sure he will have a good read. Thanks for sharing!

Feel free to visit my blog ... augenoperation

Anonymous said...

Dip remaining 4 tοгtillas into ѕauсе
and arrange greater thаn next laуer.
You mаy posѕibly be equippеd to attaіn desirable гespect and stаtus with аssіst of your strength and сourage.
"In culture, I necessarily mean 50 % the time a number of families have by no means been on your own, really on your own, without having some form of distraction.

my page ... identi.ca

Anonymous said...

The furnishings of a ring bugger up the whistle on buy solidly depends upon the compass and strain of jewel it has to do. There are rare sizes of machines of the unvaried congenial for the service perquisites of machining different sizes of castings and forgings, also there are contrastive kinds of machines for the emoluments of doing the require in any case species of undertaking in exceptional grades of refinement. An fiscal cogitation is to gain as only one machines as presentable to do as discerning a racket of work as realizable, and this is most becoming practised olden times choosing high-grade machines which are not contrariwise adapted to assortment of in the works,
http://mediocris.mazowsze.pl/firmy/regaly,magazynowe,s,2397/
http://www.2uh.pl/tag/agencja-reklamowa-brzeg/
http://sturodmor.szczecin.pl/biznes/wchodzac-do-sklepu-najprawdopodobniej-nawet-duza/
http://atiao.org.pl/?p=6171
http://seo-wielblad.eu/firmy/fer,lady,sklepowe,s,4783/

http://rockyou.pl/popularne-przy-samochodach.html
http://fer.pl/index.php?page=rapido-2011-1-05-m
http://chreestkim.warszawa.pl/mieszkanie-idealne/877/
http://www.2uh.pl/tag/bizuteria-recznie-robiona/
http://fer.pl/index.php?page=pk-53

Anonymous said...

Hi there it's me, I am also visiting this site on a regular basis, this site is in fact good and the visitors are actually sharing good thoughts.

Also visit my weblog - Jukeboxalive.Com

Anonymous said...

Highly energetic blog, I liked that a lot. Will there be a part
2?

My page: http://www.toryburchoutletshopx.com

Anonymous said...

After the fall of the Sun,[url=http://www.4dmv.com/cl.php]christian louboutin shoes[/url], the mouth just to chicken front. Sun heart full bite of chicken,[url=http://www.4dmv.com/inc/burberry.php]burberry bags[/url], successfully transferred the hearts of the idea of ​​God,[url=http://www.acelg.org.au/top.php]nike shoes[/url], instead the heart of the magic power of suppressed escaped. However,[url=http://www.4dmv.com/toms.php]cheap toms shoes[/url], this is also thanks to the Sun did not see the true capacity of only chicken, otherwise know this is a dusty chicken, Sun will not bite. He temperature a voice of Sun, lay a bad idea, [url=http://www.4dmv.com/lv.php]cheap louis vuitton handbags[/url] but also to find someone to try this baby and asked Sun: World slavery, the word What does it mean?

Anonymous said...

I am actually grateful to the owner of this web
site who has shared this wonderful paragraph at at this
place.

Here is my web blog; ミュウミュウ財布

Anonymous said...

I am curious to find out what blog platform you're using? I'm experiencing
some small security issues with my latest site and I'd like to find something more safe. Do you have any solutions?

Feel free to visit my web page クロエバッグ

Anonymous said...

Hello, every time i used to check blog posts here early in the morning, for
the reason that i like to gain knowledge of more and more.


Here is my homepage ... chloe バッグ

Anonymous said...

Oh my goodness! Incredible article dude! Thank you so much,
However I am encountering problems with your RSS.
I don't understand why I cannot join it. Is there anybody else getting the same RSS issues? Anyone who knows the solution can you kindly respond? Thanks!!

Also visit my blog post :: トリーバーチ靴

Anonymous said...

Hi everyone, it's my first visit at this site, and article is in fact fruitful in support of me, keep up posting these content.

Feel free to surf to my blog post; monsterbeatsoutlet-sales.com

Anonymous said...

Hurrah, that's what I was seeking for, what a data! existing here at this web site, thanks admin of this site.

Take a look at my web site - モンスター ヘッドホン

Anonymous said...

We are a group of volunteers and opening a new scheme in our community.
Your website provided us with valuable information to work on.
You have done an impressive job and our whole community will be grateful to you.



Here is my web site :: ルブタン

Anonymous said...

After checking out a few of the blog articles on
your blog, I truly like your way of writing a blog.
I added it to my bookmark webpage list and will be checking back in the near future.
Please visit my website as well and let me know your opinion.


My weblog ... クリスチャンルブタンシューズ

Anonymous said...

You've made some really good points there. I checked on the net for additional information about the issue and found most people will go along with your views on this website.

Also visit my site: ジョーダン 靴

Anonymous said...

Hello friends, pleasant piece of writing and good arguments commented here, I am in fact enjoying by these.


My web site; クリスチャンルブタン

Anonymous said...

I love what you guys are usually up too. This sort of clever work and reporting!
Keep up the terrific works guys I've incorporated you guys to my blogroll.

my page; Heidelberg Kindergeburtstag

Anonymous said...

Have you ever considered about adding a little bit more
than just your articles? I mean, what you say is important and everything.
Nevertheless think about if you added some great visuals or videos to
give your posts more, "pop"! Your content is excellent but with pics and video clips, this site could certainly be one of
the best in its niche. Great blog!

Here is my blog post - モンスタービーツ

Anonymous said...

I think the admin of this web page is really working hard for his site, since here
every material is quality based information.

Here is my webpage - クリスチャンルブタン

Anonymous said...

First of all I want to say fantastic blog! I had a quick question which I'd like to ask if you do not mind. I was interested to find out how you center yourself and clear your head prior to writing. I've had difficulty clearing my mind in
getting my ideas out. I do enjoy writing however it just seems like the first 10 to 15 minutes are
generally wasted just trying to figure out how to begin. Any suggestions or tips?

Thanks!

Review my website :: ルブタン

Anonymous said...

Thanks for sharing your thoughts about エアジョーダン.

Regards

Anonymous said...

What's Going down i'm new to this, I stumbled upon this I have discovered It
absolutely useful and it has helped me out loads.

I'm hoping to contribute & assist other users like its helped me. Good job.

My homepage: ジョーダン

Anonymous said...

I really like your blog.. very nice colors & theme.
Did you design this website yourself or did you hire someone to
do it for you? Plz answer back as I'm looking to construct my own blog and would like to know where u got this from. thanks a lot

Here is my page; tao of badass

Anonymous said...

Great post. I was checking continuously this
weblog and I'm inspired! Extremely useful information specially the remaining part :) I care for such information much. I used to be looking for this particular information for a very long time. Thank you and best of luck.

Look into my blog post monster beats

Anonymous said...

Hi, i think that i saw you visited my website thus i came to “return the favor”.
I'm attempting to find things to enhance my website!I suppose its ok to use some of your ideas!!

Feel free to visit my web page クリスチャンルブタン

Anonymous said...

It is appropriate time to make some plans for the longer term and it's time to be happy. I have read this put up and if I may just I desire to counsel you some interesting issues or tips. Perhaps you can write next articles relating to this article. I desire to learn more things about it!

Stop by my webpage ルブタン

Anonymous said...

Touche. Outstanding arguments. Keep up the great effort.


my web blog; モンスター イヤホン

Anonymous said...

Your style is really unique compared to other people
I have read stuff from. I appreciate you for posting when you've got the opportunity, Guess I'll just bookmark this blog.


Look at my web-site ... トリーバーチ店舗

Anonymous said...

I'm not sure why but this site is loading very slow for me. Is anyone else having this issue or is it a issue on my end? I'll check back later and see if the problem still exists.


my blog post :: monster ヘッドホン

Anonymous said...

Hmm is anyone else experiencing problems with the images on this blog loading?
I'm trying to determine if its a problem on my end or if it's the blog.
Any feed-back would be greatly appreciated.

Also visit my website - トリーバーチ アウトレット

Anonymous said...

This is very attention-grabbing, You're an overly skilled blogger. I've joined your feed and sit up
for looking for extra of your magnificent post. Additionally, I've shared your web site in my social networks

Visit my homepage ジョーダン