-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertion Sort Advanced Analysis
98 lines (92 loc) · 1.81 KB
/
Insertion Sort Advanced Analysis
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;
/*
* Complete the 'insertionSort' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY arr as parameter.
*/
struct Node {
int num;
int rights;
int doubl;
Node *next[2];
Node(int num){
this->num=num;
rights=0;
doubl=0;
next[0]=NULL;
next[1]=NULL;
}
};
long insertionSort(int arr[],int n) {
int i;
for(i=0;i<n-1;i++){
if (arr[i]>arr[i+1]) {
i--;
break;
}
}
if(i==n-1){
return 0;
}
for(i=n-1;i>0;i--){
if (arr[i]>arr[i-1]) {
i--;
break;
}
}
if(i==0){
return 1L*n*(n-1)/2;
}
long moves=0;
Node *root= new Node(arr[0]);
Node *currentNode=root;
Node *nodeToUpdate=root;
int lr=0;
int lrSupp=0;
for (int i=1;i<n;i++)
{
currentNode=root;
while (currentNode!=NULL) {
lr= arr[i]>currentNode->num;
lrSupp=arr[i]<currentNode->num;
if(lrSupp){
moves+=(currentNode->rights+1+currentNode->doubl);
nodeToUpdate=currentNode;
currentNode=currentNode->next[lr];
continue;
}
else if (lr){
currentNode->rights++;
nodeToUpdate=currentNode;
currentNode=currentNode->next[lr];
continue;
}
if (lr==lrSupp) {
moves+=(currentNode->rights);
currentNode->doubl++;
break;
}
}
if (lr==lrSupp) {
continue;
}
nodeToUpdate->next[lr]=new Node(arr[i]);
}
return moves;
}
int main()
{
int runs,n ;
int a[100001];
scanf("%d",&runs) ;
while(runs--)
{
scanf("%d",&n) ;
for(int i = 0;i < n;i++) scanf("%d",&a[i]) ;
long long ret=insertionSort(a,n);
cout << ret << endl ;
}
return 0 ;
}