-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday1_1.cpp
60 lines (60 loc) · 1.01 KB
/
day1_1.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
56
57
58
59
60
#include<iostream>
#include<malloc.h>
using namespace std;
void swap(int *a,int *b) // swapping positions of 2 elements.
{
int t = *a;
*a = *b;
*b = t;
}
void sortAscending(int a[],int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1]) // checking if the next element is smaller or not.
swap(&a[j],&a[j+1]);
}
}
for(int i=0;i<n;i++)
{
cout << a[i] << " ";
}
cout << endl;
}
void sortDescending(int a[],int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(a[j]<a[j+1]) // checking if the next element is greater or not.
swap(&a[j],&a[j+1]);
}
}
for(int i=0;i<n;i++)
{
cout << a[i] << " ";
}
cout << endl;
}
int main()
{
int n;
cin >> n;
int *a;
a = (int *)malloc(sizeof(int)*n);
for(int i=0;i<n;i++)
{
cin >> a[i];
}
sortAscending(a,n); // calling ascending function.
sortDescending(a,n); // calling descending function.
return 0;
}
/*
Time Complexity = O(n^2)
Space Complexity = O(1)
By : Akshat Jain
*/