-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermutations.c
73 lines (63 loc) · 1.71 KB
/
permutations.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Program to print all permutations of a string in sorted order.
#include <stdio.h>
#include<stdbool.h>
#include <stdlib.h>
#include <string.h>
//requirement for qsort
int compare(const void *a, const void * b)
{
return ( *(char *)a - *(char *)b );
}
void swap(char* a, char* b)
{
char t = *a;
*a = *b;
*b = t;
}
// This function finds the index of the smallest character
// which is greater than 'first' and is present in str[l..h]
int findCeil(char str[], char first, int l, int h)
{
int ceilIndex = l;
int i;
for ( i = l+1; i <= h; i++)
if (str[i] > first && str[i] < str[ceilIndex])
ceilIndex = i;
return ceilIndex;
}
//print the permutations in sorted order into the permutation file
void sortedPermutations(char str[])
{
FILE *per=fopen("permutation.txt","w");
int size = strlen(str);
//library function used to sort the string in increasing order
qsort(str, size, sizeof( str[0] ), compare);
bool isFinished = 0;
while (!isFinished)
{
static int x = 1;
fprintf(per,"%s\n", str);
int i;
for (i = size - 2; i >= 0; --i)
if (str[i] < str[i+1])
break;
// If there is no such chracter, all are sorted in decreasing order,
// means we just printed the last permutation and we are done.
if (i == -1)
isFinished = 1;
else
{
int ceilIndex = findCeil(str, str[i], i + 1, size - 1);
swap(&str[i], &str[ceilIndex]);
qsort(str + i + 1, size - i - 1, sizeof(str[0]), compare);
}
}
}
int main()
{
char str[45];
printf("Enter the string\n");
scanf("%s",str);
sortedPermutations( str );
return 0;
}