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.
Ex:
Input
[7, 1, 2, 3, 4, 5, 6]
swapTime = 2
Output
[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]);
table[j]--;
table[j+1]--;
}
}