notes110 - CS 251 notes for week of 2-11 2-11 Heap Sort of...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 251 notes for week of 2-11 2-11 Heap Sort of an Array use the same array to store the sequence to sort and the heap initially all the array is used to store the unsorted sequence (the heap is empty) heap || 3 | 7 | 5 | 6 | 1 | sequence heap | 3 || 7 | 5 | 6 | 1 | sequence heap | 7 | 3 || 5 | 6 | 1 | sequence heap | 7 | 3 | 5 || 6 | 1 | sequence heap | 7 | 6 | 5 | 3 || 1 | sequence heap | 7 | 6 | 5 | 3 | 1 || sequence 1 st phase – construct a heap from left to right o the heap needs to be an inverted heap o in phase 2 the values are from highest to lowest the complexity of phase 1 is O(nlogn) – the first n is the number of elements and the second is the height of the tree Phase 2 o from right to left remove the maximum element in the heap and place it at the end of the heap o swap the maximum element and the last element in the heap Heap implementation In an array in C, C++, and Java the index starts at 0, thus we need to subtract one from the position to get the index void heapSort (int a[], int n) { for (int h = 1; h <= n; h++ ) { int child = h;
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern