-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathisSorted.cpp
66 lines (53 loc) · 1.08 KB
/
isSorted.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
61
62
63
64
65
66
#include <iostream>
using namespace std;
/*
Check if an array is sorted in increasing order
ip: [8, 12, 15]
op: yes
ip: [100]
op: yes
ip: [100, 20, 200]
op: no
Naive Approach:
Traverse array from left to right
for each element check that
There should not be any smaller element on right side
Time:
worst case: arr is sorted, Theta(n^2)
best case: arr is sorted in descending, Theta(n)
Efficient Approach:
Traverse array from left to right
If elements are out of order we say array is sorted
i.e if I am at ith element,
this means elements from 0 to ith index should be sorted
Time: O(n)
*/
bool isSorted(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
for (int j = i+1; j < n; j++)
{
if (arr[j] < arr[i])
{
return false;
}
}
}
return true;
}
bool eff_isSorted(int arr[], int n)
{
for (int i = 1; i < n; i++)
{
if(arr[i] < arr[i-1])
return false;
}
return true;
}
int main()
{
int arr[] = {5, 12, 30, 40};
cout << eff_isSorted(arr, 4);
return 0;
}