Advertisement
Guest User

ordenar_enlazar.cpp

a guest
Aug 18th, 2010
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1. //Programa para ordenacion de datos
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <cstdlib>
  6. #include <vector>
  7. #include <iterator>
  8. #include <algorithm>
  9.  
  10. using namespace std;
  11.  
  12. int main() {
  13.  
  14.     system ("clear");
  15.  
  16.     ifstream label1 ("datos.in");
  17.  
  18.     int n, i, j, k;
  19.  
  20.     int *a, *b, *index1, *index2;
  21.  
  22.     label1 >> n;  // numero de datos
  23.  
  24.     a = new int [n]; b = new int [n]; index1 = new int [n]; index2 = new int [n];
  25.  
  26.     for (i=0; i < n; i++){
  27.  
  28.         index1[i] = 0;
  29.  
  30.     }
  31.  
  32.     cout << "\n";
  33.  
  34.     for (i=0; i < n; i++){
  35.  
  36.         label1 >> a[i];
  37.  
  38.         b[i] = a[i];
  39.  
  40.     }
  41.  
  42.     vector<int> unVector_no_orden (b, b+n);
  43.     vector<int> unVector_orden (a, a+n);
  44.     vector<int> unIndex1(index1, index1+n);
  45.     vector<int> unIndex2(index2, index2+n);
  46.  
  47.     cout << "Serie desordenada de valores\n";
  48.  
  49.     copy (unVector_no_orden.begin(), unVector_no_orden.end(), ostream_iterator<int> (cout, " "));
  50.  
  51.     cout << "\n";
  52.  
  53.     sort (unVector_orden.begin(), unVector_orden.begin()+n);
  54.  
  55.     cout << "Serie ordenada de valores\n";
  56.  
  57.     copy (unVector_orden.begin(), unVector_orden.end(), ostream_iterator<int> (cout, " "));
  58.  
  59.     cout << "\n";
  60.  
  61.     k = 1;
  62.  
  63.     for (i=0; i < n; i++){
  64.  
  65.         unIndex2[i] = k;
  66.  
  67.         if (unVector_orden[i] == unVector_orden[i + 1]) {k += 0;}
  68.  
  69.             else {k += 1;}
  70.  
  71.     }
  72.  
  73.     cout << "En la serie ordenada asigna un orden repitiendo valores si es necesario\n";
  74.  
  75.     copy (unIndex2.begin(), unIndex2.end(), ostream_iterator<int> (cout, " "));
  76.  
  77.     cout << "\n";
  78.  
  79.     cout << "Enlaza los ordenes de la serie ordenada a la serie desordenada\n";
  80.  
  81.     // Funciona pero lo ideal seria que no recorriese nuevamente la lista sino que descartara
  82.     // los ya verificados
  83.  
  84.     for (i=0; i < n; i++){
  85.  
  86.         for (j=0; j < n; j++){
  87.  
  88.             if(unVector_no_orden[j] == unVector_orden[i]) unIndex1[j] = unIndex2[i];
  89.  
  90.         }
  91.  
  92.     }
  93.  
  94.     copy (unIndex1.begin(), unIndex1.end(), ostream_iterator<int> (cout, " "));
  95.  
  96.     cout << "\n";
  97.  
  98.     delete a, b, index1, index2;
  99.  
  100.     return 0;
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement