Вопрос задан 15.06.2023 в 22:20. Предмет Информатика. Спрашивает Ливинская Ярослава.

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

пузырька.
0 0
Перейти к ответам

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

Внимание! Ответы на вопросы дают живые люди. Они могут содержать ошибочную информацию, заблуждения, а также ответы могут быть сгенерированы нейросетями. Будьте внимательны. Если вы уверены, что ответ неверный, нажмите кнопку "Пожаловаться" под ответом.
Отвечает Лумпер Давид.

#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();

}

0 0
Отвечает нейросеть ChatGpt. Будьте внимательны, ответы нейросети могут содержать ошибочные или выдуманные данные.

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!

0 0

Похожие вопросы

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

Последние заданные вопросы в категории Информатика

Задать вопрос