Koj puas tuaj yeem tshawb nrhiav binary ib daim ntawv txuas?
Koj puas tuaj yeem tshawb nrhiav binary ib daim ntawv txuas?

Video: Koj puas tuaj yeem tshawb nrhiav binary ib daim ntawv txuas?

Video: Koj puas tuaj yeem tshawb nrhiav binary ib daim ntawv txuas?
Video: Lig Pob tsuas xyooj nkauj tawm tshiab 2022-2023 2024, Kaum ib hlis
Anonim

Yog lawm, Binary nrhiav yog ua tau rau ntawm linked lis os tus lis yog xaj thiab koj paub qhov suav ntawm cov ntsiab lus hauv lis . Tab sis thaum sorting lub lis , koj ua tau nkag mus rau ib lub ntsiab lus ntawm ib lub sij hawm los ntawm tus taw tes rau qhov node xws li ib qho dhau los lossis tom ntej node.

Yog li ntawd, dab tsi yuav yog lub sij hawm nyuaj thaum kev tshawb nrhiav binary siv rau ntawm daim ntawv txuas?

Lub sijhawm nyuaj yuav tsum tsis txhob ntau tshaj O(log n). Raws li linked list ua tsis muab random nkag yog tias peb sim siv binary tshawb nrhiav algorithm nws yuav ncav O(n) raws li peb xav tau nrhiav ntev ntawm cov lis thiab mus rau nruab nrab.

Kuj Paub, binary tshawb nrhiav siv li cas? Nrhiav Binary : Nrhiav ib tug txheeb array los ntawm rov faib cov nrhiav ntu ntu hauv ib nrab. Pib nrog ib ntus npog tag nrho cov array. Yog tus nqi ntawm cov nrhiav qhov tseem ceeb yog tsawg tshaj li qhov khoom nyob rau hauv nruab nrab ntawm lub caij nyoog, nqaim lub sij hawm mus rau lub qis ib nrab. Txwv tsis pub nqaim nws mus rau sab sauv ib nrab.

Yog li ntawd, txoj kev twg binary tshawb nrhiav siv los nrhiav cov khoom hauv ib daim ntawv teev npe?

Binary nrhiav ua hauj lwm ntawm sorted arrays. Binary nrhiav pib los ntawm kev sib piv ib keeb nyob rau hauv nruab nrab ntawm lub array nrog lub hom phiaj tus nqi . Yog lub hom phiaj tus nqi phim cov keeb , nws txoj hauj lwm nyob rau hauv lub array yog xa rov qab. Yog lub hom phiaj tus nqi yog tsawg dua cov keeb , cov nrhiav txuas ntxiv nyob rau hauv qis ib nrab ntawm cov array.

Yuav ua li cas koj iterate los ntawm ib daim ntawv txuas?

Ib Iterator tuaj yeem siv los voj dhau ib LinkedList . Txoj kev hasNext() rov qab muaj tseeb yog tias muaj ntau cov ntsiab lus hauv LinkedList thiab cuav lwm yam. Txoj kev tom ntej () rov qab cov ntsiab lus tom ntej hauv qhov LinkedList thiab pov qhov kev zam NoSuchElementException yog tias tsis muaj cov khoom ntxiv tom ntej.

Pom zoo: