Advertisement
Dani_info

Untitled

May 8th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.79 KB | None | 0 0
  1. //Bouble sort
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. int main() {
  8.     int n;
  9.     cout << "Cate elemente are vectorul?"; cin >> n;
  10.     int v[20];
  11.     cout << "Introdu elementele vectorului:" << endl;
  12.     for (int i = 0; i < n; i++)
  13.         cin >> v[i];
  14.  
  15.     int ok, i = 0;
  16.     do {
  17.         ok = 1;
  18.         for (int j = 0; j < n - i - 1; j++) {
  19.             if (v[j] > v[j + 1]) {
  20.                 swap(v[j], v[j + 1]);
  21.                 ok = 0;
  22.             }
  23.         }
  24.         i++;
  25.     } while (!ok);
  26.     for (int i = 0; i < n; i++)
  27.         cout << v[i] << " ";
  28.     return 0;
  29.  
  30. }
  31. ----------------------------------------------------------
  32. //Insertion Sort
  33. #include <iostream>
  34.  
  35. using namespace std;
  36.  
  37.  
  38. int main() {
  39.     int n;
  40.     cout << "Cate elemente are vectorul?"; cin >> n;
  41.     int v[20];
  42.     cout << "Introdu elementele vectorului:" << endl;
  43.     for (int i = 0; i < n; i++)
  44.         cin >> v[i];
  45.    
  46.     for (int i = 1; i < n; i++) {
  47.         int ind = i - 1;
  48.         int arr = v[i];
  49.         while (ind >= 0 && arr < v[ind]) {
  50.             v[ind + 1] = v[ind];
  51.             ind--;
  52.         }
  53.         v[ind + 1] = arr;
  54.     }
  55.     for (int i = 0; i < n; i++)
  56.         cout << v[i] << " ";
  57.     return 0;
  58.  
  59. }
  60. ----------------------------------------------------------
  61. //Selction sort
  62. #include <iostream>
  63.  
  64. using namespace std;
  65.  
  66.  
  67. int main() {
  68.     int n;
  69.     cout << "Cate elemente are vectorul?"; cin >> n;
  70.     int v[20];
  71.     cout << "Introdu elementele vectorului:" << endl;
  72.     for (int i = 0; i < n; i++)
  73.         cin >> v[i];
  74.    
  75.     for (int m = n - 1; m >= 0; m--) {
  76.         int nmax = v[0];
  77.         int ind = 0;
  78.         for (int i = 0; i <= m; i++) {
  79.             if (v[i] > nmax) {
  80.                 nmax = v[i];
  81.                 ind = i;
  82.             }
  83.         }
  84.         swap(v[m], v[ind]);
  85.     }
  86.     for (int i = 0; i < n; i++)
  87.         cout << v[i] << " ";
  88.     return 0;
  89.  
  90. }
  91. ----------------------------------------------------------
  92. //Counting sort
  93. #include <iostream>
  94.  
  95. using namespace std;
  96.  
  97.  
  98. int main() {
  99.     int n;
  100.     cout << "Cate elemente are vectorul?"; cin >> n;
  101.     int v[20];
  102.     cout << "Introdu elementele vectorului:" << endl;
  103.     for (int i = 0; i < n; i++)
  104.         cin >> v[i];
  105.    
  106.     int c[10], b[10];
  107.     for (int i = 0; i < n; i++)
  108.         b[i] = 0;
  109.     for (int i = 0; i < n - 1; i++) {
  110.         for (int j = i + 1; j < n; j++) {
  111.             if (v[i] > v[j])
  112.                 b[i]++;
  113.             else
  114.                 b[j]++;
  115.         }
  116.     }
  117.     for (int i = 0; i < n; i++)
  118.         c[b[i]] = v[i];
  119.     for (int i = 0; i < n; i++)
  120.         cout << c[i] << " ";
  121.     return 0;
  122.  
  123. }
  124. ----------------------------------------------------------
  125. //Interclasare
  126. #include <iostream>
  127.  
  128. using namespace std;
  129.  
  130.  
  131. int main() {
  132.     int n;
  133.     cout << "Cate elemente are vectorul?"; cin >> n;
  134.     int v[20];
  135.     cout << "Introdu elementele vectorului:" << endl;
  136.     for (int i = 0; i < n; i++)
  137.         cin >> v[i];
  138.    
  139.     int x, y, a[10], b[10], c[20], k = 0;
  140.     cout << "Cate elemente are primul vector??"; cin >> x;
  141.     cout << "Introduceti elemntele primului vector:" << endl;
  142.     for (int i = 0; i < x; i++) cin >> a[i];
  143.     cout << "Cate elemente are cel de-al doilea vector??"; cin >> y;
  144.     cout << "Introdu elemntele celui de-al doilea vector:" << endl;
  145.     for (int i = 0; i < y; i++) cin >> b[i];
  146.     int i = 0, j = 0;
  147.     while (i < x && j < y) {
  148.         if (a[i] > b[j])
  149.             c[k++] = b[j++];
  150.         else
  151.             c[k++] = a[i++];
  152.     }
  153.  
  154.     for (int d = i; d < x; d++)
  155.         c[k++] = a[d];
  156.     for (int e = j; e < y; e++)
  157.         c[k++] = b[e];
  158.     for (i = 0; i < x + y; i++)
  159.         cout << c[i] << " ";
  160.     return 0;
  161.  
  162. }
  163.  
  164. ----------------------------------------------------------
  165. //Cautare binara
  166. #include <iostream>
  167.  
  168. using namespace std;
  169.  
  170.  
  171. int main() {
  172.     int n,a[100],st,dr,m,i,gasit = 0, x;
  173.     cout<<"n=";
  174.     cin>>n;
  175.     cout<<"Elementele vectorului sunt";
  176.     for(i = 0; i < n ; i++)
  177.         cin>>a[i];
  178.     cout<<"Introduceti elementul cautat";
  179.     cin>>x;
  180.     st = 0;
  181.     dr = n-1;
  182.     while(st <= dr && !gasit)
  183.     {
  184.         m = (st + dr) / 2;
  185.         if(a[m] == x)
  186.             gasit = 1;
  187.         else if(x < a[m])
  188.             dr = m - 1;
  189.         else
  190.             st = m + 1:
  191.     }
  192.     if(gasit)
  193.         cout<<"Elementul se afla in sir";
  194.     else
  195.         cout<<"Elementul nu se afla in sir";
  196.     return 0;
  197.  
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement