sorting - Sort function, helpers.c, only listing a few numbers -


my sort function seems list 3 numbers out of thousand repeatedly. not sure why. ideas?

my sort array begins here. created loop iterates through n numbers. compare values , create swap algorithm. assume swap algorithm flawed.

/**  * sorts array of n values.  */ void sort(int values[], int n) {             //todo: implement o(n^2) sorting algorithm     (int = 0; < n - 1; i++)     {         if (values[i] < values[i + 1])         {                    int holder = values[i];           values[i] = holder;           values[i + 1] = values[i];           values[i] = holder;           printf("%i\n", values[i]);                         }            }            return;         } 

there logic error in following lines:

    int holder = values[i];     values[i] = holder;     values[i + 1] = values[i];     values[i] = holder; 

i assume want swap value of values[i] , values[i+1].

you not extracting value of values[i+1] anywhere in code assign values[i].

you need use:

    int holder = values[i];     values[i] = values[i + 1];     values[i + 1] = holder; 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -