You are given a set of 2n disks that alternate between fille
Solution
Algorithm:
begin sortFilledUnfilled(diskSequence)
for all disk of diskSequence
if diskSequence[i+1] == \'UnfilledDisk\' and diskSequence[i] == \'FilledDisk\'
swap(diskSequence[i], diskSequence[i+1])
end if
end for
return diskSequence
end sortFilledUnfilled
Code:
for (int i=0;i<n;i++)
{
for (int j=0;j<n-1-i;j++)
{
if (a[j+1] == \'UnfilledDisk\' && a[j] == \'FilledDisk\')
{
swap(a[j],a[j+1]); //swap function will be defined separately to swap the filled and unfilled disk
}
}
}
Complexity:
The Complexity of the above program/algorithm is O(n^2). The Algorithm runs 2 loops for parsing the disks as we need to check the adjacent disks. Whenever an unfilled disk is followed by a filled disk it will perform the swap else nothing will be done.
This work will be continued till the outer loop reaches the last disk. As there are two loops being used with each loop parsing the whole sequence of disk till the end, hence each loop will have the complexity of O(n).
Therefore, O(n) x O(n) = O(n^2)
