1

I have a global integer pointer array, which is created this way

int * array;
array = (int *) malloc(size * sizeof(int));

I also have a sorting algorithm, which is supposed to sort 4 first numbers of the array which size is larger than 4 (16 in this case). sizeOfArray is defined as 4 in this case:

int temp,i,j;
for(i=0;i<sizeOfArray;i++){
    for(j=i;j<sizeOfArray;j++){
        if(array[i] > array[j]){
            temp=array[i];
            array[i]=array[j];
            array[j]=temp;
        }
    }
}

Output is really weird for some reason:

Unsorted: 7,6,9,3
Sorted:  3,6,5,1

The weirdest part is if I change algorithm to sort numbers in a descending order, it seems to work:

if(array[i] < array[j])

Unsorted: 10,0,1,8
Sorted: 10,8,1,0

What's causing this? I'm completely lost.

6
  • 1
    sizeof does not do what you intend, you need size as a loop condition. Commented Jan 28, 2014 at 0:46
  • does size and sizeOfArray have same values? Commented Jan 28, 2014 at 0:47
  • 2
    @kajacx No. size is 16, sizeOfArray is 4. My goal is to sort 4 first values of the array with 16 values. I'll update the question Commented Jan 28, 2014 at 0:49
  • 4
    Can you post compilable code that replicates the problem? Commented Jan 28, 2014 at 0:51
  • Put a printf() in the code and you'll see. Commented Jan 28, 2014 at 0:54

3 Answers 3

2

Here is your code wrapped to make an MCVE How to create a Minimal, Complete, Valid Example?:

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

static void print(int n, int a[n])
{
    for (int i = 0; i < n; i++)
        printf("%2d", a[i]);
    putchar('\n');
}

int main(void)
{
    int size = 16;
    int *array = (int *) malloc(size * sizeof(int));

    array[0] = 7;
    array[1] = 6;
    array[2] = 9;
    array[3] = 3;
    int sizeOfArray = 4;

    printf("Before:");
    print(sizeOfArray, array);

    int temp, i, j;
    for (i = 0; i < sizeOfArray; i++)
    {
        for (j = i; j < sizeOfArray; j++)
        {
            if (array[i] > array[j])
            {
                temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }

    printf("After: ");
    print(sizeOfArray, array);

    return 0;
}

The output from this program is:

Before: 7 6 9 3
After:  3 6 7 9

Since this is not the same as the output you get, there must be a difference — a crucial difference. Since you don't show the code that initializes the array, nor show the code that demonstrates that the first 4 elements have the unsorted values, nor show the code that demonstrates the sorted values are wonky, it is not possible to say for certain what is wrong — but the problem is not in the code you show.

I've not fixed the code to check that the memory allocation succeeds; nor have I modified the code to release the allocated space. Both should be done.

The code does use C99 features; it is trivial to revise it not to do so:

static void print(int n, int *a)
{
    int i;
    for (i = 0; i < n; i++)

and move the definition of sizeOfArray before the assignments.

Sign up to request clarification or add additional context in comments.

Comments

0

I am sure this will work for your sorting array...in 2nd iteration sizeofarray-1 will work for loop j...

int temp,i,j;
for(i=0;i<sizeOfArray;i++)
{
    for(j=0;j<sizeOfArray-1;j++)
    {
        if(array[i] > array[j])
        {
            temp=array[i];
            array[i]=array[j];
            array[j]=temp;
        }
    }
}

Comments

0

Let's do some iterations of your code with the values you provide : 7,6,9,3. Also, let's assume that sizeOfArray = 4. For i = j, your condition will never be executed, because array[i] = array[j].

For i = 0 and j = 1 => 7 > 6 => array = {6, 7, 9, 3} For i = 0 and j = 2 => 6 < 9 => array = {6, 7, 9, 3} For i = 0 and j = 3 => 6 > 3 => array = {3, 7, 9, 6}

For i = 1 and j = 2 => 7 < 9 => array = {3, 7, 9, 6} For i = 1 and j = 3 => 7 > 6 => array = {3, 6, 9, 7}

For i = 2 and j = 3 => 9 > 7 => array = {3, 6, 7, 9}

Thus, I obtained the four first elements of your array sorted correctly (which contains size elements and I assume that size = 16).

If you're not sure about the value of sizeOfArray or size, I suggest you to print them and to check if it's really the value you want.

Hope this helps you.

1 Comment

Actually, this is not bubble sort.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.