/ Published in: C++
Implementacija algoritma sortiranja pomoću hrpe iz kolegija Strukture podataka.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
void InsertH(int x, array_tree *T){ int counter = 2; if(T->array[9999].used){ cout << "Hrpa je puna! Pokrenite program iznova i pokusajte ponovno." << endl; return; } while(T->array[counter].used) counter++; T->array[counter].used = 1; while(counter > 1 && x < T->array[counter / 2].label){ T->array[counter].label = T->array[counter / 2].label; counter /= 2; } T->array[counter].label = x; } void SortH(array_tree *T){ int counter = 1; cout << T->array[counter].label << " "; if(T->array[counter+1].used == 0) return; while(T->array[counter + 1].used) counter++; T->array[1].label = T->array[counter].label; T->array[counter].label = -1; T->array[counter].used = 0; counter = 1; int b = 2; while(T->array[b].used && ((T->array[counter].label > T->array[b].label) || (T->array[b+1].used && (T->array[counter].label > T->array[b+1].label)))){ if(T->array[b].used && T->array[b+1].used) if(T->array[b].label > T->array[b+1].label) b++; int tmp = T->array[counter].label; T->array[counter].label = T->array[b].label; T->array[b].label = tmp; counter = b; b *= 2; } SortH(T); }