AMALIY TOPSHIRIQ № 11
Vazifa:
Har bir talaba o’zining ismi familyasidagi harflarni alifbo tartibida QuickSort() va MergeSort() joylashtiring.
Yechim:
#include
#include
#include
using namespace std;
// QuickSort
int partition(vector& arr, int low, int high) {
string pivot = arr[high];
int i = low - 1;
for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
return i + 1;
}
void quickSort(vector& arr, int low, int high) {
if (low < high) {
int pivotIndex = partition(arr, low, high);
quickSort(arr, low, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, high);
}
}
// MergeSort
void merge(vector& arr, int left, int mid, int right) {
int i, j, k;
int n1 = mid - left + 1;
int n2 = right - mid;
vector L(n1), R(n2);
for (i = 0; i < n1; i++)
L[i] = arr[left + i];
for (j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];
i = 0;
j = 0;
k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(vector& arr, int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
int main() {
vector names = {"A","D","H","A","M","O","V","N","A","M","U","S","L","I","M","A"};
quickSort(names, 0, names.size() - 1);
cout << "QuickSort:" << endl;
for (const string& name : names) {
cout << name << " ";
}
cout << endl;
mergeSort(names, 0, names.size() - 1);
cout << "MergeSort:" << endl;
for (const string& name : names) {
cout << name << " ";
}
cout << endl;
return 0;
}
Natija:
AMALIY TOPSHIRIQ № 12
Vazifa:
Grafni hosil qiling va unda Eyler va Gamilton sikli mavjudligini tekshiring. Uning hususiyatlarini aytib bering.
Yechim:
V = (1,2)(1,3)(1,4)(2,3)(2,4)(2,5)(3,5)(4,5)
Eyler emas, Gamilton emas
Graf Eyler emas, chunki uning qirralari kesishgan. (1,2), (1,3) va (4,5) qovurg'alar kesishadi.
Graf ham Hamilton emas, chunki grafning barcha cho'qqilaridan o'tadigan Hamilton yo'li yo'q.
Grafada tsikllar mavjud. Masalan, tsikl (1,2,3,5,4,1) va tsikl (1,3,5,2,4,1).
AMALIY TOPSHIRIQ № 13
Vazifa:
vector list; dan foydalangan xolda assigned() va erase funksiyalaridan foydalaning va natijalar ekranga chiqarilsin.
Yechim:
#include
#include
#include
using namespace std;
int main() {
vector list;
// assign elements to the vector
list.assign({ "ADHAMOVNA", "MUSLIMA" });
cout << "Original List:" << endl;
for (const string& item : list) {
cout << item << endl;
}
cout << endl;
// erase individual characters from each string
for (string& item : list) {
item.erase(0, item.size());
}
cout << "Updated List:" << endl;
for (const string& item : list) {
cout << item << endl;
}
cout << endl;
return 0;
}
Natija:
AMALIY TOPSHIRIQ № 14
Vazifa
x2-3x+1=0 teglamaning haqiqiy ildizlari to‘plamini toping.
Yechim:
#include
#include
int main() {
double a = 1.0;
double b = -3.0;
double c = 1.0;
double discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
double root1 = (-b + sqrt(discriminant)) / (2 * a);
double root2 = (-b - sqrt(discriminant)) / (2 * a);
std::cout << "Haqiqiy ildizlar mavjud: x1 = " << root1 << ", x2 = " << root2 << std::endl;
} else if (discriminant == 0) {
double root = -b / (2 * a);
std::cout << "Bir haqiqiy ildiz mavjud: x = " << root << std::endl;
} else {
std::cout << "Haqiqiy ildiz mavjud emas" << std::endl;
}
return 0;
}
Natija:
Do'stlaringiz bilan baham: |