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.