{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

notes88 - HeapSort void heapSort(int a int n)cfw/a is the...

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

View Full Document Right Arrow Icon
Heap Sort -uses a heap to do the sorting -heap is implemented in the same array void heapSort(int a[], int n){ //a is the unsorted array of integers //n is the number of elements //PHASE 1: build heap, remove the elements from s & insert into heap for(int i=0; i<n; i++){ //insert i into heap //element i is now in the heap //we need to fix the heap from i to the root (upheap) int child = i; int parent = (child-1)/2; while(parent& gt;=0){ & nbsp; if(a[child] < a[parent]){ //inverted heap, do not change & nbsp; break; & nbsp; } & nbsp; //need to swap parent and child & nbsp; int temp = a[child]; & nbsp; a[child] = a[parent]; & nbsp; a[parent] = temp; & nbsp; //update parent and child & nbsp; child = parent; & nbsp; parent = (child-1)/2; } } //PHASE 2: remove max. element from heap & insert into s //fix heap (down heap), repeat for all elements for(int j = n-1; j >= 0; j--){ //swap max. and element in j int temp = a[0]; //always max. at zero a[0] = a[i]; a[j] = temp; //fix heap from the root to the bottom parent = 0; int left = 2*parent+1; int right = left+1; while(left < j){ //find max. child int child = left; if(right < j && a[child] < a[right]){ & nbsp; child = right; } //swap only if the child is larger than the parent if(a[parent] > a [child]){ //no more swapping needed break; } //we need to swap parent and child int temp = a[parent]; a[parent] = a[child]; a[child] = temp; parent = child; left = 2*parent+1;
Image of page 1

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

View Full Document Right Arrow Icon
right = left+1; } } } Phase 1 and Phase 2: O(nlogn) + O(nlogn) = O(nlogn) Hashing  -it maps a key to a record Example: SSN --> student record -How to implement the mapping:     mapping from a key to a record can be implemented with
Image of page 2
Image of page 3
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