-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday16_sorting.cpp
60 lines (55 loc) · 1.12 KB
/
day16_sorting.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 <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
template <class T>
void merge_sort(T array[],int beg, int end){
if (beg==end){
return;
}
int mid = (beg+end)/2;
merge_sort(array,beg,mid);
merge_sort(array,mid+1,end);
int i=beg,j=mid+1;
int l=end-beg+1;
T *temp = new T [l];
for (int k=0;k<l;k++){
if (j>end || (i<=mid && array[i]<array[j])){
temp[k]=array[i];
i++;
}
else{
temp[k]=array[j];
j++;
}
}
for (int k=0,i=beg;k<l;k++,i++){
array[i]=temp[k];
}
delete temp;
}
int main() {
long int n,i,j,d,temp,inner_d;
long long int answer=1000000000000;
cin>>n;
long int A[n];
for(int k=0;k<n;k++){
cin>>A[k];
}
merge_sort(A,0,n-1);
for(int l=0;l<n;l++){
inner_d=abs(A[l]-A[l+1]);
if(inner_d<answer){
answer=inner_d;
}
}
for(int b=0;b<n;b++){
int d=abs(A[b]-A[b+1]);
if(d==answer){
cout<<A[b]<<" "<<A[b+1]<<" ";
}
}
return 0;
}