Шукаєте відповіді та рішення тестів для S2 Estruturas de Dados e Algoritmos Fundamentais 2024 02? Перегляньте нашу велику колекцію перевірених відповідей для S2 Estruturas de Dados e Algoritmos Fundamentais 2024 02 в elearning.uab.pt.
Отримайте миттєвий доступ до точних відповідей та детальних пояснень для питань вашого курсу. Наша платформа, створена спільнотою, допомагає студентам досягати успіху!
Considere uma classe Sorter onde está definida uma variável \tiny a[size]
1 template<class T, int size=10>
2 class Sorter {
3 public:
4 ...
5 void insertionSort();
6 private:
7 int length;
8 T a[size];
9 };
Considere o método insertionSort() dessa classe
11 // Pré-condição: a[0]...a[a.length-1] é uma array de elementos
12 // T não ordenada.
13 // Pós-condição: A array a está ordenada por ordem decrescente.
14
15 template<class T, int size>
16 void Sorter<T,size>::insertionSort()
17 {
18 for (int i = 1; i < length; i++)
19 {
20 T temp = a[i];
21 int j = i - 1;
22 while (j >= 0 && temp > a[j])
23 {
24 a[j+1] = a[j];
25 j--;
26 }
27 a[j+1] = temp;
28 }
29 }
Quando ordenada em ordem descendente, utilizando o método insertionSort(), qual das listas seguintes necessita do maior número de mudanças de posição dos seus elementos?
Considere a seguinte classe
1 // Classe que ordena uma array de objectos em ordem
2 // decrescente utilizando o insertionSort.
3
4 template<class T, int size=10>
5 class Sorter {
6 public:
7 Sorter(T arr[], int s=size)
8 : length(s)
9 { // construtor
10 for (int i = 0; i < length; i++)
11 a[i] = arr[i];
12 }
13
14 ...
15 void selectionSort();
16 private:
17 int length;
18 T a[size];
19 // Trocar a[i] e a[j] na array a.
20 void swap(int i, int j)
21 {
22 <código 1>
23 }
24 };
Considere o método selectionSort() dessa classe
26 // Ordenar a array a em ordem decrescente, utilizando selectionSort.
27 // Pré-condição: a é uma array de elementos comparáveis.
28
29 template<class T, int size>
30 void Sorter<T,size>::selectionSort()
31 {
32 for (int i = 0; i < length - 1; i++)
33 {
34 // Encontrar o maior no intervalo a[i+1] a a[n-1]
35 T max = a[i];
36 int maxPos = i;
37 for (int j = i + 1; j < length; j++)
38 // a[j] é ainda maior
39 if (max < a[j])
40 {
41 max = a[j];
42 maxPos = j;
43 }
44 // Troca a[i] e a[maxPos]
45 swap(i, maxPos);
46 }
47 }
Se uma array de tipo int, contiver os elementos 89, 42, -3, 13, 109, 70, 2, qual das seguintes configurações representará a array, depois da terceira passagem do referido método, na ordenação da array por ordem descendente?
Assuma que \tiny a[0] ... a[n-1] é uma array de n números inteiros positivos e que a seguinte asserção é verdadeira
a[0] > a[k] para todo o k tal que 0 < k < NQual das seguintes afirmações tem de ser verdadeira?
Considere uma classe Sorter onde está definida uma variável \tiny a[size]
1 template<class T, int size=10>
2 class Sorter {
3 public:
4 ...
5 void insertionSort();
6 private:
7 int length;
8 T a[size];
9 };
Considere o método insertionSort() dessa classe
11 // Pré-condição: a[0]...a[a.length-1] é uma array de elementos
12 // T não ordenada.
13 // Pós-condição: A array a está ordenada por ordem decrescente.
14
15 template<class T, int size>
16 void Sorter<T,size>::insertionSort()
17 {
18 for (int i = 1; i < length; i++)
19 {
20 T temp = a[i];
21 int j = i - 1;
22 while (j >= 0 && temp > a[j])
23 {
24 a[j+1] = a[j];
25 j--;
26 }
27 a[j+1] = temp;
28 }
29 }
A invariante para o ciclo mais exterior (o ciclo for) é