-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccurateSorting.c
48 lines (43 loc) · 1.04 KB
/
AccurateSorting.c
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
#include <stdio.h>
#include <stdlib.h>
//Accurate Sorting.
//The array elements can exchange their positions with their adjacent position elements if:
// the difference between the values is 1.
//If the sorted array is in the Ascending order, print YES, else No.
int main()
{
int sortArray[10];
int count=0;
int decider=0;
printf("Enter the number of elements in the array");
scanf("%d",&count);
for(int i=0;i<count;i++)
{
scanf("%d",&sortArray[i]);
}
for(int i=0;i<(count-1);i++)
{
if(sortArray[i]-sortArray[i+1]==1 || sortArray[i]-sortArray[i+1]==-1)
{
if(sortArray[i]-sortArray[i+1]==1)
{
int temp;
temp=sortArray[i+1];
sortArray[i+1]=sortArray[i];
sortArray[i]=temp;
}
}
}
for(int i=0;i<(count-1);i++)
{
if(sortArray[i]>sortArray[i+1])
{
printf("No");
decider=1;
}
}
if(decider==0)
{
printf("Yes");
}
}