1
2// STL IN C++ FOR SORING
3#include <bits/stdc++.h>
4#include <iostream>
5using namespace std;
6int main()
7{
8 int arr[] = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0};
9 int n = sizeof(arr)/sizeof(arr[0]);
10 sort(arr, arr+n); // ASCENDING SORT
11 reverse(arr,arr+n); //REVERESE ARRAY
12 sort(arr, arr + n, greater<int>());// DESCENDING SORT
13 }
1// C++ program to demonstrate default behaviour of
2// sort() in STL.
3#include <bits/stdc++.h>
4using namespace std;
5
6int main()
7{
8 int arr[] = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0};
9 int n = sizeof(arr)/sizeof(arr[0]);
10
11 sort(arr, arr+n);
12
13 cout << "\nArray after sorting using "
14 "default sort is : \n";
15 for (int i = 0; i < n; ++i)
16 cout << arr[i] << " ";
17
18 return 0;
19}
1#include <bits/stdc++.h>
2using namespace std;
3
4#define size(arr) sizeof(arr)/sizeof(arr[0]);
5
6
7int main(){
8
9 int a[5] = {5, 2, 6,3 ,5};
10 int n = size(a);
11 sort((a), a + n);
12 for(int i = 0; i < n; i++){
13 cout << a[i];
14 }
15
16
17 return 0;
18
19}
20
1#include <iostream>
2using namespace std;
3
4#define MAX 100
5
6int main()
7{
8 //array declaration
9 int arr[MAX];
10 int n,i,j;
11 int temp;
12
13 //read total number of elements to read
14 cout<<"Enter total number of elements to read: ";
15 cin>>n;
16
17 //check bound
18 if(n<0 || n>MAX)
19 {
20 cout<<"Input valid range!!!"<<endl;
21 return -1;
22 }
23
24 //read n elements
25 for(i=0;i<n;i++)
26 {
27 cout<<"Enter element ["<<i+1<<"] ";
28 cin>>arr[i];
29 }
30
31 //print input elements
32 cout<<"Unsorted Array elements:"<<endl;
33 for(i=0;i<n;i++)
34 cout<<arr[i]<<"\t";
35 cout<<endl;
36
37 //sorting - ASCENDING ORDER
38 for(i=0;i<n;i++)
39 {
40 for(j=i+1;j<n;j++)
41 {
42 if(arr[i]>arr[j])
43 {
44 temp =arr[i];
45 arr[i]=arr[j];
46 arr[j]=temp;
47 }
48 }
49 }
50
51 //print sorted array elements
52 cout<<"Sorted (Ascending Order) Array elements:"<<endl;
53 for(i=0;i<n;i++)
54 cout<<arr[i]<<"\t";
55 cout<<endl;
56
57
58 return 0;
59
60}
61
1sort(arr, arr+length); //increase
2sort(arr, arr+length, greater<int>()); //decrease
1#include <iostream>
2using namespace std;
3int main () {
4
5 int n,i,j,temp;
6 cout << "how many Arrays you wanna sort? ";
7 cin >> n;
8 cout << endl << endl;
9 int a[n];
10 for(i = 0;i < n; i++){
11 cout << "please enter your " << i + 1 << " array : ";
12 cin >> a[i];
13 }
14 for(i = 0; i < n; i++){
15 for(j = 0; j < n - 1; j++){
16 if( a[j] > a[j+1]){
17 temp = a[j];
18 a[j] = a[j+1];
19 a[j+1] = temp;
20 }
21 }
22 }
23
24 cout << "\nArray after sorting using default sort is : \n";
25 for (i = 0; i < n; i++)
26 cout << a[i] << '\t';
27
28 return 0;
29}