Implementation of sort algorithm