Syed Hussain
Syed Hussain

Reputation: 67

My sorting function gives unexpected output

I write a generalized C language program that given a sequence of numbers, sorts, even numbers in ascending order, odd numbers in descending order and places all even numbers in the initial part of an array then odd numbers.

Example: 2, 5, 1, 0, 4, 7, 9, 3, -2, 10, 20, 15
Expect: -2, 0, 2, 4, 10, 20, 15, 9, 7, 5, 3, 1

Six functions are required. The solution must be provided using only the mentioned functions.No global variables shall be declared. Use appropriate data types, return types and function arguments.

  1. Input() – takes total number of elements and values as input from the user. Stores the values in “input” array.
  2. SortEven() – sorts the even numbers in ascending order and stores them in an array named “even”
  3. SortOdd() – sorts the odd numbers in descending order and stores them in an array named “odd”
  4. Merge() – places all the even numbers in the initial part of array named “result” then odd numbers.
  5. Display() – displays the contents of “result” array.
  6. main() – calls the Input() module to begin the execution.

Program:

#include <stdio.h>

int main() {
    input();
}
int input() {
    int n;
    printf("Enter The Number Of Elements You Want To Enter : ");
    scanf("%d", &n);
    int a[n], i, ev = 0, od = 0;
    for (i = 0; i < n; i++) {
        printf("Enter Number : ");
        scanf("%d", &a[i]);
        if (a[i] % 2 == 0) {
            ev++;
        } else {
            od++;
        }
    }
    sorteven(a, ev, od, n);
}
int sorteven(int a[], int ev, int od, int n) {
    int i, j = 0, swap, even[ev];
    for (i = 0; i < n; i++) {
        if (a[i] % 2 == 0) {
            even[j] = a[i];
            j++;
        }
    }
    for (i = 0; i < ev - 1; i++) {
        for (j = 0; j < ev - i - 1; j++) {
            if (even[j] > even[j + 1]) {
                swap = even[j];
                even[j] = even[j + 1];
                even[j + 1] = swap;
            }
        }
    }
    sortodd(a, ev, od, n, even);
}
int sortodd(int a[], int ev, int od, int n, int even[]) {
    int i, k = 0, swap, odd[od], j;
    for (i = 0; i < n; i++) {
        if (a[i] % 2 != 0) {
            odd[k] = a[i];
            k++;
        }
    }
    for (i = 0; i < od - 1; i++) {
        for (j = 0; j < od - i - 1; j++) {
            if (odd[j] < odd[j + 1]) {
                swap = odd[j];
                odd[j] = odd[j + 1];
                odd[j + 1] = swap;
            }
        }
    }
    merge(a, ev, od, n, even, odd);
}
int merge(int a[], int ev, int od, int n, int even[], int odd[]) {
    int merge[n], i;
    for (i = 0; i < ev; i++) {
        merge[i] = even[i];
    }
    for (i = ev; i < n; i++) {
        merge[i] = odd[i];
    }
    display(merge, n);
}
int display(int merge[], int n) {
    int i;
    printf("OUTPUT : ");
    for (i = 0; i < n; i++) {
        printf(" %d ", merge[i]);
    }
}

Upvotes: 1

Views: 119

Answers (1)

J. Piquard
J. Piquard

Reputation: 1663

When looking in the source code, the problem is located in the merge() function. Both sorteven() and sortodd()are well implemented, but when merging both sub-arrays, the for(i=ev;i<n;i++) is not correct.

To add the odd[] array at the end of the even[] array, write:

int j;
// point to the first item of odd[]
for(i=ev,j=0;i<n;i++,j++)
{
    merge[i]=odd[j];
}

Instead of:

only odd[0] to odd[od-1] are allocated and defined.

for(i=ev;i<n;i++)
{
    merge[i]=odd[i];
}

Outputs of:

{ 2, 5, 1, 0, 4, 7, 9, 3, -2, 10, 20, 15 };

Are:

OUTPUT :  -2  0  2  4  10  20  15  9  7  5  3  1

Upvotes: 2

Related Questions