I got a problem. I need to sort an array in different ways. The problem is that after I sort it the first time my original array stays sorted. I tried copying the original array to another one but it remains sorted. My code is the following:
void printArray(int ** array, int n){
int i;
for(i = 0; i < n; i++){
printf(" %d ", (*array)[i]);
}
}
int main(){
int *array, n, number, i, j;
printf("\nIntroduce the size of the array:");
scanf("%d", &n);
array = (int*)malloc(n * sizeof(int));
for(i = 0; i < n; i++){
number = rand() % 1000 + 1;
array[i] = number;
}
printf("\nUnsorted array:");
printArray(&array, n);
//bubble sort
int *array2, aux;
array2 = array;
for (i = 0; i < n-1; i++){
for (j = 0; j < n-1; j++){
if(array2[j] > array2[j+1]){
aux = array2[j];
array2[j] = array2[j+1];
array2[j+1] = aux;
}
}
}
printf("\nSorted array:");
printArray(&array2, n);
//The problem is in here, if I print the original array, it's already sorted
printf("\nUnsorted original array:");
printArray(&array, n);
}