|
Mavzu: qidirish va saralash algoritmlari
|
Sana | 05.01.2024 | Hajmi | 149,96 Kb. | | #130704 |
Bog'liq 2-labaratoriya ish
Muhammad Al-Xorazmiy nomidagi
Toshkent axborot texnologiyalari universiteti
“Ma’lumotlar tuzilmasi va algoritmlar”
2-Labaratoriys ish
Bajardi:
2-AMALIYOT ISHI
MAVZU: QIDIRISH VA SARALASH ALGORITMLARI
A. To’g’ridan-to’g’ri qo’shish usuli bilan saralash algoritmi(insertion sort)
B. Tanlash orqali saralash algoritmi (Selection sort)
C. Pufaksimon saralash algoritmi (Bubble sort)
D. Chiziqli qidirish(linear search)
E. Binar qidirish(binary search)
10-variant
A massiv elementlari qiymatlarini Pufaksimon saralash algoritmidan foydalanib o’sish tartibida saralab, eng kichkina manfiy elementini ekranga chiqazuvshi dastur tuzing.
Dastur kodi:
#include
#include
void flip(std::vector& arr, int i) {
int start = 0;
while (start < i) {
std::swap(arr[start], arr[i]);
start++;
i--;
}
}
int findMaxIndex(std::vector& arr, int n) {
int maxIndex = 0;
for (int i = 1; i < n; ++i) {
if (arr[i] > arr[maxIndex]) {
maxIndex = i;
}
}
return maxIndex;
}
void pancakeSort(std::vector& arr) {
int n = arr.size();
for (int currSize = n; currSize > 1; --currSize) {
int maxIndex = findMaxIndex(arr, currSize);
if (maxIndex != currSize - 1) {
flip(arr, maxIndex);
flip(arr, currSize - 1);
}
}
}
int main() {
// Massivni to'ldirish
std::vector arr = {5, -2, 10, -7, 3};
// Massivni o'sish tartibida saralash
pancakeSort(arr);
// Eng kichik manfiy elementni chiqarish
std::cout << "Eng kichik manfiy element: " << arr[0] << std::endl;
return 0;
}
Dastur natijasi:
|
| |