Prolog program Write programs to generate the next higher permutation of a list
ID: 3798238 • Letter: P
Question
Prolog program
Write programs to generate the next higher permutation of a list of positive integers, in two ways: By applying the following definition of the problem: Permutation LI is the next, higher permutation of L if L1 is a higher permutation of L and LI is not far higher than L. L1 is a higher permutation of L if it is a permutation of L and is higher than L. LI is higher than L if there is a leftmost element of LI that is greater than the leftmost element of L. (below, L1 higher than L in both pairs). (e.g. L = [4, 2, 5, 3]; L1 = [5, 2, 4, 3] L = [5, 2, 3, 4];L1 = [5, 2, 4, 3]. L1 is far higher than L if there is a higher permutation L2 of L such that LI is higher than L2. By implementing the following algorithm: Scan L from the right end. Find the rightmost number (R) that is less than it's right neighbour (R.n) (e.g. L = [1, 2, 3, 5, 4, 2] R = 3 Scan L from the right again finding the rightmost number X > R (X = 4. Swap X and R ([1, 2, 4, 5, 3, 2], note numbers after 4 in decreasing order) and then reverse the digits on the right of X ([1, 2, 4, 2, 3, 5].Explanation / Answer
Anser of B Part by C Programing
main()
(
Int l,r,x
Assuming that you entr the arry
for(i=0;i<n;i++)
{
for (j=i+1;j<n;j++)
{ if( a[i]<a[j])
{
t=a[j+1]
a[i]=a[j+1]
a[j+1]=t;
}
p=i;
for(k=p+2 ;k<n ;k++)
{
for(m=k+1;m<n;m++)
{
if(a[k]<a[m])
{
t1=a[k];
a[k]=a[m];
a[m]=t;
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.