LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-16-2018, 12:16 PM   #1
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Sorting algorithms


This question is prompted by curiosity, nothing more.

A Google search on sorting algorithm leads to this page (among others):
https://en.wikipedia.org/wiki/Sorting_algorithm

When a Linux programmer invokes the sort command, which algorithm is used? Are there "under the covers" several sorting programs and the sort command chooses the best for each case based on characteristics such as file size and line length?

Daniel B. Martin
 
Old 03-16-2018, 12:26 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,217

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
Well, here's the source code for it:

https://github.com/coreutils/coreuti...ter/src/sort.c

Merge sort. According to Wikipedia.

Last edited by dugan; 03-16-2018 at 12:31 PM.
 
1 members found this post helpful.
Old 03-16-2018, 12:32 PM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,879
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
The two places to research this should be:
  1. The manpage for sort(1)
  2. The source code for the core utils, containing source for the sort(1) command
Search for the word "algorithm" within the source, there are two occurrences I can see, one of which discusses Knuth, which memory serves would be the shell sort, or a modified shell, I forget. The other occurrence appears to be a btree using sequential sort.
 
1 members found this post helpful.
Old 03-16-2018, 12:42 PM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,288
Blog Entries: 3

Rep: Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718
The OpenBSD sort has merge sort, quick sort, and radix sort.

https://cvsweb.openbsd.org/cgi-bin/c.../usr.bin/sort/

The code is quite clean so it should be feasible to port it if needed.

However, you'd have to use a runtime switch to tell it to use a different sort than merge sort which it also has for its default.
 
1 members found this post helpful.
Old 03-16-2018, 09:34 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,632
Blog Entries: 4

Rep: Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931
The sort-command is actually quite sophisticated, although it's not the best there is. Dr. Knuth wrote an entire book on "Sorting & Searching," if you are theoretical-mathematician enough to understand a single page of it.
 
  


Reply

Tags
algorithm, sorting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Rapid Listing, alphabetically sorting, dir/files sorting in C ? Xeratul Programming 18 11-24-2014 10:13 AM
[SOLVED] mcrypt algorithms bucovaina78 Linux - Security 4 07-17-2012 09:23 AM
Algorithms With C (Mastering) delite Programming 3 12-28-2008 12:04 PM
Algorithms Amdx2_x64 Programming 7 08-09-2008 06:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration