0

I have one array and I need to sort numbers in that array in groups. For example if I have 9 numbers and my P is 3 I need to sort three numbers in descending order then another 3 and another 3.

Example:

123 124 125 222 223 224 333 334 335

M is number of groups and P is amount of numbers in group. if M is 3 and P is also 3 it needs to give answer like

125 124 123 224 223 222 335 334 333

This is what I came up with but it doesn't work.

void sortgroup(int M, int P, int A[i])
{
      int c, d, swap, z = 0;
      for (c = z ; c < ( P*M - 1 + z ); c++)
      {
        for (d = z ; d < P + z - 1; d++)
        {
          if (A[d] < A[d+1])
          {
            swap       = A[d];
            A[d]   = A[d+1];
            A[d+1] = swap;
          }
        }
      z = P + z;
      }
}
Quto
  • 109
  • 7
  • Please describe the behaviour of your program in more detail than "doesn't work" - give the input, expected output and actual output. And please indicate what debugging you have done and what you have found. – kaylum Dec 05 '16 at 22:24
  • 2
    Create a pointer to each "group". Sort each group with a generic sort function. – Michael Dorgan Dec 05 '16 at 22:24
  • 3
    Oh and, your variable names are absolutely horrid. I see this and don't want anything to do with trying to understand what you are doing. Comments will help a lot as well. – Michael Dorgan Dec 05 '16 at 22:26
  • Actually I am beginner and I am trying to solve this, if I were a professional programmer I wouldn't come here for help. If you are willing to help you can ask question and I will surely try to give the best answer I can. Thanks. – Quto Dec 05 '16 at 22:30
  • How can I seperate them in groups when I must have one dimensional array? @MichaelDorgan – Quto Dec 05 '16 at 22:34
  • @kaylum so basically its a program where user inputs how many groups he wants to have and how many members in each group. Based on that he enters different numbers (groups * members) Then I am supposed to sort those groups in descending order and output it seperately for each group. – Quto Dec 05 '16 at 22:35

1 Answers1

5

Your function sortgroup tries to do too much at once - and you have a LOT of local variables, all cryptically named.

First, break the problem down: break the input array into the M chunks/groups of length P, then handle sorting each chunk separately, this will keep your code cleaner.

Abstractly:

void sortGroups(int* array, size_t arrayLength, size_t groupLength, size_t groupCount) {

    // get each group:
    for( size_t i = 0; i < arrayLength; i += groupLength ) {

        size_t start = i;
        size_t end   = min( i + groupLength, arrayLength );

        sortSingleGroup( array, start, end );
    }
}

void sortSingleGroup(int* array, size_t start, size_t end) {

    // sort the numbers in the range array[start] to array[end-1]
    // there are plenty of sorting libraries/functions you can download from the interwebs to do this
}

Note how I use non-cryptic variable names, and the sub-problem of sorting a group is removed from the initial-problem of splitting the array into groups.

Also, it would be an idea to add input validation:

void sortGroups(int* array, size_t arrayLength, size_t groupLength, size_t groupCount) {
    assert( array != NULL ); // prevent segfault/access-violation
    assert( groupLength > 0 ); // prevent infinite-loop
    if( arrayLength == 0 ) return; // optimization
    if( groupCount == 0 ) return; // optimization

    ...

void sortSingleGroup(int* array, size_t start, size_t end) {
    assert( array != NULL ); // prevent segfault/access-violation
    assert( start < end ); // sanity-check

    ...
Dai
  • 141,631
  • 28
  • 261
  • 374
  • Especially given that the OP is inexperienced, it's worth noting that assertions are inappropriate for validating external inputs, not least because they might not be tested at all. Assertions are for testing aspects of program state that the programmer relies on and thinks cannot fail to be true. – John Bollinger Dec 05 '16 at 22:44
  • @JohnBollinger as this is C, I suppose they could simply return error codes. I wish C had scoped-enums, though. – Dai Dec 05 '16 at 22:48
  • min( i + groupLength, arrayLength ); this gives me error warning: implicit declaration of function 'min' [-Wimplicit-function-declaration] – Quto Dec 05 '16 at 22:53
  • @Quto you need to define `min`, it isn't part of C's standard library ( http://stackoverflow.com/questions/3437404/min-and-max-in-c ) – Dai Dec 05 '16 at 22:57
  • @Dai Can you please check this is the last function that has problem I think. It only sorts each group once like changes first two numbers if it matches and then goes to other group. – Quto Dec 05 '16 at 23:18
  • @Quto What sorting algorithm is this meant to be? Why not use a pre-existing quicksort implementation? – Dai Dec 05 '16 at 23:30
  • @Dai this is supposed to be bubble sorting algorithm. I am not using pre existing quicksort because I couldn't figure out how to use it. :) – Quto Dec 05 '16 at 23:44