-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmergesort.cpp
More file actions
99 lines (88 loc) · 1.71 KB
/
mergesort.cpp
File metadata and controls
99 lines (88 loc) · 1.71 KB
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
#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define MOD 1000000007
struct node{
int info;
node *link;
};
void split(node* source, node** a, node** b){
node* adv2;
node* adv1;
adv1 = source;
adv2 = source->link;
while (adv2 != NULL) {
adv2 = adv2->link;
if (adv2 != NULL) {
adv1 = adv1->link;
adv2 = adv2->link;
}
}
*a = source ;
*b = adv1->link ;
adv1->link = NULL;
}
node* Merge(node* a, node* b)
{
node* result = NULL;
if (a == NULL)
return (b);
else if (b == NULL)
return (a);
if (a->info <= b->info) {
result = a;
result->link = Merge(a->link, b);
}
else {
result = b;
result->link = Merge(a, b->link);
}
return (result);
}
void CowboyBebop(node** headNode){
node* head = *headNode ;
node *a , *b ;
if((head == NULL) || (head->link == NULL)){
return ;
}
split(head,&a,&b);
CowboyBebop(&a);
CowboyBebop(&b);
*headNode = Merge(a,b);
}
void printList(node* n)
{
while (n != NULL) {
cout << n->info << " ";
n = n->link;
}
}
void push(node** head_ref, int new_data)
{
node* new_node = new node();
new_node->info = new_data;
new_node->link = (*head_ref);
(*head_ref) = new_node;
}
using namespace std;
int main(void){
std::ios_base::sync_with_stdio(false);
// #ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
node* a = NULL ;
int t = 1 , n , d ;
cin >> n ;
for (int i = 0; i < n; i++)
{
cin >> d ;
push(&a,d);
}
// create a linked list
// cowboybebop is my def mergesort()
while(t--){
CowboyBebop(&a);
}
printList(a) ;
return 0;
}