Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux Sort command

Tags:

linux

sorting

I would like to know which sorting algorithm the linux SORT command uses?

like image 368
Vineeth Avatar asked Nov 19 '09 03:11

Vineeth


People also ask

What sort command do in Linux?

The sort command is a command-line utility for sorting lines of text files. It supports sorting alphabetically, in reverse order, by number, by month, and can also remove duplicates.

How do you sort data in Linux?

To sort a file containing numeric data, use the -n flag with the command. By default, sort will arrange the data in ascending order. If you want to sort in descending order, reverse the arrangement using the -r option along with the -n flag in the command.

What is the command of sort?

The sort command is used in Linux to print the output of a file in given order. This command processes on your data (the content of the file or output of any command) and reorders it in the specified way, which helps us to read the data efficiently.

How do I sort a list of files in Linux?

To list all files and sort them by size, use the -S option. By default, it displays output in descending order (biggest to smallest in size). You can output the file sizes in human-readable format by adding the -h option as shown. And to sort in reverse order, add the -r flag as follows.


2 Answers

mergesort

It1 uses mergesort rather than quicksort or heapsort for two reasons:

  • mergesort is a stable sort and typically the efficient quicksort implementations are not
  • while it may do more swaps or moves it does fewer comparisons and so tends to work better with text input

1. Linux distros are free to choose their own sort utility but I imagine virtually all use GNU sort so I have described that.

like image 156
DigitalRoss Avatar answered Oct 12 '22 04:10

DigitalRoss


An External R-Way merge sort according to Algorithm details of UNIX Sort Command. Found via this stackoverflow question.

like image 29
Jonathan Avatar answered Oct 12 '22 04:10

Jonathan