Lokacin da muke magana game da rarrabuwa a cikin tsarin shirye-shirye, ana ɗaukarsa ɗayan ayyuka masu mahimmanci kuma galibi ana buƙata yayin haɓaka aikace-aikace. A kimiyyar kwamfuta, rarrabuwar algorithm wata hanya ce da ake amfani da ita don sake tsara abubuwan da ke cikin jeri a cikin takamaiman tsari, walau hawan hawan ko saukowa ko na ƙamus. A cikin wannan yanayin, za mu fi mai da hankali ne kan matsalar nau'ikan nau'ikan nau'ikan nau'ikan shirye-shiryen C, ayyukan sa, da yadda yake ba da ingantacciyar mafita.
#include
nau'in mara amfani (int array[], int n) {
don (int mataki = 0; mataki <n - 1; ++mataki) {int min_idx = mataki; don (int i = mataki + 1; i <n; ++i) {idan (array[i] <array[min_idx]) {min_idx = i; }} int temp = tsararru [min_idx]; tsararru [min_idx] = tsararru [mataki]; tsararru[mataki] = temp; } } [/code]
Bayanin Ayyukan Rarraba a cikin C
Babban ƙa'idar da ke bayan rarrabuwa algorithm da aka yi amfani da ita a cikin C galibi ɗaya ne na kwatance. Tsarin ya ƙunshi ƙididdigewa ta hanyar fihirisar jeri, kwatanta abubuwa da canza su idan sun kasance cikin tsari mara kyau. Duban da kyau cikin lambar da aka ambata, aikinmu, nau'in(), yana daidaita wannan aikin daidaitawa.
Da farko, aikin yana farawa da madauki na waje yana gudana daga kashi na farko zuwa ɗaya kafin na ƙarshe, wanda zaku lura daga madauki kalmar 'for (int step = 0; step <n - 1; ++ step)'. Yana ɗaukar kashi na farko azaman ƙarami (`int min_idx = mataki`). Madauki na gida-don madauki sannan yana jujjuya sauran abubuwan da ke cikin tsararru. Idan a kowane hali ta sami wani abu mafi ƙanƙanta fiye da abin da muka zaci tun farko (`if (array[i] <array[min_idx]`),sai ya sanya hakan a matsayin sabon mafi ƙarancin. musanya wannan ƙaramar element tare da kashi na farko, ta yadda za a tabbatar da cewa matsayi na farko ya ƙunshi mafi ƙanƙanci.
Laburaren Amfani da Ayyuka a cikin Matsalolin da Aka Bayar
Kyawun shirye-shiryen C ba wai kawai a cikin ƙarfinsa ba ne, har ma da samun dakunan karatu yana sauƙaƙa wa codeers aiwatar da ɗimbin ayyuka. A cikin yanayin jerin abubuwan mu, mun yi amfani da ɗakin karatu na `stdio.h`. Wannan ɗakin karatu yana riƙe da ayyukan da suka haɗa da ayyukan shigarwa/fitarwa ('printf()' da 'scanf()' misali).
Duk da haka, ainihin wannan matsalar ba ta ta'allaka kan ɗakunan karatu da yawa na C ba amma a cikin aikin da muka ƙirƙira, nau'in(). Wannan aikin yana amfani da ra'ayin Jerin Zabi, ɗaya daga cikin mafi sauƙi nau'ikan rarrabuwa algorithms. Sauƙin sa, duk da haka, baya lalata iyawarsa da amincinsa a fagen rarrabuwar kawuna.
Yayin da muke bayyana ɓangarori masu rikitarwa na shirye-shirye, ba za mu iya nisantar da kanmu daga tasirin da salon ya yi a duniya ba. Ya kasance wuraren shakatawa, nune-nunen ko kuma kyawawan makonni masu kayatarwa waɗanda ke jan hankalin duniya.