void quick_sort( int listf], int left, int right)
(
int pivot, left_arrow, right_arrow;
left arrow = left;
right_arrow = right;
pivot = list [(left + right)/2J;
do {
while ( list[right_arrow] > p iv o t)
right arrow—; while ( list[left_arrow] < p iv o t)
1eft_arrow++; if ( left_arrow < = right arrow )
{
sw ap( list[left_arrow], list[right_arrow] );
left_arrow++;
right_arrow ~;}
}
while ( right_arrow > = left jjr r o w );
i f ( left < right arrow )
quick_sort( list, left, right arrow );
i f ( left arrow < right)
quick_sort( list, left arrow, right);
}
116
Takrorolash uchun savol va topshiriqlar
1. Rekursiya nima?
2. Rekursiv “cho‘kish” va “suzib chiqish” deganda nimani tushu-
nasiz? M isollar bilan iizohlang.
3. Rekursiyaning afzalliklarini nimada deb o ‘ylaysiz?
4. Quyidagi masalalar uchun dastur ishlab chiqing.
a) a ni eng kam k o ‘paytirish amallari yordam ida hisoblang;
b) faqat 1 sonini q o‘shish orqali ikkita natural sonni qo‘shing;
c) faqat qo‘shish amalidan foydalanib, ikki natural sonni k o ‘pay-
tiring;
d) rekursiyadan foydalanib hisoblang:
e) Fibonachchi sonlari ketma-ketligi / 0=1 , / i = l , f n-
2
=f i- \+f» n> 1
formulalar bilan aniqlanadi. Shu ketma-ketlikning dastlabki k ta hadini
rekursiv usulda toping.
Do'stlaringiz bilan baham: |