• Tezkor tartiblash.
  • Foydalanilgan adabyotlar
  • Nukus innovatsion instituti




    Download 72,89 Kb.
    bet3/3
    Sana14.05.2024
    Hajmi72,89 Kb.
    #231883
    1   2   3
    Bog'liq
    С kattaliklar O\'tebaeva N

    Izlash va tartiblash
    Ikkiga bo’lib izlash. Quyidagi dasturda tartiblangan massivda ikkiga bo’lib kalit sonni izlash algoritmi asosida tuzilgan funksiyadan foydalanish keltirilgan:
    #include
    #include
    int bsearch(int a[],int key,int n)
    {
    int m1,m2,m;
    m1 = 0;m2 = n-1;
    while(m1< = m2)
    {
    m = (m2+m1)/2;
    if (a[m] == key) return m;
    if (a[m]>key) m2 = --m;
    if (a[m]}
    return -1;
    };
    int main(int argc, char* argv[])
    {
    int m;
    int a[] = {5,6,9,11};
    m = bsearch(a,7,4);
    printf("%d",m);
    getch();
    return 0;
    }
    Keyingi misolda shu funksiya satrlar uchun varianti keltirilgan:


    #include
    #include
    #include
    #define size 5
    int strbsearch(char a[][size],char key[],int n)
    {
    int m1,m2,m,pr;
    m1 = 0;m2 = n-1;
    while(m1< = m2)
    {
    m = (m2+m1)/2;
    pr = strcmp(a[m],key);
    if (pr == 0) return m;
    if (pr == -1) m2 = --m;
    if (pr == 1) m1 = ++m;
    }
    return -1;
    };
    int main(int argc, char* argv[])
    {
    int m;
    char a[][size] = {"aaa","aab","aac","aad"};
    m = strbsearch(a,"aab",4);
    printf("%d",m);
    getch();
    return 0;
    }
    Tezkor tartiblash. Quyidagi dasturda tezkor tartiblash algoritmiga asoslangan funksiyadan foydalanilgan. Algoritm mohiyati shundan iboratki, avval yetakchi element tanlanadi. Funksiyada yetakchi element sifatida boshlang’ich element tanlanadi. Shundan so’ng massiv ikki qismga ajratiladi. Yetakchi elementdan kichik elementlar past qismga, katta elementlar yuqori qismga to’planadi. Shundan so’ng rekursiya asosida algoritm ikkala qismga alohida qo’llanadi.
    #include
    #include
    int a[] = {5,4};
    void sqsort(int k1, int k2)
    {
    if(k1 < k2){
    int i, j, k;
    i = k1; j = k2;
    while(i < j)
    {
    if (a[k1] > a[i]) {i++; continue;}
    if (a[k1] < a[j]) {j--; continue;}
    k = a[j]; a[j] = a[i]; a[i] = k;
    }
    k = a[k1]; a[k1] = a[i]; a[i] = k;
    sqsort(k1, i);
    sqsort(i+1, k2);
    };
    }
    int main(int argc, char* argv[])
    {
    int i;
    sqsort(0, 1);
    for(i = 0; i < 2; i++) printf("%d ", a[i]);
    getch();
    return 0;
    }


    Foydalanilgan adabyotlar:


    1. Bjarne Stroustrup. Programming: Principles and Practice Using C++ (2nd Edition). Person Education, Inc. 2014. second printing, January 2015.

    2. Harry Hariom Choudhary, Bjarne M Stroustrup. C++ Programming Professional. Sixth Best Selling Edition for Beginner's & Expert's 2014.

    3. “C++ tilida dasturlash asoslari” fani bo’yicha o’quv uslubiy majmua Тошкент 2016.

    Download 72,89 Kb.
    1   2   3




    Download 72,89 Kb.