|
-rasm. Kiritish usuli (insertion sort)
|
bet | 3/5 | Sana | 12.06.2024 | Hajmi | 0,96 Mb. | | #262783 |
Bog'liq Raxmonova M.R. 5-rasm. Kiritish usuli (insertion sort)
Kiritish usulining algoritmi quyidagicha: InsertionSort (Arr, N) // Arr is an array of size N.
{For ( I:= 2 to N ) // N elements => (N-1) pass
{insert_at = I; // Find suitable position insert_at, for Arr[I] item = Arr[I]; J=I-1;
While ( J ? 1 && item < Arr[J] )
63
O‘ZBEKISTONDA FANLARARO INNOVATSIYALAR VA
8-SON ILMIY TADQIQOTLAR JURNALI 20.05.2022
{Arr[J+1] = Arr[J];
// insert_at = J; J--;}
insert_at = J+1;
Arr[insert_at] = item; // Arr[J+1] = item;}}}
Yana bir saralash usullaridan biri bu - Birlashtirish usuli (6-rasm Merge sort)dir. Bu usul “Bo‘l va zabt et” tamoyiliga amal qiladi, unga ko‘ra ma’lumotlar massivi teng qismlarga bo‘linadi, ular alohida tartiblanadi. Ular birlashgandan so'ng, natijada tartiblangan massiv hosil bo'ladi.[5]
6-rasm. Birlashtirish usuli (Merge sort).
Birlashtirish usulining dastur listingi quyidagi ko’rinishda yozish mumkin: void Merge(int *a, int low, int high, int mid)
{ int i, j, k, temp[high-low+1]; i = low;
k = 0;
j = mid + 1;
while (i <= mid && j <= high)
{if (a[i] < a[j]){temp[k] = a[i]; k++;
i++;}
else
{temp[k] = a[j]; k++;
j++;
}}
while (i <= mid)
{ temp[k] = a[i]; k++;
i++; }
while (j <= high)
{ temp[k] = a[j]; k++;
j++;
}
O‘ZBEKISTONDA FANLARARO INNOVATSIYALAR VA
8-SON ILMIY TADQIQOTLAR JURNALI 20.05.2022
for (i = low; i <= high; i++)
{a[i] = temp[i-low];}
}
void MergeSort(int *a, int low, int high)
{int mid;
if (low < high)
{mid=(low+high)/2; MergeSort(a, low, mid); MergeSort(a, mid+1, high); Merge(a, low, high, mid);} }
Tanishadigan yana bir saralash algoritmi bu Tez saralash usuli (7-rasm quick sort)dir. Eng tezkor tartiblash algoritmlaridan biri hisoblanadi. Birlashtirish usuli kabi, u bo'lish va egallash asosida ishlaydi.[6]
|
| |