Home > Back-end >  C function to sort part of an array not producing any output
C function to sort part of an array not producing any output

Time:06-02

I have an assignment to modify the selection sort to sort all of the values on the odd positions of an array in ascending order and to sort all of the values on the even positions in descending order. I am currently working on the oddSort function

void oddSort(int arrSize, int arr[]){
    int i;
    int lastOdd;
    int currentMin;
    
    lastOdd = findLastOdd(arrSize);
    
    for(i=0; i<lastOdd; i =2){
        if(i=0){
            currentMin = arr[i];
        }
        else if (arr[i] < currentMin){
            swap(&arr[i], &currentMin);
        }
    }
}

but, when I try to apply this function to an array and print the output for it the compiler returns nothing.

int main(){
    int arrayOne[10] = {246, 101, 223, 228, 1, 249, 99, 111, 191, 126}; 
    int i;
    oddSort(10, arrayOne);
    for(i=0; i<10; i  ){
        printf("%d ", arrayOne[i]);
    }
    return 0;
}

My pseudocode for the evenSort function is similar to the oddSort function so I will assume that that won't work either. I checked the swap and findLastOdd functions independently and they work, so I am sure that there is something wrong with the oddSort function itself. Any ideas as to what? edit: here are the rest of the user defined functions in my code

int findLastOdd(int someNumber){//to give the oddSort function a stopping point
    if(someNumber % 2 == 0){
        return someNumber - 1;
    }
    else{
        return someNumber;
    }
}


int findLastEven(int someNumber){//to give the evenSort function a starting point
    if(someNumber % 2 == 0){
        return someNumber;
    }
    else{
        return someNumber - 1;
    }
}

void swap (int* a, int* b){// swaps two array elements using pointers
    int temp;
    temp = *a;
    *b = *a;
    *b = temp;
}

CodePudding user response:

For starters there is no need to split the function that sorts the array into two separate functions.

Within the function oddSort in the if statement

if(i=0){

you are using the assignment = operator instead of the comparison operator ==. And moreover odd indices start from 1 not from 0.

And it is not enough to use only one loop to sort an array using the selection sort method.

Even the condition in your single for loop

for(i=0; i<lastOdd; i =2){

is incorrect because the value of the index lastOdd can be a valid index position of an element of the array.

And the function swap contains a typo. Instead of

int temp;
temp = *a;
*b = *a;
*b = temp;

there must be

int temp;
temp = *a;
*a = *b;
*b = temp;

Here is a demonstration program that shows how the sorting function can be written.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void OddEvenSort( int a[], size_t n )
{
    for ( size_t i = 0; i < n; i   )
    {
        size_t target = i;
        for ( size_t j = i;   j < n &&   j < n ;  )
        {
            if ( target % 2 == 0 )
            {
                if ( a[target] < a[j] ) target = j;
            }
            else
            {
                if ( a[j] < a[target] ) target = j;
            }
        }

        if ( target != i )
        {
            int tmp = a[i];
            a[i] = a[target];
            a[target] = tmp;
        }
    }
}

int main( void ) 
{
    enum { N = 20 };
    int a[N];

    srand( ( unsigned int )time( NULL ) );

    for ( size_t i = 0; i < N; i   )
    {
        a[i] = rand() % N;
    }

    for ( size_t i = 0; i < N; i   )
    {
        printf( "%d ", a[i] );
    }
    putchar( '\n' );

    OddEvenSort( a, N );

    for ( size_t i = 0; i < N; i   )
    {
        printf( "%d ", a[i] );
    }
    putchar( '\n' );
}

The program output might look like

11 11 14 4 11 17 18 4 12 6 16 8 15 10 9 7 13 9 4 15 
18 4 16 4 15 6 14 7 13 8 12 9 11 10 11 11 9 15 4 17

As you can see values in even positions are sorted in the descending order

18 16 15 14 13 12 11 11 9 4

and values in odd positions are sorted in the ascending order

4 4 6 7 8 9 10 11 15 17

CodePudding user response:

Notice that in your function oddSort you seemingly sorting the odd numbers of your array in o(logn) run time, but it's Not possible to sort o(n/2)=o(n) elements in o(logn). In fact the minimum run time for sorting is o(nlogn).

CodePudding user response:

int findLastOdd(int someNumber){//to give the oddSort function a stopping point
    if(someNumber % 2 == 0){
        return someNumber - 1;
    }
    else{
        return someNumber;
    }
}

void swap (int* a, int* b){// swaps two array elements using pointers
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

int findMinValue(const int* arr, int startIndex, int arrSize) {
    int min = startIndex;

    for (int i = startIndex; i < arrSize; i  = 2) {
        if (arr[i] < arr[min]) min = i;
    }

    return min;
}

void oddSort(int arrSize, int* arr){
    int i;
    int lastOdd;
    int currentMin;

    lastOdd = findLastOdd(arrSize);

    for(i=1; i<lastOdd; i =2){
        currentMin = findMinValue(arr, i, arrSize);
        swap(&arr[currentMin], &arr[i]);
    }
}

int main(){
    int arrayOne[10] = {246, 101, 223, 228, 1, 249, 99, 111, 191, 126};
    int i;
    oddSort(10, arrayOne);
    for(i=0; i<10; i  ){
        printf("%i: %d\n", i, arrayOne[i]);
    }
    return 0;
}

In the swap function, you mixed up the variables. Also you had a syntax error in the if statement in oddSort(). You assigned 0 to i instead of actually checking if i is equal to zero with ==.

In Selection Sort, you need to iterate the array and find the smallest value you can find. You return the index of that element and then you swap the current element with the smallest one. You forgot this step apparently, so I made the function for you findMinValue().

You can finish off the code yourself and make it prettier/get rid of some inefficiencies, but the code I posted above works (on my machine :^)).

  • Related