N õlchamli massiv berilgan. Massiv elementlarini pufakchali saralash usulida õsish kamayish tartibida saralang
#include
using namespace std;
void bubbleSortAscending(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
void bubbleSortDescending(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] < arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSortAscending(arr, n);
cout << "Pufakchali saralash (õsish tartibida): ";
printArray(arr, n);
int arr2[] = {64, 25, 12, 22, 11};
bubbleSortDescending(arr2, n);
cout << "Pufakchali saralash (kamayish tartibida): ";
printArray(arr2, n);
return 0;
}
Dostları ilə paylaş: |