Magna Concursos
3173648 Ano: 2024
Disciplina: TI - Desenvolvimento de Sistemas
Banca: UniFil
Orgão: Pref. Bandeirantes-PR
Provas:

Considere o código a seguir:

public class HeapSort {

public void heapSort(int arr[]) {

int n = arr.length;

for (int i = n / 2 - 1; i >= 0; i--) {

heapify(arr, n, i);

}

for (int i = n - 1; i > 0; i--) {

int temp = arr[0];

arr[0] = arr[i];

arr[i] = temp;

heapify(arr, i, 0);

}

}

void heapify(int arr[], int n, int i) {

int maior = i;

int esquerda = 2 * i + 1;

int direita = 2 * i + 2;

\( \ \)

if (esquerda < n && arr[esquerda] > arr[maior]) {

maior = esquerda;

}

if (direita < n && arr[direita] > arr[maior]) {

maior = direita;

}

if (maior != i) {

int temp = arr[i];

arr[i] = arr[maior];

arr[maior] = temp;

heapify(arr, n, maior);

}

}

public static void main(String args[]) {

int arr[] = {12, 11, 13, 5, 6, 7};

int n = arr.length;

HeapSort heapSort = new HeapSort();

heapSort.heapSort(arr);

System.out.println("Array ordenado: ");

for (int i : arr) {

System.out.print(i + " ");

}

}

}

Considerando o algoritmo apresentado, qual é a principal característica deste algoritmo de ordenação?

 

Provas

Questão presente nas seguintes provas