Código 101: Classifique uma matriz usando recursão
Pergunta: Dada uma variedade de números inteiros, classifique a matriz em ordem ascendente. Exemplo 1: entrada: nums = [5,2,3,1] Saída: [1,2,3,5]
Exemplo 2: entrada: nums = [5,1,1,2,0,0] Saída: [0,0,1,1,2,5]
Restrições: 1 <= nums.length <= 5 * 104 -5 * 104 <= nums[i] <= 5 * 104
Solution:
Let’s start with Recursion.
I found this definition while starting with recursion and found it useful.
Recursion is the process of defining a problem (or the solution to a problem) in terms of (a simpler version of) itself.
Recursive Algorithm
All recursive algorithms must have the following:
Base Case (when to stop)
Work needed to move toward Base Case or Solution
Recursive Call (call ourselves)
Now coming back to the problem, we know that we need to breakdown our solution in above 3 points
Think when do you tell an array to be sorted.
if the size is zero?
if the size is 1?
if the array is in ascending order :)?
Now can we make a recursive call to attain 1.a?
yes, if we make a recursive call by decreasing the size of the array
Now we will try to code what we just thought.
public class SortArray {
public static void sort(int[] arr, int n) {
if (n == 0) return;
sort(arr, n-1);
}
public static void main(String[] args) {
int[] arr = new int[] {3, 4, 2, 6, 1, 1, 9, 1};
for (int i : arr) {
System.out.print(i + ” ,”);
}
System.out.println(“\n>>> “); classificar (arr, arr.length – 1); system.out.println (” <<< “); para (int i: arr) {System.out.print (i +”, “);}}} agora podemos acrescentar um que podemos acrescentar e, em seguida. Digamos que tenhamos uma matriz classificada. A matriz e ela será classificada.[] arr, int n) {if (n == 0) return; int val = arr[n]; classificar (arr, n-1); inserir (arr, val, n); } // Então, apenas pegamos o último elemento e o colocamos na posição correta agora para inserir elementos de volta, escreveremos o método de inserção. Inserção de vazio estático privado (int[] arr, int valor, int n) {if (n == 0 || arr.[n-1] <= value) {arr[n] = valor; } else {int val = ARR[n-1]; inserir (arr, valor, n – 1); arr[n] = val; }} Código e a ordem das chamadas importam java.util.arrays; classe pública SortArray {public static void Sort (int[] arr, int n) {if (n == 0) return; int val = arr[n]; classificar (arr, n-1); inserir (arr, val, n); } Inserção de void estático privado (int[] arr, int valor, int n) {if (n == 0 || arr.[n-1] <= value) {
arr[n] = value;
} else {
int val = arr[n-1];
insert(arr, value, n – 1);
arr[n] = val;
}
}
public static void main(String[] args) {
int[] arr = new int[] {9, 4, 2, 9, 1};
for (int i : arr) {
System.out.print(i + ” ,”);
}
System.out.println(“\n>>> “); classificar (arr, arr.length – 1); system.out.println (” <<< “); para (int i: arr) {System.out.print (i +”, “);}}} classificar [9, 4, 2, 9, 1] Tamanho do ARR: 4 Classificação [9, 4, 2, 9, 1] Tamanho do ARR: 3 classificar [9, 4, 2, 9, 1] Tamanho do ARR: 2 classificar [9, 4, 2, 9, 1] Tamanho do ARR: 1 classificação [9, 4, 2, 9, 1] Tamanho do ARR: 0 Inserir [9, 4, 2, 9, 1] Tamanho do ARR: 1 Inserir [9, 4, 2, 9, 1] Tamanho do ARR: 0 Valor de inserção: 4 AT 0 INSERTA VALOR: 9 AT 1 INSERT [4, 9, 2, 9, 1] Tamanho do ARR: 2 Inserir [4, 9, 2, 9, 1] Tamanho do ARR: 1 Inserir [4, 9, 2, 9, 1] ARR TAMANHO: 0 Valor de inserção: 2 AT 0 INSERIÇÃO VALOR: 4 AT 1 Valor de inserção: 9 AT 2 Inserir [2, 4, 9, 9, 1] Tamanho do ARR: 3 Valor de inserção: 9 em 3 Inserir [2, 4, 9, 9, 1] Tamanho do ARR: 4 Inserir [2, 4, 9, 9, 1] Tamanho do ARR: 3 Inserir [2, 4, 9, 9, 1] Tamanho do ARR: 2 Inserir [2, 4, 9, 9, 1] Tamanho do ARR: 1 Inserir [2, 4, 9, 9, 1] Tamanho do ARR: 0 Valor de inserção: 1 em 0 Inserção Valor: 2 AT 1 Valor de inserção: 4 AT 2 Valor de inserção: 9 AS 3 Valor de inserção: 9 em 4 Espero que você tenha uma melhor compreensão agora, continue com a série Code101 e você terá uma melhor compreensão. #Happycoding
Fonte