-
Notifications
You must be signed in to change notification settings - Fork 33
/
ShivalikaGoel.cpp
120 lines (110 loc) · 1.77 KB
/
ShivalikaGoel.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include<iostream>
#include<cstring>
using namespace std;
int chartoint(string str,int arr[])
{
int n=str.length();
// cout<<n<<endl;
int j=0,i=0;
while(i!=n)
{
// cout<<i<<" "<<j<<endl;
if(str[i]!=' ')
{
// cout<<str[i]<<endl;
if(str[i]=='-')
{
i=i+1;
// cout<<str[i]<<endl;
int num=0;
while(str[i]!=' '&&i!=n)
{
// cout<<num<<endl;
num=num*10+(str[i]-'0');
i++;
}
// cout<<num<<endl;
arr[j]=-num;
j++;
}
else
{
int num=0;
while(str[i]!=' '&&i!=n)
{
num=num*10+(str[i]-'0');
i++;
}
// cout<<num<<endl;
arr[j]=num;
j++;
}
// cout<<arr[j-1]<<endl;
}
else
i++;
}
return j;
}
void merger(int arr[],int s,int mid,int e)
{
int l1=mid-s+1;
int l2=e-mid;
int arr1[l1];
int arr2[l2];
for(int i=0;i<l1;i++)
arr1[i]=arr[s+i];
for(int i=0;i<l2;i++)
arr2[i]=arr[mid+1+i];
int i=0,j=0,k=s;
while(i<l1&&j<l2)
{
if (arr1[i]<=arr2[j])
{
arr[k]=arr1[i];
i++;
k++;
}
else
{
arr[k]=arr2[j];
j++;
k++;
}
}
while(i<l1)
{
arr[k] = arr1[i];
i++;
k++;
}
while(j<l2)
{
arr[k] = arr2[j];
j++;
k++;
}
}
void mergesort(int arr[],int i,int j)
{
if(i<j)
{
int mid=(j+i)/2;
mergesort(arr, i, mid);
mergesort(arr, mid+1, j);
merger(arr,i,mid,j);
}
}
int main(int argc,char*argv[])
{
string s1,s2;
s1=argv[1];
s2=argv[2];
int arr[100];
int n=chartoint(s2,arr);
// for(int i=0;i<n;i++)
// cout<<arr[i]<<" ";
mergesort(arr,0,n-1);
for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
}