There is a array of 100 blocks, here each block contains one color and this color comes from an array of characters a,b,c,…,z randomly.
Ex:
a b d e g i h f d a
Now you have to write a program, that will return sorted array with following conditions.
- Following swapping order should be follow.
Character maximum swap
a 1
b 2
c 3
… …
z 26
Output:
a a b d d e f h g i
I have used the following methods.
//return no of blocks
int getNoOfBlocks();
//return the alphabet at particular block
char getData(int);
//swap the value
void swapBlock(int, int);
void arrangeBlocks(void)
{
int size = getNoOfBlocks();
for(int i = 0 ; i < size; i++)
{
int min = getData(i)-97;
int pos = 0;
int ascii = 0;
for(int j = i + 1; j < size; j++)
{
ascii = getData(j)-97;
if(min > ascii)
{
min = ascii;
pos = j;
}
}
swapBlock(i, pos+1);
}
}
You are swapping the wrong elements, it should be:
Also your first loop is wrong, it should be: