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 7976415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:54:12+00:00 2026-06-04T08:54:12+00:00

I am fairly new to programming and have been learning sorting algorithms for a

  • 0

I am fairly new to programming and have been learning sorting algorithms for a while now.I have implemented Quick sort below in C. It uses median of three partitioning to select the pivot.
The problem with the code is every time I run it, the element that is supposed to be the median/ mid value of the sorted array is unsorted, ie it occurs somewhere else.
Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

void swap(int *x, int *y)
{
    int temp = *x;
    *x = *y;
    *y = temp;
    return ;
}

int median3(int a[], int left, int right)//Uses median of three partitioning technique
{
    int center = (left + right)/2;
    if (a[center] < a[left]) 
        swap(&a[left],&a[center]);
    if (a[right] < a[left])
        swap(&a[left],&a[right]);
    if (a[right]< a[center])
        swap(&a[center],&a[right]);

    swap(&a[center], &a[right - 1]);//since the largest is already in the right.
    return a[right - 1];
}

void quicksort(int a[], int left, int right)
{
  if (left < right) {
    int pivot = median3(a,left,right);
    int i = left;
    int j = right - 1;
    for ( ; ;) {
        while(a[++i]<pivot) {} 
        while(pivot<a[--j]) {} 
        if ( i < j)
            swap(&a[i],&a[j]);
        else
            break ;
    }
    swap(&a[i],& a[right -1]);
    quicksort(a,left,i-1);
    quicksort(a,i+1,right);
  }
    return ;
}

int main(int argc, char* argv[])
{
    int i;
    int a[] = {8,1,4,9,6,3,5,2,7,0};
    quicksort(a,0,9);
    for ( i = 0 ; i < 10 ; i++)
        printf("%d\n",a[i]);
    return 0;
}
  • 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-04T08:54:13+00:00Added an answer on June 4, 2026 at 8:54 am

    Watch out for the edge case, where left == right - 1:

    void quicksort(int a[], int left, int right)
    {
      if (left < right) {
        int pivot = median3(a,left,right);
        if (left == right - 1) return;  // only two elements, already sorted by median3()
        int i = left;
        int j = right - 1;
    

    http://ideone.com/X5Ydx

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

Sidebar

Related Questions

I'm fairly new to programming in Objective-C. While I have been able to find
I'm very much new to programming and have been doing fairly well so far.
I'm fairly new to database programming in WinForms, and have been using BindingSource, DataSet,
I have been working on Eclipse recently. I am fairly new to java programming,
I'm fairly new to programming and from learning I have seen different ways of
I'm fairly new to Visual Studio (specifically Visual Basic .NET) but have been programming
I'm fairly new to programming in general but have been working with Java for
I'm fairly new to programming C# and I have written a program that uses
I'm fairly new to programming and have been focussing a great deal on Java
I am fairly new to PHP, but have applied my previous knowledge of programming,

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.