0

pls could anyone tell me where have i made the mistake?...as i have tried to find it myself but couldn't find it.

void sort(int values[], int n)
{

    int min=0,max=0;
    do{
        //to compare and change
        for(min=0,max=min+1;min<n;min++ ){
            if(values[min]>values[max]){
               int x=values[min]+values[max];
                values[min]=x-values[min];
                values[max]=x-values[max];
            }
            max++;
        }
    }
    while(values[min] > values[max]);
    return;
 }

1 Answer 1

2

My sincere recommendation is ... start again. You have two nested loops, each time the DO-WHILE loop begins, the min and max values are set to zero, as the end-of-cycle condition for while is

values[min] > values[max]

can occur at any time or never happen, so we can have an infinite loop, also you are changing the values of the array itself so that your function can not serve in any way. It is useful to use paper and pencil and see what happens with the values of the array and with the INDICES, it is important that you understand the value associated with an index can change, but not the index itself.

EDIT:

your idea of a DO WHILE loop combined with FOR is correct (not so your execution), we can use a control variable, which we can set to zero at the beginning of each WHILE iteration, then run a complete for loop, in order to perform the swaps of values needed in the array, if there is a change in the array we increase the counter, and we put as end condition of cycle while the counter is zero, when there are no more changes the counter is still zero and we leave the loop. .. I guess it is easier a small scheme of the code:

int count = 0;
do
{
    count = 0;
    for(......)
       {
            // if previous is greater than next swap
            if(.....)
            {
                // we make the necessary changes in the ARRAY
                count++;
            }
       }
}
while(count>0);
4
  • MARS thanks for replying... but I would like to ask...about what u said about do...while loop....my thoughts were that I want to start a for loop to check and compare the values at different index of the array and only if the value was greater than the next value...they swap....else...the for loop for swap will not run......pls tell me what do u think of this....instead of do while...I run a normal for loop to iterate over....n using if else....I check if the next value is greater than the preceeding value n then return...else if its smaller...just return.....
    – shaistha
    Commented Sep 8, 2017 at 18:06
  • you can read my edit
    – MARS
    Commented Sep 8, 2017 at 18:30
  • Thank u..I kinda get it now...will make the necessary changes
    – shaistha
    Commented Sep 8, 2017 at 19:02
  • thanks MARS i have made the changes and my bubble sort works!!
    – shaistha
    Commented Sep 9, 2017 at 9:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .