Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8878125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:38:19+00:00 2026-06-14T19:38:19+00:00

Quicksort is not stable, since it exchanges nonadjacent elements. Please help me understanding this

  • 0

Quicksort is not stable, since it exchanges nonadjacent elements.

Please help me understanding this statement.

I know how partitioning works, and what stability is. But I cant figure out what makes the above as the reason for this to be not stable?
Then I believe the same can be said for merge sort – though it is quoted to be a stable algorithm.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:38:20+00:00Added an answer on June 14, 2026 at 7:38 pm

    Consider what happens during the partition for the following array of pairs, where the comparator uses the integer (only). The string is just there so that we have two elements that compare as if equal, but actually are distinguishable.

    (4, "first"), (2, ""), (3, ""), (4, "second"), (1, "")
    

    By definition a sort is stable if, after the sort, the two elements that compare as if equal (the two 4s) appear in the same order afterwards as they did before.

    Suppose we choose 3 as the pivot. The two 4 elements will end up after it and the 1 and the 2 before it (there’s a bit more to it than that, I’ve ignored moving the pivot since it’s already in the correct position, but you say you understand partitioning).

    Quicksorts in general don’t give any particular guarantee where after the partition the two 4s will be, and I think most implementations would reverse them. For instance, if we use Hoare’s classic partitioning algorithm, the array is partitioned as follows:

    (1, ""), (2, ""), (3, ""), (4, "second"), (4, "first")
    

    which violates the stability of sorting.

    Since each partition isn’t stable, the overall sort isn’t likely to be.

    As Steve314 points out in a comment, merge sort is stable provided that when merging, if you encounter equal elements you always output first the one that came from the “lower down” of the two halves that you’re merging together. That is, each merge has to look like this, where the “left” is the side that comes from lower down in the original array.

    while (left not empty and right not empty):
        if first_item_on_left <= first_item_on_right:
           move one item from left to output
        else:
           move one item from right to output
    move everything from left to output
    move everything from right to output
    

    If the <= were < then the merge wouldn’t be stable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that quicksort is not stable method, namely for equal elements, maybe member
I find this Quicksort partitioning approach confusing and wrong, yet it seems to work.
This is all my code for my quicksort method, it works on a set
I'm going nuts over this special quicksort algorithm and I don't know where is
I just implemented QuickSort algorithm from book and got weird output. It works but
I'm doing a simple StringList.sort, but Delphi uses a QuickSort that is not a
In this quicksort implementation I think the program I'm running gets into a racing
This is not my school home work. This is my own home work and
As a learning experience I recently tried implementing Quicksort with 3 way partitioning in
Can anyone point out why this implementation of quick sort is not working, i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.