Dab tsi yog npuas sorting hauv Java?
Dab tsi yog npuas sorting hauv Java?

Video: Dab tsi yog npuas sorting hauv Java?

Video: Dab tsi yog npuas sorting hauv Java?
Video: Poj niam hlub txiv ua dev ntis plhus 20.07.2018 2024, Tej zaum
Anonim

Npauj npaim yog qhov yooj yim tshaj plaws kev xaiv algorithm, nws muab piv rau thawj ob lub ntsiab lus, yog tias tus thawj yog ntau dua qhov thib ob, swaps lawv, txuas ntxiv ua (piv thiab swaps) rau cov khub tom ntej ntawm cov khoom uas nyob ib sab. Nws mam li pib dua nrog thawj ob lub ntsiab lus, sib piv, swaps kom txog thaum tsis muaj kev sib pauv ntxiv.

Ib sab ntawm no, dab tsi yog npuas hom hauv Java nrog piv txwv?

Npuas Sort yog qhov yooj yim tshaj plaws kev xaiv algorithm uas ua haujlwm los ntawm kev rov sib pauv cov ntsiab lus uas nyob ib sab yog tias lawv tsis ncaj ncees lawm. Piv txwv : Thawj Pass: (5 1 4 2 8) -> (1 5 4 2 8), Ntawm no, algorithm piv cov thawj ob lub ntsiab lus, thiab sib pauv txij li 5 > 1. (1 5 4 2 8) -> (1 4 5 28), Sib pauv txij li 5> 4.

Tsis tas li ntawd, ua li cas koj sau cov npuas hauv Java? Bubble Sort hauv Java

  1. public class BubbleSortExample {
  2. static void bubbleSort(int arr) {
  3. int n = arr.length;
  4. int temp = 0;
  5. for(int i=0; i <n; i++){
  6. for(int j=1; j < (n-i); j++){
  7. if(arr[j-1] > arr[j]){
  8. // pauv cov ntsiab lus.

Kuj nug, dab tsi yog txhais los ntawm npuas hom hauv Java?

Ua npuas ncauj yog ib qho yooj yim algorithm uas piv cov thawj lub caij ntawm cov array mus rau lwm tus. Yog hais tias lub caij tam sim no ntawm cov array yog tus lej ntau dua qhov tom ntej, cov ntsiab lus tau hloov pauv.

Lub tshuab npuas ua haujlwm li cas?

Es tsis txhob searching ib array tag nrho, lub npuas txheeb ua haujlwm los ntawm kev sib piv cov khoom nyob ib sab ntawm cov khoom hauv array. Yog hais tias cov khoom tsis nyob rau hauv qhov raug txiav txim, lawv yuav swapped kom qhov loj tshaj ntawm ob mus txog. Lub swapping txuas ntxiv mus txog thaum tag nrho cov array nyob rau hauv qhov kev txiav txim kom raug.

Pom zoo: