NAMUNA
(Amaliyot ishlarini bajarilishiga namuna )
AMALIYOTY ISHI-2
Bajardi:Mehriddin G’ofurov
Mavzu:QIDIRISH VA SARALASH ALGORITMLARI
VARIANT-14
B.Tanlash orqali saralash algoritmi (Selection sort)
14.Toq va juft sonlardan tashkil topgan B massiv berilgan. Barcha toq sonlarni chiqarib, juft sonlarini Pufaksimon saralash algoritmidan foydalanib o’sish tartibda joylashtiruvchi dastur tuzing.
DASTUR KODI
#include
using namespace std;
void twoWaySort(int arr[], int n)
{
int l = 0, r = n - 1;
int k = 0;
while (l < r)
{
while (arr[l] % 2 != 0)
{
l++;
k++;
}
while (arr[r] % 2 == 0 && l < r)
r--;
if (l < r)
swap(arr[l], arr[r]);
}
sort(arr, arr + k, greater());
sort(arr + k, arr + n);
}
int main()
{
int arr[] = { 1, 3, 2, 7, 5, 4 };
int n = sizeof(arr) / sizeof(int);
twoWaySort(arr, n);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
Dostları ilə paylaş: |