Video: Dab tsi yog npuas sorting hauv Java?
2024 Tus sau: Lynn Donovan | [email protected]. Kawg hloov kho: 2023-12-15 23:47
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
- public class BubbleSortExample {
- static void bubbleSort(int arr) {
- int n = arr.length;
- int temp = 0;
- for(int i=0; i <n; i++){
- for(int j=1; j < (n-i); j++){
- if(arr[j-1] > arr[j]){
- // 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:
Dab tsi sorting algorithm muaj qhov zoo tshaj plaws asymptotic complexity?
Heap Sib Ib yam li ntawd, qhov kev txheeb xyuas algorithm twg muaj lub sijhawm zoo tshaj plaws? Rau qhov zoo tshaj plaws Insertion Cai thiab Heap Sib yog qhov zoo tshaj plaws raws li lawv cov ntaub ntawv zoo tshaj plaws khiav lub sij hawm nyuaj yog O (n).
Puas yog npuas npuas?
Delegation: focusin/focusout Cov xwm txheej tsom thiab plooj tsis ua npuas. Cov piv txwv saum toj no tsis ua haujlwm, vim tias thaum tus neeg siv tsom mus rau ib qho, qhov kev tshwm sim ua kom pom tseeb tshwm sim ntawm qhov kev tawm tswv yim nkaus xwb. Nws tsis npuas
Yuav ua li cas los tsim ib lub npuas nyob rau hauv ib daim ntawv txuas hauv C ++?
Txhawm rau ua npuas, peb ua raws li cov kauj ruam hauv qab no: Kauj Ruam 1: Xyuas seb cov ntaub ntawv ntawm 2 qhov chaw nyob ib sab 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. Kauj Ruam 2: Thaum kawg ntawm kev hla 1, cov khoom loj tshaj plaws yuav yog qhov kawg ntawm daim ntawv. Kauj ruam 3: Peb txiav lub voj, thaum tag nrho cov ntsiab lus pib
Qhov twg sorting algorithm yog qhov zoo tshaj plaws hauv qhov phem tshaj?
Sorting algorithms Algorithm Cov ntaub ntawv qauv Lub Sijhawm complexity: Qhov phem tshaj plaws ceev xaiv Array O(n2) Sib sau ua ke Array O(n log(n)) Heap txheeb Array O(n log(n)) Smooth txheeb Array O(n log(n))
Yuav ua li cas koj tshem tawm sorting los ntawm lub rooj nyob rau hauv kev nkag?
Txhawm rau tshem tawm ib hom: Qhib lub Tsev tab. Nyem qhov Clear All Sorts khawm hauv pawg Sort & Lim. Kev nkag mus tshem tawm txhua yam koj tau thov