for (unsigned int i=heapsize/2-1; i>=0;-I.)

Source: Internet
Author: User

The representation of the unsigned int
Today, I encountered a bug when I wrote the heap sort.
  
 
  1. void builMaxHeap( int *arr,unsigned int heapSize){
  2. unsigned int i;
  3. for(i=heapSize/2-1; i>=0;--i){
  4. std::cout<<i<<std::endl;
  5. //maxHeap(arr,i,heapSize); 这里暂且注释掉
  6. }
  7. }
This is really the whole dead man, thought it was the wrong program, the output of the result is
Hehe, can only hehe, the reason is that we use the unsigned unsigned int type to represent I
 
   
  
  1. for(i=heapSize/2-1; i>=0;--i){
In other words, if I is a negative number, exit the loop, unfortunately Ah, for unsigned type I is never end to get negative AH!!!! A typical death





From for notes (Wiz)

= 0; -i), mamicode.com "style=" color: #ffffff "target=" _blank ">for (unsigned int i=heapsize/2-1; i>=0;-I.)

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.