tag:blogger.com,1999:blog-6314876008291942531.post7640798472226428995..comments2024-01-14T00:36:43.430-08:00Comments on Antonio Gulli's coding playground: Sequences in sorted orderUnknownnoreply@blogger.comBlogger2125tag:blogger.com,1999:blog-6314876008291942531.post-67479663900035886372012-08-31T09:08:58.682-07:002012-08-31T09:08:58.682-07:00Basically out of all the possible k-permutations y...Basically out of all the possible k-permutations you print just the sorted one. You can generate all the perms and skip printing the unsorted ones (but you'll generate n!(n-k)! strings and most of them will be not printed out) or you can just avoid to generate the unsorted ones and print the remaining.<br /><br />Here the code for this second solution:<br /><br /><br />public static void printSortedPerm(List list, String str, int k, int top) {<br /> <br /> if (k == 0) {<br /> System.out.println(str);<br /> }<br /> else {<br /> for(int i = 0; i < list.size(); i++) {<br /> if (list.get(i) < top) continue;<br /> Integer next = list.remove(i);<br /> printSortedPerm(list, str + next, k-1, next);<br /> list.add(i, next);<br /> }<br /> }<br /> }<br /><br /><br />// main<br />List list = new ArrayList();<br />list.add(1);<br />list.add(2);<br />list.add(3);<br />list.add(4);<br />printSortedPerm(sortedList, "", 3, -1);Claudio Corsihttps://www.blogger.com/profile/13435215356783747956noreply@blogger.comtag:blogger.com,1999:blog-6314876008291942531.post-46999230174804646512012-08-24T00:25:41.304-07:002012-08-24T00:25:41.304-07:00classical question to test how you play with recur...classical question to test how you play with recursioncodingplaygroundhttps://www.blogger.com/profile/08478993186814330588noreply@blogger.com