Priority queue (storage structure array)--java implementation

Source: Internet
Author: User

1 /*Priority Queue--an improvement to the queue2 * Priority is given to the data being stored--small priority--in the team header3 * Implementation of priority queue4 array: Suitable for small data volumes (not implemented with Rear+front)5 * Priority Queue header in Items-1, queue tail at 0 is fixed6 heap: Suitable for large volumes of data7 * Efficiency of priority queue: Insert O (N) Remove O (1)8 * Application of Priority queue: Operating system thread scheduling algorithm9  * */Ten  Public classMypriorityqueue { One     Private intmaxSize; A     Private Long[] arr;//guaranteed to be orderly when inserted -     Private intitems; -      the      PublicMypriorityqueue (ints) { -MaxSize =s; -arr =New Long[maxSize]; -Items = 0; +     } -      +      Public voidInsertLongkey) { A         intJ; at         if(items = = 0) {//join directly to empty -arr[items++] =key; -         } -         Else{//do not empty the small element on the top--Queue header -              for(j = items-1;j >= 0;j--){ -                 if(Key >Arr[j]) { inARR[J+1] =Arr[j]; -                 } to                 Else{ +                      Break; -                 } the             } *ARR[J+1] =key; $items++;Panax Notoginseng         } -     } the      +      Public LongRemove () { A         returnarr[--items]; the     } +      -      Public BooleanIsEmpty () { $         returnItems = = 0; $     } -      -      Public BooleanIsfull () { the         returnItems = =maxSize; -     }Wuyi      the      Public Longgetpeekmin () { -         returnArr[items-1]; Wu     } -      About      Public voidDiaplaypq () { $          for(inti = Items-1;i >= 0;i--){ -System.out.print (Arr[i] + ""); -         } - System.out.println (); A     } +}

Priority queue (storage structure array)--java implementation

Related Article

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.