heaps using the same array our initial code fragment showed unordered data in an array this data was then inserted into heapand finally removed from the heap and written back to the array in sorted order in this procedure two size- arrays are requiredthe initial array and the array used by the heap in factthe same array can be used both for the heap and for the initial array this cuts in half the amount of memory needed for heapsortno memory beyond the initial array is necessary we've already seen how trickledown(can be applied to half the elements of an array to transform them into heap we transform the unordered array data into heap in placeonly one array is necessary for this task thusthe first step in heapsort requires only one array howeverthe situation becomes more complicated when we apply remove(repeatedly to the heap where are we going to put the items that are removedeach time an item is removed from the heapan element at the end of the heap array becomes emptythe heap shrinks by one we can put the recently removed item in this newly freed cell as more items are removedthe heap array becomes smaller and smallerwhile the array of ordered data becomes larger and larger thuswith little planningit' possible for the ordered array and the heap array to share the same space this is shown in figure heap ce sorted array sorted array sorted array sorted array figure dual-purpose array sorted array