I need results starting from the beginning with 123, as shown below. I want to print from the very beginning without sort()
.
1 2 3
1 2 4
1 2 5
.....
3 5 6
4 5 6
But, the results are shown below.
4 5 6
3 5 6
2 5 6
1 5 6
3 4 6
2 4 6
1 4 6
2 3 6
1 3 6
1 2 6
3 4 5
2 4 5
1 4 5
2 3 5
1 3 5
1 2 5
2 3 4
1 3 4
1 2 4
1 2 3
result = "";
var N = 6;
var M = 3;
var arr = new Array(M);
combi(N, M, arr, M);
alert(result);
function combi(n, r, arr, sz) {
var i = n + 1;
while (i-- > r) {
arr[r - 1] = i;
if (r > 1) {
combi(i - 1, r - 1, arr, sz);
} else {
var j = -1;
while (++j < sz) {
result += arr[j] + " ";
}
result += "\n";
}
}
}
source
share