An warware: nau'in kumfa c

Tabbas, zan iya ɗaukar wannan aikin! Ga yadda zan fara labarin:

Rarraba algorithms wani muhimmin bangare ne na kimiyyar kwamfuta da shirye-shirye saboda suna ba mu damar yin odar bayanai da kyau. Ɗaya daga cikin mafi sauƙi kuma mafi yawan dabarun rarrabuwa shine Bubble Sort, ƙayyadaddun tushen algorithm wanda akai-akai ta hanyar jeri, yana kwatanta abubuwan da ke kusa, da musanya su idan sun kasance cikin tsari mara kyau. Ana yin hanyar wucewa ta jeri-jeri akai-akai har sai ba a buƙatar musanyawa ba, yana nuna cewa an jera jeri.

Bubble Sort ba ingantaccen rarrabuwa ba ne don manyan jeri, amma saboda sauƙin sa, galibi ana koyar da shi a cikin gabatarwar darussan kimiyyar kwamfuta. Ko da yake matsakaicin lokaci da kuma mafi munin lokacin rikitarwa na O (n ^ 2) na iya sanya shi zaɓi mara kyau don manyan bayanan bayanai, har yanzu yana iya zama mai amfani a wasu lokuta masu amfani inda sauƙi da sauƙin aiwatarwa al'amarin fiye da ɗanyen aiki.

#include

vaid bubbleSort(int array[], int size) {
don (int mataki = 0; mataki < size - 1; ++ mataki) {don (int i = 0; i < size - mataki - 1; ++i) {idan (array[i]> tsararru[i + 1) ]) {
int temp = tsararru[i];
tsararru[i] = tsararru[i + 1];
tsararru[i + 1] = yanayin zafi;
}
}
}
}

m printArray (int tsararru[], girman int) {
don (int i = 0; i < size; ++i) printf("%d", tsararru[i]); printf("n"); } int main() {int data[] = {-2, 45, 0, 11, -9}; girman int = girman (bayanai) / girman (bayanai [0]); bubbleSort (bayanai, girman); printf ("Rarraba Tsara a Tsarin Haurawa: n"); printArray (bayanai, girman); dawo 0; } [/code]

Fahimtar Lambar Tsarin Bubble

A cikin lambar da ke sama, mun fara haɗa da stdio.h ɗakin karatu, wanda ke ba mu damar yin ayyukan shigarwa da fitarwa. Babban aikin shirin mu yana rufe a cikin bubbleSort() aiki, wanda ke ɗaukar tsararru da girmansa azaman sigogi, kuma yana tsara tsararru ta amfani da Bubble Sort algorithm.

Algorithm ɗin kumfa yana aiki ta maimaita musanya abubuwan da ke kusa idan sun kasance cikin tsari mara kyau. Ana maimaita wannan tsari har sai ba a buƙatar ƙarin musanyawa. A cikin aiwatarwa, ana amfani da gida biyu don madaukai don cimma wannan. Madauki na waje, mataki, yana sarrafa sau nawa algorithm ya kamata ya maimaita kan tsararru. Madauki na ciki, i, sannan ta shiga cikin jeri da kwatanta kowane nau'i na abubuwa.

Mabuɗin Ayyuka da Laburaren

A cikin code, da printf() Ana amfani da aikin don buga tsararrun tsararru. An haɗa wannan aikin a cikin stdio.h ɗakin karatu. The girman () ana amfani da afareta don samun girman jeri, ta hanyar raba jimillar girman jeri da girman nau'in tsararru ɗaya.

The bubbleSort() aiki yana tsara tsararru. Yanayi aikin da aka ayyana; mai amfani yana ba da jikin aikin. Aikin printArray() ana amfani da shi don buga tsararru. Hakanan aiki ne da aka ayyana mai amfani. The babban () Aiki shine farkon aiwatar da shirin a C. Misali yana amfani da wannan aikin don nuna nau'in kumfa algorithm.

Shafi posts:

Leave a Comment