Implementation of heap sorting (chuangfa technology written test of lianfa Branch) and ring for judging one-way linked list

Source: Internet
Author: User
First run the Code:
# Include <iostream> # include <algorithm> using namespace STD; void heapadjust (INT data [], int I, int length) {int nchild; int ntemp; for (ntemp = data [I]; 2 * I + 1 <length; I = nchild) {nchild = 2 * I + 1; if (nchild <length-1 & Data [nchild + 1]> data [nchild]) // compare which child is larger than itself. If it is a right child, nchild ++; {nchild ++;} If (ntemp <data [nchild]) // if it is smaller than your largest child, exchange {data [I] = data [nchild]; data [nchild] = ntemp;} else // if it is bigger than the largest child, it will not exchange break ;}} void heapsort (int A [], int N) {for (INT I = n/2-1; I> = 0; I --) /* construct a [] into a large top heap */{heapadjust (A, I, n);} For (Int J = n-1; j> = 1; j --) {swap <int> (A [J], a [0]); // STL swapheapadjust (A, 0, j) ;}} int main () {int A [10] = {9, 3, 7, 5, 3, 4, 2, 0, 1, 6}; heapsort (A, 10); For (INT I = 0; I <10; I ++) cout <A [I] <""; cout <Endl; return 0 ;}

The above are mainly functional functions. You can get the main function done! I have passed the debugging!
The time complexity of heap sorting is O (nlogn). The worst case is the time complexity. The space complexity is O (1 ). However, heap sorting is unstable!
During the interview process, heap sorting is often used. For example, the following questions are typical topics of heap sorting:

1. Calculate the maximum 10 elements for pieces of data. At this time, we can use the small top heap! Why!

2. Calculate the minimum 10 elements for pieces of data. At this time, we can use the big top stack! Why!

I believe there will be many colleagues who will ask the two questions above. The answer is actually very simple. When looking for the biggest element, we will create a small top heap with 10 elements, the heap top element must be the smallest, and then compare the remaining element with the heap top element. If it is larger than the heap top, replace the element and adjust the heap, after adjustment, the heap top is still the smallest of the 10 elements, and the remaining elements are compared in sequence.


Difference between heap sorting and direct insert sorting


Directly select the sort, In order .. n] to select the record with the smallest keyword. The record must be compared n-1 times, and then in R [2 .. n] in the selection of the minimum keyword record, and need to do more than the N-2. In fact, many comparisons in the next N-2 comparison may have been done in the previous n-1 comparison, but since these comparison results were not retained in the previous sort, therefore, these comparison operations are repeated during the next sorting. Partial comparison results can be saved in a tree structure to reduce the number of comparisons.


If a one-way linked list contains loops, how can we find the first node in the circular part of this linked list?
// If no ring exists in the linked list, return falsebool loop (node * head) {bool flag = true; If (Head = NULL) {flag = false ;} node * One = head; node * Two = head-> next; If (two = NULL) {flag = false;} while (one! = Two) {If (one! = NULL) {one = one-> next;} If (two! = NULL) {two = two-> next;} If (two = NULL) {break;} Two = two-> next; if (one = NULL | two = NULL) {break ;}} if (one = NULL | two = NULL) {flag = false ;} return flag;}/* Thought: assume that the node is at the position X. Assume that the pointer with Step 1 and the pointer with Step 2 meet at the position X + Z, if the Loop Length is Y, then 2 (x + Z)-(x + z) = K * y, then when a pointer is removed from the beginning, when the other pointer starts to move backward from the encounter point, the two pointers will encounter at the beginning of the loop. */Node * findloopplace (node * head, unsigned int * place = NULL) {// find the location of the loop, place storage location if (! Loop (head) {return NULL;} node * One = head; node * Two = head-> next; unsigned int COUNT = 1; while (one! = Two) {one = one-> next; two = two-> next;} One = head; while (one! = Two) {If (count! = 1) {one = one-> next;} Two = two-> next; count ++;} * place = count; return one ;}
In addition, there are also small choice questions in terms of big-end and small-end, pointer and syntax.


Implementation of heap sorting (chuangfa technology written test of lianfa Branch) and ring for judging one-way linked list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.