Click here to Skip to main content
Licence CPOL
First Posted 13 May 2008
Views 16,445
Downloads 244
Bookmarked 9 times

Fastest In-Place Stable Sort

By | 13 May 2008 | Article
A stable version of quicksort

Introduction

If you need a stable sorting algorithm that is fast and does not need extra storage space of O(N) then you should go straight past the usual O(N^2) sorts like Insertion Sort, Selection Sort and Bubble Sort. This application contains 2 in-place stable sorts that are much faster:

  1. In-Place Merge Sort
  2. Stable Quick Sort

This application demonstrates both algorithms and compares them with:

  • Insertion Sort which is also stable and in-place
  • Traditional Merge Sort which is stable but required O(N) extra storage
  • Traditional Quick Sort which is not stable but is in place.

Background - In Place Merge Sort

I took the algorithm for In Place Merge Sort from Thomas Baudel's version. It sorts in the same way that a merge sort does except the merge process is in-place. It is very fast and elegant and uses O(logN) extra space. The extra effort in maintaining stability in-place makes it slower than most other O(NLogN) sorts but speed is still in the same order of magnitude.

Background - Stable Quick Sort

This algorithm sorts in the same way that Quick Sort does. The difference is that the pivot function doesn't just swap elements that need to be moved. Instead, it builds up lists of runs in a buffer. Whenever the buffer is full, it accumulates the runs in a binary pattern. This algorithm is about 20% faster than In-Place Merge Sort but it is not as elegant. It also uses O(logN) extra space. The extra effort in maintaining stability in-place also makes it slower than most other O(NLogN) sorts but speed is still in the same order of magnitude.

Using the Code

The download includes an executable that demonstrates the sorts. Other than that, the sort is contained in one module.

Points of Interest

I attempted several things before this version:

  • I made a stable version of heap sort (it ran in O(n^2) time).
  • I made a version of in-place merge using the run buffers but could not get it to be as fast as Thomas's version.

History

  • 16/5/08 - Added In-Place Merge Sort, Traditional Merge Sort and Traditional Quick Sort

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Craig Brown



Australia Australia

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmvpDave Kreskowiak3:39 18 Mar '10  
GeneralStable quicksort - have you looked at: PinmemberSelma19:26 15 May '08  
AnswerRe: Stable quicksort - have you looked at: PinmemberCraig Brown19:45 15 May '08  
GeneralDifferent Comparison PinmemberCraig G. Wilson3:27 14 May '08  
AnswerRe: Different Comparison PinmemberCraig Brown15:02 14 May '08  
GeneralRe: Different Comparison Pinmembermastamac4:45 13 Apr '12  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 14 May 2008
Article Copyright 2008 by Craig Brown
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid