
Дан массив а=[2,10,5,3,11,9,12]. Удалите из массива четные числа. Выполните сортировку методом
пузырька.

Ответы на вопрос

#include <iostream>
#include <vector>
using namespace std;
void bubbleSort(vector<int> &a_){
vector<int> t;
int n = a_.size();
for(int i = 0; i < n; i++)
for(int j = 0; j < n - i - 1; j++)
if(a_[j] > a_[j + 1])
swap(a_[j], a_[j + 1]);
for(auto &i: a_) if(i & 1) t.push_back(i);
a_ = t;
}
void solve(){
vector<int> a = {2, 10, 5, 3, 11, 9, 12};
bubbleSort(a);
cout << "a = {";
for(int i = 0; i < a.size() - 1; i++)
cout << a[i] << ", ";
cout << a.back() << "}";
}
int main(){
solve();
}



Task: Removing Even Numbers from an Array and Sorting it using Bubble Sort
To remove even numbers from the given array `[2, 10, 5, 3, 11, 9, 12]` and sort it using the bubble sort algorithm, you can follow these steps:
1. Iterate through the array and remove all even numbers. 2. Implement the bubble sort algorithm to sort the remaining numbers in ascending order.
Let's go through each step in detail.
Step 1: Removing Even Numbers
To remove even numbers from the array, you can iterate through the array and filter out the even numbers using a conditional statement. Here's an example in JavaScript:
```javascript let arr = [2, 10, 5, 3, 11, 9, 12]; arr = arr.filter(num => num % 2 !== 0); ```
After executing this code, the `arr` variable will contain only the odd numbers `[5, 3, 11, 9]`.
Step 2: Sorting using Bubble Sort
The bubble sort algorithm works by repeatedly swapping adjacent elements if they are in the wrong order. This process is repeated until the array is sorted.
Here's an example implementation of the bubble sort algorithm in JavaScript:
```javascript function bubbleSort(arr) { let len = arr.length; let swapped; do { swapped = false; for (let i = 0; i < len - 1; i++) { if (arr[i] > arr[i + 1]) { let temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; swapped = true; } } } while (swapped); return arr; }
let sortedArr = bubbleSort(arr); ```
After executing this code, the `sortedArr` variable will contain the sorted array `[3, 5, 9, 11]`.
Summary
To remove even numbers from the array `[2, 10, 5, 3, 11, 9, 12]` and sort it using the bubble sort algorithm, you can follow these steps:
1. Remove even numbers from the array using the `filter` method. 2. Sort the remaining numbers using the bubble sort algorithm.
The resulting sorted array will be `[3, 5, 9, 11]`.
Please let me know if you need any further assistance!


Похожие вопросы
Топ вопросов за вчера в категории Информатика








Последние заданные вопросы в категории Информатика
-
Математика
-
Литература
-
Алгебра
-
Русский язык
-
Геометрия
-
Английский язык
-
Химия
-
Физика
-
Биология
-
Другие предметы
-
История
-
Обществознание
-
Окружающий мир
-
География
-
Українська мова
-
Информатика
-
Українська література
-
Қазақ тiлi
-
Экономика
-
Музыка
-
Право
-
Беларуская мова
-
Французский язык
-
Немецкий язык
-
МХК
-
ОБЖ
-
Психология
-
Физкультура и спорт
-
Астрономия
-
Кыргыз тили
-
Оʻzbek tili