Skip to content

Instantly share code, notes, and snippets.

@vapaternina
Last active January 31, 2016 03:08
Show Gist options
  • Save vapaternina/3b4d7a6b9a2c41a70fbc to your computer and use it in GitHub Desktop.
Save vapaternina/3b4d7a6b9a2c41a70fbc to your computer and use it in GitHub Desktop.
2 vectores, A y B en A se insertaran los que estan en B en su ubicacion correcta.
///--------@Autor: Victor Paternina----------///
///--------@Fecha: 26/01/2016 - 5:28pm-------///
#include <iostream>
using namespace std;
//Subrutina rodar vector
void rodarVector(int vector[], int posicion,int tam){
for(int i=tam-1; i>posicion; i--){
vector[i] = vector[i-1];//Rodar vector 1 espacio a la derecha desde una posicion dada
}
}
//Subrutina insertar elemento
void insertarElemento(int vectorA[],int vectorB[],int tamA,int tamB){
int pos;
for(int i = 0; i<tamA; i++){
for(int j = 0; j<tamB; j++){
if(i == 0 && vectorB[j] < vectorA[i]){//Verificacion de lugar correcto
pos = i;
rodarVector(vectorA,pos,tamA);
vectorA[pos] = vectorB[j];
}
if(i == tamA-1 && vectorB[j]>vectorA[i]){//Verificacion de lugar correcto
pos = i;
vectorA[pos] = vectorB[j];
}
if(vectorA[i]<vectorB[j] && vectorA[i+1]>vectorB[j]){//Verificacion de lugar correcto
pos = i+1;
rodarVector(vectorA,pos,tamA);
vectorA[pos] = vectorB[j];
}
}
}
}
//Escribir vector
void escribirVector(int vector[],int tam){
for(int i=0; i<tam; i++){
cout << vector[i] <<" ";
}
cout << "\n";
}
int main(){
//ejemplo
int n = 9;
int m = 4;
int vectorA[9] = {3,6,8,11,23};
int vectorB[4] = {2,4,7,24};
int posicion;
escribirVector(vectorB,m);
escribirVector(vectorA,n);
insertarElemento(vectorA,vectorB,n,m);
escribirVector(vectorA,n);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment