Skip to content

Conversation

AndreaSeminara
Copy link

No description provided.

Comment on lines +49 to +51
for(int j = 0; j < n; j++){

if(A[i] > A[j]) swap(A,i,j);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Immagino la task non riguardasse implementare un buon algoritmo di ordinamento, ma non posso non notare che bastano due cambiamenti per migliorare le prestazioni.
In questo momento ci sono dei casi in cui $i = j$, per cui compari un elemento con sè stesso, Per di più, ogni volta scorri l'array per intero nel loop interno.

// Variante sort
void sort(int *A, int n) {
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      // Se il nuovo elemento A[j] è più grande di A[i]
      // allora scambiamo i due elementi 
      // per mettere A[j] in cima (a sinistra)
      if (A[j] > A[i]) swap(A, i, j);
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants