vector ordenado c++ code example

Example: ordenar un vector

//de menor a mayor
void ordenar(int v[], int size){
  int i, j, aux;

  //ALGORITMO DE BURBUJA
  for (i = 0; i < size - 1; i++){
     for (j = i + 1; j < size; j++){
      if(v[i] > v[j]){
       aux = v[i];
       v[i] = v[j];
       v[j] = aux;
      }
    }
  }

  printf("\n");
  for(int i = 0; i <= size - 1; i++){
    printf("vector[%d]: %d\n", i, v[i]);
  }
}

//de mayor a menor
void ordenar_mayor_menor(int *v, int tam){
	int i, j, aux;

	for(i = 1; i < tam; i++){
		for(j = tam - 1; j >= i; j--){
			if(v[j] > v[j-1]){
				aux = v[j];
				v[j] = v[j-1];
				v[j-1] = aux;
			}
		}
	}
}

Tags:

C Example