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.

Avaruusmuikku
  • 91
  • 1
  • 1
  • 8

3 Answers3

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.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
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.

Gabriel L.
  • 4,678
  • 5
  • 25
  • 34
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;
        }
    }
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user3241004
  • 24
  • 1
  • 5