Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: risiko exercise implemented #301

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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