SHARE
TWEET

Heap sort

a guest Jan 27th, 2020 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream f("date.in");
  6. ofstream g("date.out");
  7.  
  8. struct node{
  9.     int inf;
  10.     node *urm, *ant;
  11. };
  12.  
  13. node *prim, *ultim;
  14.  
  15. void creare(int x){
  16.     prim = new node;
  17.     prim -> inf = x;
  18.     prim -> urm = NULL;
  19.     prim -> ant = NULL;
  20.     ultim = prim;
  21. }
  22.  
  23. void afisare(){
  24.     node *p = prim;
  25.     while(p != NULL){
  26.         g << p -> inf << " ";
  27.         p = p -> urm;
  28.     }
  29. }
  30.  
  31. void inseraref(int x){
  32.     node *p = new node;
  33.     p -> inf = x;
  34.     p -> ant = NULL;
  35.     p -> urm = prim;
  36.     prim = p;
  37. }
  38.  
  39. void inserareb(int x){
  40.     node *p = new node;
  41.     p -> inf = x;
  42.     p -> ant = ultim;
  43.     p -> urm = NULL;
  44.     ultim -> urm = p;
  45.     ultim = p;
  46. }
  47.  
  48. void inserare(int x){
  49.     node *p = prim;
  50.     while(p != NULL){
  51.         if(p -> inf <= x && p -> urm -> inf >= x){
  52.             node *t = new node;
  53.             t -> inf = x;
  54.             t -> ant = p;
  55.             t -> urm = p -> urm;
  56.             p -> urm = t;
  57.             break;
  58.         }
  59.         p = p -> urm;
  60.     }
  61. }
  62.  
  63. int main(){
  64.     int x;
  65.     f >> x;
  66.     creare(x);
  67.  
  68.     while(f >> x){
  69.         if(x < prim -> inf)
  70.             inseraref(x);
  71.         else
  72.             if(x > ultim -> inf)
  73.                 inserareb(x);
  74.             else
  75.                 inserare(x);
  76.     }
  77.  
  78.     afisare();
  79.     return 0;
  80. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top