Advertisement
Guest User

Untitled

a guest
May 21st, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int n, a[1000];
  6.  
  7. int maxim(int start, int end) {
  8.     if (start == end) return a[start];
  9.     return max(maxim(start, start+(end-start)/2), maxim(start+(end-start)/2+1, end));
  10. }
  11. int minim(int start, int end) {
  12.     if (start == end) return a[start];
  13.     return min(minim(start, start+(end-start)/2), minim(start+(end-start)/2+1, end));
  14. }
  15. int suma(int start, int end) {
  16.     if (start == end) return a[start];
  17.     return suma(start, start+(end-start)/2) + suma(start+(end-start)/2+1, end);
  18. }
  19. void scrie(int start, int end) {
  20.     if (start == end) {
  21.         cout << a[start] << " ";
  22.         return ;
  23.     }
  24.     scrie(start, start+(end-start)/2);
  25.     scrie(start+(end-start)/2+1, end);
  26. }
  27. void quicksort(int st, int dr) { // crescator !!!!!
  28.     int i, j, m;
  29.     i = st;
  30.     j = dr;
  31.     m = a[(i+j)/2];
  32.     while (i < j) {
  33.         while (a[i] < m) i++;
  34.         while (a[j] > m) j--;
  35.         if (i <= j) {
  36.             swap(a[i], a[j]);
  37.             i++;
  38.             j--;
  39.         }
  40.     }
  41.     if (st < j) quicksort(st, j);
  42.     if (i < dr) quicksort(i, dr);
  43. }
  44. void quicksortd(int st, int dr) { // descrescator !!!!!
  45.     int i, j, m;
  46.     i = st;
  47.     j = dr;
  48.     m = a[(i+j)/2];
  49.     while (i < j) {
  50.         while (a[i] > m) i++;
  51.         while (a[j] < m) j--;
  52.         if (i <= j) {
  53.             swap(a[i], a[j]);
  54.             i++;
  55.             j--;
  56.         }
  57.     }
  58.     if (st < j) quicksortd(st, j);
  59.     if (i < dr) quicksortd(i, dr);
  60. }
  61.  
  62. int main() {
  63.  
  64.     cout << "Dati n: "; cin >> n;
  65.     cout << "Dati elementele tabloului: ";
  66.     for (int i = 1; i <= n; i++) {
  67.         cin >> a[i];
  68.     }
  69.     cout << "Avem elementele tabloului: "; scrie(1, n); cout << endl;
  70.     cout << "Maximul din tablou este: " << maxim(1, n) << endl;
  71.     cout << "Minimul din tablou este: " << minim(1, n) << endl;
  72.     cout << "Suam elementelor din tablou este: " << suma(1, n) << endl;
  73.     cout << "Avem elementele tabloului sortate descrescator: "; quicksortd(1, n); scrie(1, n); cout << endl;
  74.  
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement