Max Bentata
Max Bentata

Reputation: 505

sort function not working properly (bubble sort) - c

I'm having some trouble with my sort function which also uses the swap function to work. I have been trying to find my mistake but cant find it. the rest of the code works perfectly, only the sort function doesn't work (it doesn't sort anything.)

I will leave the code here if anybody has any ideas of how to solve my problem please reply.

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

void sort(int *values, int n);
void swap(int arr[], int i);
void populate(int arr[]);
const int arraysize = 10;


int main()
{

    srand((long int) time(NULL));

    int ela[10] = {0};

    populate(ela); // populates with random values

    printf("before func\n");
    for(int i = 0; i < arraysize; i++)
    {
        printf("%i\n", ela[i]); 
    }


    sort(ela, arraysize); // this is the function that is not working

    printf("\n\nafter func\n");

    for(int i = 0; i < arraysize; i++)
    {
        printf("%i\n", ela[i]); 
    }

    return 0;
}





void sort(int *values, int n)
{
int count = 1;
    while(count != 0)
    {
        count = 0;
        for (int i = 0; i < n; i++) {

            if(values[i] > values[(i + 1)] && values[(i + 1)] != '\0')
            {
                swap(values, i);
                count++;
            }
            if (count == 0) break;
        }

    }

}

void swap(int arr[], int i)
{
    int save = arr[i];
    arr[i] = arr[i+1];
    arr[i + 1] = save;
    return;
}

void populate(int arr[])
{
    for(int i = 0; i < arraysize; i++)
    {
        arr[i] = (rand() % 15);
    }
}

Upvotes: 1

Views: 955

Answers (4)

Vlad from Moscow
Vlad from Moscow

Reputation: 311126

Your bubble sort function is wrong.

Here is a demonstrative program that shows how the function can be written

#include <stdio.h>

void swap( int a[], size_t i )
{
    int tmp = a[i];
    a[i] = a[i+1];
    a[i+1] = tmp;
}

void bubble_sort( int a[], size_t n )
{
    for ( _Bool sorted = 0; n-- != 0 && !sorted;  )
    {
        sorted = 1;
        for ( size_t i = 0; i < n; i++ )
        {
            if ( a[i+1] < a[i] )
            {
                sorted = 0;
                swap( a, i );
            }
        }
    }
}

int main(void) 
{
    int a[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
    const size_t N = sizeof( a ) / sizeof( *a );

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

    bubble_sort( a, N );

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

    return 0;
}

The output is

9 8 7 6 5 4 3 2 1 0 
0 1 2 3 4 5 6 7 8 9 

Upvotes: 0

Mr. Perfectionist
Mr. Perfectionist

Reputation: 2746

If you want to use sort() function, you have to add algorithm header file. But it will not work for c. If you add using namespace std; Then it will work. And then you have to call like that.

sort(ela, ela+arraysize);

You can edit your sort function like below. Above answers are also nice.

void Sort(int *values, int n)
{
    int cnt=0;
    while(1)
    {
        cnt++;
        if(cnt==n-1)
            break;
        for (int i = 0; i < n-cnt; i++)
        {

            if(values[i] > values[(i + 1)] && values[(i + 1)] != '\0')
            {
                swap(values, i);
            }
        }

    }

}

Upvotes: 0

Shawnic Hedgehog
Shawnic Hedgehog

Reputation: 2373

Here is an updated version of your sort() function:

void sort(int *values, int n)
{
int count = 1;
    while(count != 0)
    {
        count = 0;
        for (int i = 0; i < n; i++) {

            if(values[i] > values[(i + 1)] && values[(i + 1)] != '\0')
            {
                swap(values, i);
                count++;
            }
        }
    }
}

Your if(count==0) break; statement would exit your for loop even when you would check initial values of the array. So for instance [5 6 9 8] would exit because count was 0 when it checked the first two items.

Upvotes: 0

Srinath Mandava
Srinath Mandava

Reputation: 3462

void sort(int *values, int n)
{
int count = 1;
    while(count != 0)
    {
        count = 0;
        for (int i = 0; i < n-1; i++) {

            if(values[i] > values[(i + 1)] )
            {
                swap(values, i);
                count++;
            }
            //if (count == 0) break;

        }

    }

}

I doubt this would never be true values[(i + 1)] != '\0' as the values are integers..So running the for loop until the condition i<n-1 would work fine as you are swapping i+1 element with i.
And also if (count == 0) break; inside your for loop should be removed as the loop would break if there is some input like 2 3 2 1 and so wouldn't sort .

Working Ideone: http://ideone.com/k1LJau

Upvotes: 1

Related Questions