Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int n, a[1000];
- int maxim(int start, int end) {
- if (start == end) return a[start];
- return max(maxim(start, start+(end-start)/2), maxim(start+(end-start)/2+1, end));
- }
- int minim(int start, int end) {
- if (start == end) return a[start];
- return min(minim(start, start+(end-start)/2), minim(start+(end-start)/2+1, end));
- }
- int suma(int start, int end) {
- if (start == end) return a[start];
- return suma(start, start+(end-start)/2) + suma(start+(end-start)/2+1, end);
- }
- void scrie(int start, int end) {
- if (start == end) {
- cout << a[start] << " ";
- return ;
- }
- scrie(start, start+(end-start)/2);
- scrie(start+(end-start)/2+1, end);
- }
- void quicksort(int st, int dr) { // crescator !!!!!
- int i, j, m;
- i = st;
- j = dr;
- m = a[(i+j)/2];
- while (i < j) {
- while (a[i] < m) i++;
- while (a[j] > m) j--;
- if (i <= j) {
- swap(a[i], a[j]);
- i++;
- j--;
- }
- }
- if (st < j) quicksort(st, j);
- if (i < dr) quicksort(i, dr);
- }
- void quicksortd(int st, int dr) { // descrescator !!!!!
- int i, j, m;
- i = st;
- j = dr;
- m = a[(i+j)/2];
- while (i < j) {
- while (a[i] > m) i++;
- while (a[j] < m) j--;
- if (i <= j) {
- swap(a[i], a[j]);
- i++;
- j--;
- }
- }
- if (st < j) quicksortd(st, j);
- if (i < dr) quicksortd(i, dr);
- }
- int main() {
- cout << "Dati n: "; cin >> n;
- cout << "Dati elementele tabloului: ";
- for (int i = 1; i <= n; i++) {
- cin >> a[i];
- }
- cout << "Avem elementele tabloului: "; scrie(1, n); cout << endl;
- cout << "Maximul din tablou este: " << maxim(1, n) << endl;
- cout << "Minimul din tablou este: " << minim(1, n) << endl;
- cout << "Suam elementelor din tablou este: " << suma(1, n) << endl;
- cout << "Avem elementele tabloului sortate descrescator: "; quicksortd(1, n); scrie(1, n); cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement