Two methods are available for full sorting:
Method 1: recursion is used to consider an arrangement as a column starting with a number + another sub-arrangement, for example, the full arrangement of the array list [N, list [I] indicates an arrangement starting with list [I, therefore, the number of all arrays is list [0] + list [1] + list [2] ...... list [n-1]. The arrangement of elements headed by list [I] can be viewed as list [I], with the addition of n-1 elements. Each time list [I] is used as the header, You can exchange elements between list [I] and list [0], and then sort the self-sequence list [1] ---- list [n-1, it can be recursive.
/* * recursion */void full_rank_recurisive(int *list,int n,int num){if(num == n){print_list(list,list+n);putchar('\n');return;}int i ;for(i = num;i<n;i++){swap(&list[i],&list[num]);full_rank_recurisive(list,n,num+1);swap(&list[i],&list[num]);}}
Method 2: the recursive method is not used. First, the array is regarded as an increment, which is a habit of hand-written arrangement. The first element is found from the right each time, this element is smaller than the element on his right. The elements on his right are monotonically decreasing, and then re-scan from the right side to find the first element larger than him and exchange the element value, then arrange the elements on the right from small to large, so that the loop knows that the element cannot be found.
void full_rank(int *p_start,int *p_end)//inorder from little to big{print_list(p_start,p_end);putchar('\n');while(1){int *ptr = p_end - 2;for(;ptr !=p_start - 1; ptr--){if(*ptr < *(ptr+1))break;}if(ptr == p_start - 1)break;int *p2 = p_end - 1;for(;p2 != ptr;p2--){if(*p2 > *ptr)break;}swap(ptr,p2);qsort(ptr+1,p_end - ptr-1,sizeof(p2[0]),cmp);print_list(p_start,p_end);putchar('\n');}}
There are two options for combination: recursive or exhaustive. Each number is either selected or not.
void DFS(int *src,int *dest,int n,int m, int k,int index){if(k == m || index == n){print_list(dest,dest+m);putchar('\n');return;}if(n - index == m - k){for(;index<n;)dest[k++] = src[index++];print_list(dest,dest+m);putchar('\n');}else{dest[k] = src[index];DFS(src,dest,n,m,k+1,index+1);DFS(src,dest,n,m,k,index+1);}}