Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- public class Mergesort {
- public static void main(String[] args) {
- Scanner s = new Scanner(System.in);
- int quantEntrada = Integer.parseInt(s.nextLine());
- String[] vetor = s.nextLine().split("\\|");
- int[] aux1 = new int[quantEntrada];
- int[] aux = new int[quantEntrada];
- System.out.println(Arrays.toString(msort(conversao(vetor,aux), aux1,0, quantEntrada -1)));
- }
- public static int[] conversao(String [] vetor, int[] aux){
- int j = 0;
- for (String a : vetor) {
- aux[j++] = Integer.parseInt(a);
- }
- return aux;
- }
- public static int[] msort(int[] vetor, int[] aux, int valorI, int valorF) {
- if (valorI < valorF) {
- int meio = (valorI + valorF)/2;
- msort(vetor, aux, valorI, meio);
- msort(vetor, aux,meio + 1, valorF);
- intercalacao(vetor, aux, valorI, meio, valorF);
- }
- return vetor;
- }
- public static void intercalacao(int[] vetor, int[]aux, int valorI, int meio, int valorF) {
- int i = valorI;
- int j = meio + 1;
- for (int count = valorI; count <= valorF; count++)
- aux[count] = vetor[count];
- for (int count1 = valorI; count1 <= valorF; count1++) {
- if (i > meio) {
- vetor[count1] = aux[j++];
- } else if (j > valorF) {
- vetor[count1] = aux[i++];
- } else if (aux[i] > aux[j]) {
- vetor[count1] = aux[j++];
- } else {
- vetor[count1] = aux[i++];
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement