forked from codemistic/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13_IsSorted .cpp
44 lines (41 loc) · 1003 Bytes
/
13_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
#include<iostream>
using namespace std;
struct Array
{
int* A;
int size;
int length;
};
void Display(struct Array* arr)
{
int i;
cout << "the elements of the array is " << endl;
for (i = 0; i < arr->length; i++)
cout <<arr->A[i] << " ";
}
int IsSorted(struct Array* arr)
{
for (int i = 0; i < arr->length - 1; i++)
{
if (arr->A[i] > arr->A[i + 1]) // if 1st element is greater then 2nd element then it will k/as not sorted so return 0
return 0;
}
return 1; // if everything is ok and the element came successfully out of loop means it is sorted return 1 for thet
}
int main()
{
struct Array arr;
cout << "Enter the size of Array " << endl;
cin >> arr.size;
arr.A = new int[arr.size];
int no;
cout << "Enter the length of the array " << endl;
cin >> no;
arr.length = 0;
cout << "Enter the elements of the array " << endl;
for (int i = 0; i < no; i++)
cin >> arr.A[i];
arr.length = no;
cout<<IsSorted(&arr);
return 0;
}