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
1#include<iostream>
2using namespace std;
3
4int main(int argc, char const *argv[])
5{
6 int numb[7];
7 int i, j;
8
9 for(i=0;i<=6;i++)
10 {
11 cout << "Enter a number" << endl;
12 cin >> numb[i];
13 }
14
15 for (i=0;i<=5;i++)
16 {
17 for (j=i+1;j<=5;j++)
18 {
19 int temp;
20
21 if (numb[i] > numb[j])
22 {
23 temp = numb[i];
24 numb[i] = numb[j];
25 numb[j] = temp;
26 }
27 }
28 }
29 for (i=0;i<=6;i++)
30 {
31 cout << endl << numb[i] << endl;
32 }
33}
34
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}
1#include <algorithm> // std::sort
2
3int myints[] = {32,71,12,45,26,80,53,33};
4// using default comparison (operator <):
5std::sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33
6
7// fun returns some form of a<b
8std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)
1// sort algorithm example
2#include <iostream> // std::cout
3#include <algorithm> // std::sort
4#include <vector> // std::vector
5
6bool myfunction (int i,int j) { return (i<j); }
7
8struct myclass {
9 bool operator() (int i,int j) { return (i<j);}
10} myobject;
11
12int main () {
13 int myints[] = {32,71,12,45,26,80,53,33};
14 std::vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33
15
16 // using default comparison (operator <):
17 std::sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33
18
19 // using function as comp
20 std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)
21
22 // using object as comp
23 std::sort (myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80)
24
25 // print out content:
26 std::cout << "myvector contains:";
27 for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
28 std::cout << ' ' << *it;
29 std::cout << '\n';
30
31 return 0;
32}