largest number with each digit only swap at-most K times and only adjacent swap

I had a test, and there was a problem I still can't solve.
Given an array of numbers, with EACH ELEMENT have at-most K swap allowed, and only adjacent swap, find the largest lexicographical order.

[7, 1, 2, 3, 4, 5, 6]
swapTime = 2

[7, 3, 4, 1, 2, 6, 5]

At first I thought it was a modified BubbleSort, but it was not correct, any ideas? Here's the pseudo code:

void findMaxNum(int num[], int swapTime) {
    int table[n];
    for(i=0; i<n; ++i) 
        table[i] = swapTime;

    for(i=0; i<n-1; ++i)
        for(j=0; j<n-i-1; ++j)
            if(table[j]!=0 && num[j]<num[j+1]) {
                swap(num[j], num[j+1]);
                swap(table[j], table[j+1]);

Read more here:

Content Attribution

This content was originally published by Phineas at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: