pg_algorithms
is PostgreSQL extension that provides an implementation of the most well-known algorithms. This is a pet project and its main purpose is to learn PostgreSQL hacking, so do not use it in the production environment.
-
- Bubble sort
Bucket SortCounting SortHeap SortInsertion SortMerge Sort- QuickSort
Radix SortSelection SortShellSort
To install pg_algorithms
, take the following steps:
git clone https://github.com/kostiantyn-nemchenko/pg_algorithms
cd pg_algorithms
make install
After that, execute this statement in psql:
CREATE EXTENSION pg_algorithms;
postgres=# select bubble_sort('{9876, 0, -1, 23786, 124, 7, -17000, 93, 5, 60, 313}'::int[]);
bubble_sort
--------------------------------------------
{-17000,-1,0,5,7,60,93,124,313,9876,23786}
(1 row)
postgres=# select quick_sort('{9876, 0, -1, 23786, 124, 7, -17000, 93, 5, 60, 313}'::int[]);
quick_sort
--------------------------------------------
{-17000,-1,0,5,7,60,93,124,313,9876,23786}
(1 row)