Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
1
170050034-170050094-170050096-git
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
RAJDEEP YADAV
170050034-170050094-170050096-git
Commits
1ab6f142
Commit
1ab6f142
authored
Aug 26, 2018
by
POORVI HEBBAR
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
change implementation to merge-sort
parent
6a5d2ec6
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
6 deletions
+46
-6
sorting.cpp
sorting.cpp
+46
-6
No files found.
sorting.cpp
View file @
1ab6f142
#include<iostream>
#include<vector>
#include "sorting.h"
using
namespace
std
;
vector
<
int
>
merge
(
vector
<
int
>
arr1
,
vector
<
int
>
arr2
){
int
n1
=
arr1
.
size
(),
n2
=
arr2
.
size
(),
count1
=
0
,
count2
=
0
;
vector
<
int
>
mergedArray
(
n1
+
n2
);
for
(
int
i
=
0
;
i
<
n1
+
n2
;
i
++
){
if
(
count1
>=
n1
){
mergedArray
[
i
]
=
arr2
[
count2
];
count2
++
;
}
else
if
(
count2
>=
n2
){
mergedArray
[
i
]
=
arr1
[
count1
];
count1
++
;
}
else
{
if
(
arr1
[
count1
]
<=
arr2
[
count2
]){
mergedArray
[
i
]
=
arr1
[
count1
];
count1
++
;
}
else
{
mergedArray
[
i
]
=
arr2
[
count2
];
count2
++
;
}
}
}
return
mergedArray
;
}
vector
<
int
>
sort_custom
(
vector
<
int
>
arr
){
int
N
=
arr
.
size
();
for
(
int
i
=
0
;
i
<
N
;
i
++
)
for
(
int
j
=
0
;
j
<
N
-
i
-
1
;
j
++
)
if
(
arr
[
j
]
>
arr
[
j
+
1
])
swap
(
arr
[
j
],
arr
[
j
+
1
]);
int
n
=
arr
.
size
();
if
(
n
==
1
){
return
arr
;
}
else
{
int
n1
=
n
/
2
;
int
n2
=
n
-
n1
;
vector
<
int
>
arr1
(
n1
),
arr2
(
n2
);
for
(
int
i
=
0
;
i
<
n
;
i
++
){
if
(
i
<
n1
)
arr1
[
i
]
=
arr
[
i
];
else
if
(
i
<
n1
+
n2
)
arr2
[
i
-
n1
]
=
arr
[
i
];
}
vector
<
int
>
ans1
=
sort_custom
(
arr1
);
vector
<
int
>
ans2
=
sort_custom
(
arr2
);
vector
<
int
>
ans
=
merge
(
ans1
,
ans2
);
return
ans
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment