-
Notifications
You must be signed in to change notification settings - Fork 3
/
merge-sort.cpp
55 lines (51 loc) · 994 Bytes
/
merge-sort.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
#include <iostream>
#include <merge-sort.h>
using namespace std;
void merge (int vector1[], int inicio1, int fin1, int inicio2, int fin2)
{
int pos1=inicio1, pos2=inicio2, pos3=0;
int * vector2;
vector2= new int [fin2+1];
while (pos1<=fin1 and pos2<=fin2)
{
if(vector1[pos1]<vector1[pos2])
{
vector2[pos3]=vector1[pos1];
pos1++;
}
else
{
vector2[pos3]=vector1[pos2];
pos2++;
}
pos3++;
}
while(pos1<=fin1)
{
vector2[pos3]=vector1[pos1];
pos1++; pos3++;
}
while(pos2<=fin2)
{
vector2[pos3]=vector1[pos2];
pos2++; pos3++;
}
for(pos3=0; pos3<=fin2-inicio1; pos3++)
{
vector1[inicio1+pos3]=vector2[pos3];
}
delete vector2;
}
void mergeSort (int vector[], int inicio, int fin)
{
if(fin-inicio == 0)
{
return;
}
else
{
mergeSort (vector, inicio, (inicio+fin)/2);
mergeSort(vector, (inicio+fin)/2 +1, fin);
merge(vector, inicio, (inicio+fin)/2, (inicio+fin)/2+1, fin);
}
}