forked from yennanliu/CS_basics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge.sql
More file actions
18 lines (14 loc) · 746 Bytes
/
Merge.sql
File metadata and controls
18 lines (14 loc) · 746 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
Merge
https://stackoverflow.com/questions/55223960/textbook-mergesort-implementation-in-sql-postgres
*/
CREATE OR REPLACE FUNCTION merge(A1 double precision[],A2 double precision[], i integer, j integer,acc double precision[])
RETURNS double precision[] AS $$
SELECT
CASE WHEN (i > array_length(A1,1) and j > array_length(A2,1)) THEN acc
WHEN i > array_length(A1,1) THEN merge(A1,A2,i,j+1,array_append(acc,A2[j]))
WHEN j > array_length(A2,1) THEN merge(A1,A2,i+1,j,array_append(acc,A1[i]))
WHEN A1[i] < A2[j] THEN merge(A1,A2,i+1,j,array_append(acc, A1[i]))
WHEN A1[i] >= A2[j] THEN merge(A1,A2,i,j+1,array_append(acc, A2[j]))
END;
$$ LANGUAGE SQL;