Sort List use merge sort

Why you are so sad? Just because you can not sort a list?
If someone want you to sort a list, what will you do? quicksort? No.
Merge sort is your choose! How much do you think a merge sort will cost? $O(n^2)$ ? $O(n^3)$ ? No!
It ONLY take you $O(n * log_2n)$! Buy now, you can get a input part freely!

For merge sort, it’s very useful. I don’t want to say how merge sort works. If you want that, just Google “Merge sort”.
I just want to practise. Here is my code.

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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
struct Node{
int val;
Node * next;
}node;

Node * merge(Node * s, int len){
if(len == 1) return s;
Node * a = s, *tmp;
Node * res = NULL, *now = NULL;
for(int i = 1;i < len >> 1;++ i)
s = s -> next;
tmp = s -> next;
s -> next = NULL;// find the start of the second part and set the end of the first part to NULL
s = tmp;
a = merge(a, len >> 1);
s = merge(s, len - (len >> 1));
if(a -> val < s -> val) res = a, a = a -> next;
else res = s, s = s-> next;
now = res;
while(a && s){
if(a -> val < s -> val){
now -> next = a;
now = a;
a = a -> next;
}else{
now -> next = s;
now = s;
s = s -> next;
}
}
if(!a) now -> next = s;
else now -> next = a;
return res;
}
int main(){
int n = 0;
int val = 0;
Node * head, * tail;
scanf("%d",&n);
scanf("%d",&val);
Node * tmp = (Node *)malloc(sizeof(Node));
head = tmp;
head -> val = val;
tail = head;
for(int i = 0;i < n - 1;++ i){
scanf("%d",&val);
tmp = (Node *)malloc(sizeof(Node));
tmp -> next = NULL;
tmp -> val = val;
tail -> next = tmp;
tail = tmp;
}
head = merge(head, n);
while(head){
printf("%d ",head -> val);
head = head -> next;
}
printf("\n");
return 0;

}
Contents
,