我对这个问题非常沮丧。我在pl / pgsql中编写了一个merge-sort函数,如下所示:
create or replace function merge(a int[],low int, high int) returns int[]
as $$
declare mid int;
i int;
left_low int;
right_low int;
temp int[];
begin
mid = (low + high)/2;
left_low = low;
right_low = mid+1;
temp = array_fill(0,array[high - low +1],array[low]);
i=low;
while (left_low <= mid and right_low <= high) loop
if (a[left_low] < a[right_low]) then
temp[i] = a[left_low];
left_low = left_low+1;
i=i+1;
else
temp[i] = a[right_low];
right_low = right_low+1;
i=i+1;
end if;
end loop;
while ( left_low <= mid) loop
temp[i] = a[left_low];
left_low = left_low+1;
i=i+1;
end loop;
while ( right_low <= high) loop
temp[i] = a[right_low];
right_low = right_low+1;
i=i+1;
end loop;
for i in low..high loop
a[i] = temp[i];
end loop;
return a;
end;
$$ language plpgsql;
create or replace function merge_sort(arr int[],low int, high int) returns int[] as $$
declare mid int;
left_part int[];
right_part int[];
b int[];
begin
if (low < high) then
mid = (low + high)/2;
left_part = merge_sort(arr,low,mid);
right_part = merge_sort(arr,mid+1,high);
b = left_part || right_part;
b = merge(b,low,high);
return b;
end if;
if (low = high) then
return arr;
end if;
end;
$$ language plpgsql;
select merge_sort(array[1,6,7,2,45,6,3,8],1,8);
它引发了“数组值必须以”{“或维度信息”开头的错误,如您所见:
ERROR: array value must start with "{" or dimension information
CONTEXT: PL/pgSQL function merge_sort(integer[],integer,integer) line 12 at assignment
SQL statement "SELECT merge_sort(arr,low,mid)"
PL/pgSQL function merge_sort(integer[],integer,integer) line 9 at assignment
PL/pgSQL function merge_sort(integer[],integer,integer) line 9 at assignment
那很奇怪!函数“merge_sort(arr int [],low int,high int)”arr的参数声明为“int []”,显然是一个数组。
请帮帮我。谢谢。