eu preciso fazer QuickSort em uma matriz o maximo que consegui foi isso, por favor me deem dicas

8 views
Skip to first unread message

igor ramon

unread,
May 21, 2020, 4:06:09 PM5/21/20
to Fórum - De Aluno Para Aluno

public class Particao {
  public static void main (String [] args) {
    float [][] v = {{0.1f, 0.2f, 0.1f, 0.2f, 0.1f},
                    {0.1f, 0.2f, 0.3f, 0.1f, 0.1f},
                    {0.2f, 0.3f, 0.1f,0.1f, 0.3f},
                    {0.4f, 0.1f, 0.1f,0.1f, 0.2f},
                    {0.2f, 0.2f, 0.3f, 0.3f, 0.1f},
    };
    
    float p = particao (v);
    System.out.println ("Posição:" + p);
    
    
    
    
    for (int i = 0; i < v.length; i ++) {
        System.out.println("");
        for (int g = 0; g < v[i].length; g++) {
      System.out.print ("["+v [i][g] + "]");
        }
    }
  }

  static int particao (float v [][]) {
    float pivo = v [0][0];
    int k = 0;
    int l = v.length-1;
    int i = 1;
    int j = v.length- 1;
    while (i <= j && k >= l) {
      if (v [i][l] <= pivo) {
        i ++; // sobe oi
        l--;
      }
      else if (v [j][k]> pivo) {
        j--;
        k++;// desce oj
      }
      else {
        // troca
  float temp = v[k][l];      
           v[k][l]= v[i][j];
           v[i][j]= temp;
        i ++; k++;
        j--; l--;
      }
    }
    v [0][0] = v [i][j];
    v [j][i]  = pivo;
    return j;
  }
}

Filipe Simoes

unread,
May 22, 2020, 7:48:13 PM5/22/20
to Fórum - De Aluno Para Aluno
Boa noite, 
Igor eu não estudo JAVA e sim C mas, fiz um programa parecido com a sua ideia, espero que sirva de alguma ajuda.

#include <stdio.h>
int main()
{
    int vetor[10] = {1, 7, 4, 3, 5, 6, 9, 2, 0, 8};
    int i;

    void ordemcrecente(int vetor[10], int n);
    ordemcrecente(vetor, 10);

    printf("\n  ");
    for(i = 0; i < 10; i = i + 1)
        {
            printf("%d, ", vetor[i]);
        }
    printf("\n\n");
    system("pause");
return 0;
}
void ordemcrecente(int vetor[10], int n)
{
    int a, b, temporaria;

    for(a = 0; a < n; a = a + 1)
        {
           for(b = a + 1; b < n; b = b + 1)
           {
                if(vetor[a] > vetor[b])
                    {
                        temporaria = vetor[a];
                        vetor[a] = vetor[b];
                        vetor[b] = temporaria;
                    }
           }
        }
}
Reply all
Reply to author
Forward
0 new messages