Table of Contents[Hide][Show]
Computer science yog txhua yam hais txog kev nkag siab txog qhov nyuaj ntawm cov algorithms thiab cov qauv ntaub ntawv.
Koj muaj cov npe ntawm cov khoom uas yuav tsum tau txheeb xyuas, tab sis koj tsis muaj sijhawm los yog cov peev txheej los siv cov txheej txheem kev txheeb xyuas ntau dua.
Insertion sorting yog ib qho yooj yim sorting algorithms, tab sis nws tuaj yeem qeeb rau cov npe loj.
Kev siv yooj yim thiab kev nkag siab tau ua rau txoj kev no nyiam ntawm cov programmers. Nws zoo meej rau cov npe me me lossis thaum koj xav tau kev daws sai.
Nyob rau hauv no blog ncej, peb yuav saib lub sij hawm complexity ntawm insertion sorting. Qhov algorithm no yog siv los txheeb cov arrays, thiab nws muaj lub sijhawm ua haujlwm ntawm O (n2). Qhov no txhais tau hais tias lub sij hawm complexity nce nrog qhov loj ntawm cov array.
Txawm li cas los xij, qhov algorithm no tuaj yeem ua nrawm dua li lwm cov txheej txheem txheeb xyuas, xws li kev xaiv nrawm.
Cia peb saib ze dua ntawm qhov kev txheeb xyuas qhov kev sib dhos ua haujlwm li cas!
Dab tsi yog Insertion Sort Algorithm?
Ib lub caij ntawm ib lub sij hawm, qhov kev tso npe nkag tsim ib qho kev sib cais, uas feem ntau hu ua ib daim ntawv teev npe.
Piv txwv li, kev txheeb xyuas yog siv rau hauv cov khoos phis tawj nyuaj xws li compilers, qhov kev txiav txim ntawm tokens yog qhov tseem ceeb rau kev txhais cov program.
Insertion Sort ua haujlwm li cas?
Thaum peb siv insertion sort los txheeb cov array, lub algorithm pib los ntawm kev nrhiav cov khoom me tshaj plaws hauv cov npe thiab muab tso rau hauv txoj haujlwm kom raug.
Nws mam li pom cov khoom me tshaj plaws tom ntej thiab ntxig rau hauv txoj haujlwm kom raug, thiab lwm yam.
Lub algorithm ua haujlwm los ntawm looping los ntawm cov npe, sib piv txhua yam rau ib qho uas tuaj ua ntej nws.
Yog hais tias cov khoom nyob rau hauv qhov kev txiav txim tsis ncaj ncees lawm, lub algorithm swaps lawv. Nws mam li kuaj xyuas seb daim ntawv teev npe puas raug txheeb xyuas, thiab yog tias nws yog, cov algorithm xaus.
Hauv kev xyaum, kev txheeb xyuas feem ntau yog siv los ntawm ob peb kab ntawm cov cai, ua rau nws yog qhov kev xaiv nrov rau kev txheeb cov array me me. Txawm li cas los xij, lub sijhawm nyuaj yuav tsum xav txog thaum siv cov algorithm no.
Piv txwv li:
Ntawm no yog ib qho piv txwv ntawm yuav ua li cas insertion sorting ua haujlwm. Peb yuav siv cov nram qab no array:
1, 2, 3, 4, 5, 6
Lub algorithm pib los ntawm kev nrhiav cov khoom me tshaj plaws nyob rau hauv daim ntawv teev npe, uas yog 1. Nws ces ntxig rau hauv txoj hauj lwm kom raug, thawj txoj hauj lwm. Nws mam li pom cov khoom me tshaj plaws tom ntej, uas yog 2. Nws ntxig nws rau hauv txoj hauj lwm kom raug, uas yog txoj hauj lwm thib ob.
Nws mam li pom cov khoom me tshaj plaws tom ntej, uas yog 3. Nws ntxig nws rau hauv txoj haujlwm kom raug, uas yog qhov thib peb txoj haujlwm.
Tom qab ntawd nws pom cov khoom me tshaj plaws tom ntej, uas yog 4. Nws ntxig nws rau hauv txoj haujlwm kom raug, uas yog plaub txoj haujlwm, thiab lwm yam. Cov npe tam sim no tau txheeb!
Peb tuaj yeem pom los ntawm qhov piv txwv tias cov algorithm siv rau kev sib piv thiab sib pauv los txheeb cov npe. Qhov no yog vim nws siv n2 kev sib piv thiab sib pauv los txheeb cov npe ntawm cov khoom n. Hauv qhov no, n = 6.
Yuav txhim kho Insertion Sort Lub Sijhawm Complexity li cas?
Thaum cov txheej txheem ntxig muaj lub sijhawm ua haujlwm ntawm O (n2), nws tuaj yeem txhim kho los ntawm kev siv cov kev txheeb xyuas zoo dua, xws li kev xaiv nrawm.
Quicksort muaj O(n log n) runtime, uas yog nrawm dua O(n2).
Txawm li cas los xij, qee qhov xwm txheej, kev txheeb cais tuaj yeem nrawm dua li kev xaiv nrawm.
Piv txwv li, yog tias daim ntawv teev npe twb nyob rau hauv kev txiav txim, kev txheeb cais yuav siv sij hawm tsawg dua li kev xaiv nrawm.
Hauv kev xyaum, kev txheeb xyuas feem ntau yog siv los ntawm ob peb kab ntawm cov cai, ua rau nws yog qhov kev xaiv nrov rau kev txheeb cov array me me.
Txawm li cas los xij, lub sijhawm nyuaj yuav tsum xav txog thaum siv cov algorithm no.
Sijhawm nyuaj
Qhov phem tshaj Case Complexity O(n2):
Lub sij hawm complexity nce nrog qhov loj ntawm cov array. Nws siv n2 kev sib piv thiab sib pauv los txheeb cov npe ntawm cov khoom n.
Piv txwv li, yog tias peb muaj qhov loj me ntawm 1000, lub algorithm yuav siv 1,000,000 kev sib piv thiab sib pauv los txheeb cov array.
Qhov zoo tshaj plaws Case Complexity O(n):
Lub sij hawm complexity yog tib yam li qhov loj ntawm cov input array. Kuv
t siv n kev sib piv thiab sib pauv los txheeb cov npe ntawm cov khoom n. Piv txwv li, xav txog ib qho array ntawm qhov loj 5. Lub algorithm yuav siv tsib qhov sib piv thiab sib pauv los txheeb cov array.
Qhov Nruab Nrab Case Complexity O(n2):
Lub sijhawm complexity yog nruab nrab ntawm qhov phem tshaj plaws thiab qhov zoo tshaj plaws cov ntaub ntawv complexities nyob rau hauv cov ntaub ntawv no.
Nws siv n2 kev sib piv thiab sib pauv los txheeb cov npe ntawm cov khoom n.
Yog li, sorting sorting yog ib tug ruaj khov sorting algorithm.
Vim li cas Insertion Sort Stable?
Insertion sort yog ruaj khov vim nws khaws qhov kev txiav txim ntawm cov khoom sib npaug hauv cov khoom nkag.
Qhov no yog qhov tseem ceeb rau ntau daim ntawv thov, xws li cov ntaub ntawv rov qab los yog kev txheeb xyuas nyiaj txiag. Piv txwv li, yog tias peb muaj ob daim ntawv teev cov lej thiab xav muab piv rau lawv, peb yuav tsum ua kom paub tseeb tias qhov kev txiav txim ntawm cov ntsiab lus tau khaws cia.
Yog tias cov npe tsis raug txheeb xyuas, peb yuav tsis sib piv lawv kom raug.
Sau ntawv cia Ncua