Implementing GridSort