Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

129
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda