Δημοσιεύτηκε: 23 Μαρ 2012, 13:40
από stamatiou
Προσπάθησα να φτιάξω ένα πρόγραμμα που υλοποιεί τον insertion sort αλλά όταν δοκιμάζω να το κάνω με dynamic memory allocation μου βγάζει έξοδο ένα τεράστιο νούμερο :/
Κώδικας: Επιλογή όλων


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

int main(void) {
int i,j,key,size,*array;
do {
printf("Enter size: ");
scanf("%d",&size);
} while (size <= 0);
if(!(array = (int *) malloc(size * sizeof(int)))) {
printf("FAILED TO ALLOCATE MEMORY...\nEXITING...\n");
return 1;
}

for(i = 0; i < size; i++) {
scanf("%d",&array[i]);
}

for(j = 2; j < size; j++) {
i = j - 1;
key = array[j];
while(i >= 0 && array[i] > key) {
array[i + 1] = array[i];
i--;
}
array[i + 1] = key;
}
for(i = 0; i < 9; i++) {
printf("%d",array[i]);
}
putchar('\n');
return 0;
}