-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathSortAlgo.cpp
55 lines (40 loc) · 1.32 KB
/
SortAlgo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// A C++ program to demonstrate working of sort(),
// reverse()
#include <algorithm>
#include <iostream>
#include <vector>
#include <numeric> //For accumulate operation
using namespace std;
int main()
{
// Initializing vector with array values
int arr[] = {10, 20, 5, 23 ,42 , 15};
int n = sizeof(arr)/sizeof(arr[0]);
vector<int> vect(arr, arr+n);
cout << "Vector is: ";
for (int i=0; i<n; i++)
cout << vect[i] << " ";
// Sorting the Vector in Ascending order
sort(vect.begin(), vect.end());
cout << "\nVector after sorting is: ";
for (int i=0; i<n; i++)
cout << vect[i] << " ";
// Sorting the Vector in Descending order
sort(vect.begin(),vect.end(), greater<int>());
cout << "\nVector after sorting in Descending order is: ";
for (int i=0; i<n; i++)
cout << vect[i] << " ";
// Reversing the Vector (descending to ascending , ascending to descending)
reverse(vect.begin(), vect.end());
cout << "\nVector after reversing is: ";
for (int i=0; i<n; i++)
cout << vect[i] << " ";
cout << "\nMaximum element of vector is: ";
cout << *max_element(vect.begin(), vect.end());
cout << "\nMinimum element of vector is: ";
cout << *min_element(vect.begin(), vect.end());
// Starting the summation from 0
cout << "\nThe summation of vector elements is: ";
cout << accumulate(vect.begin(), vect.end(), 0);
return 0;
}