Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
Swap two elements of an array function

Can you help me by telling me what is wrong with this? Why is the swap function not working?

void swap(int a[], int b, int c) {
    int temp = a[b];
    a[b] = a[c];
    a[b] = temp;
}

void bubble1 (int a[], int N){
    int i;
    for(i=0;i<N-1;i++){
        if(a[i]>a[i+1]){
            swap(a,i,i+1);
        }
    }
}


void main() {
    int N = 11;
    int a[12]={5,3,12,4,25,10,14,35,2,8,13};

    bubble1 (a,N);

    int i;
    for(i = 0; i < N; i++){
        printf("%d\n",a[i]);
    }
}

If I don't use the swap function and do the swapping manually in the "bubble" function it works. However if I use the swap it doesn't work, even though it's exactly the same. What am I doing wrong here?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

 int temp = a[b];
 a[b] = a[c];
 a[b] = temp;

Simple typo, you are assigning to a[b] twice. The second one should be a[c]

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda