Cov txheej txheem:

Yuav ua li cas los tsim ib lub npuas nyob rau hauv ib daim ntawv txuas hauv C ++?
Yuav ua li cas los tsim ib lub npuas nyob rau hauv ib daim ntawv txuas hauv C ++?

Video: Yuav ua li cas los tsim ib lub npuas nyob rau hauv ib daim ntawv txuas hauv C ++?

Video: Yuav ua li cas los tsim ib lub npuas nyob rau hauv ib daim ntawv txuas hauv C ++?
Video: Yuav ua li cas thiaj khiav dim lub ntiajteb no 2024, Tej zaum
Anonim

Txhawm rau ua npuas, peb ua raws cov kauj ruam hauv qab no:

  1. Kauj Ruam 1: Xyuas seb cov ntaub ntawv ntawm 2 qhov nyob ib sab yog nyob rau hauv qhov kev txiav txim nce los yog tsis. Yog tias tsis yog, sib pauv cov ntaub ntawv ntawm 2 qhov chaw nyob ib sab.
  2. Kauj Ruam 2: Thaum kawg ntawm kev hla 1, cov khoom loj tshaj plaws yuav yog qhov kawg ntawm daim ntawv.
  3. Kauj ruam 3: Peb txiav lub voj, thaum tag nrho cov ntsiab lus pib.

Ua li no, koj yuav ua li cas thiaj tsim tau cov npuas hauv ib daim ntawv txuas hauv C ++?

Txhawm rau ua npuas, peb ua raws cov kauj ruam hauv qab no:

  1. Kauj Ruam 1: Xyuas seb cov ntaub ntawv ntawm 2 qhov nyob ib sab yog nyob rau hauv qhov kev txiav txim nce los yog tsis. Yog tias tsis yog, sib pauv cov ntaub ntawv ntawm 2 qhov chaw nyob ib sab.
  2. Kauj Ruam 2: Thaum kawg ntawm kev hla 1, cov khoom loj tshaj plaws yuav yog qhov kawg ntawm daim ntawv.
  3. Kauj ruam 3: Peb txiav lub voj, thaum tag nrho cov ntsiab lus pib.

Ib yam li ntawd, dab tsi yog npuas hom C ++? Npuas Sort . Hauv npuas tsi , raws li cov ntsiab lus txheeb lawv maj mam " npuas " (los yog nce) mus rau lawv qhov chaw nyob hauv cov array, xws li npuas nce nyob rau hauv ib khob dej qab zib. Cov npuas tsi pheej sib piv cov nyob ib sab ntawm ib array. Thawj thiab thib ob ntsiab yog muab piv thiab swapped yog hais tias tawm ntawm kev txiav txim.

Qhov thib ob, koj yuav txheeb cov ntaub ntawv li cas hauv cov npe txuas?

Algorithm

  1. Tsim ib chav kawm Node uas muaj ob tus cwj pwm: cov ntaub ntawv thiab tom ntej.
  2. Tsim lwm chav kawm SortList uas muaj ob tus cwj pwm: lub taub hau thiab tus Tsov tus tw.
  3. addNode() yuav ntxiv cov node tshiab rau cov npe:
  4. sortList() yuav txheeb cov nodes ntawm cov npe nyob rau hauv ascending order.
  5. display() yuav tso saib cov nodes tam sim no nyob rau hauv daim ntawv:

Dab tsi yog qhov zoo tshaj plaws sorting algorithm rau cov npe txuas?

Sib koom ua ke feem ntau nyiam rau kev txheeb xyuas cov npe txuas. Qhov kev ua haujlwm qeeb qeeb-nruab nrab ntawm cov npe sib txuas ua rau qee qhov algorithms (xws li kev ceev ) ua haujlwm tsis zoo, thiab lwm yam (xws li heapsort ) ua tsis tau kiag li. Cia lub taub hau yog thawj qhov ntawm cov npe txuas kom raug txheeb xyuas thiab headRef yog tus taw tes rau lub taub hau.