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

  • SEARCH
  • Home
  • 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 6355897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:51:34+00:00 2026-05-24T22:51:34+00:00

my median 3 implementation is not working fine here. i have to choose 3

  • 0

my median 3 implementation is not working fine here. i have to choose 3 numbers randomly for medium here is my code please help me.

#include"stdafx.h"
#include <iostream>
#include<algorithm>
using namespace std;
#define size 10
int i;                               
void show(int* array, int n);
int partition(int* array, int pValue, int left, int right);
void QuickSort(int* array, int left, int right);

int main(void)
{
    int array[size];
    int i;

    for( i = 0; i < size; i++)              
    {
         array[i]=rand()%100;
    }

    cout<<endl<<"The random generated numbers are: "<<endl;
    show(array, size);
    QuickSort(array,0,size - 1);                
    cout<<endl<<"The sorted numbers are : "<<endl;
    show(array, size);

    system("pause");
    return 0;
}

void show(int* array, int n)
{
    int i;

    for( i = 0; i < n; i++) cout<<array[i]<<'\t';
}



void QuickSort(int* array, int left, int right)
{
    for(i=0;i<3;i++)
    {
       array[i]=array[rand()%100];
      }
      stable_sort(array,array+3);
     int p=array[(i+1)/2];
    //int p = array[left];              
    int split;

    if(right > left)                         
    {
        split = partition(array, p, left, right);

        array[split] = p;
        QuickSort(array, left, split-1);   
        QuickSort(array, split+1, right);    
    }
}


int partition(int* array, int p, int left, int right)
{
    int lb = left;
    int rb = right;

    while(lb < rb)             
    {
         while( p < array[rb]&& rb > lb)      
         {
              rb--;                     
         }
         swap(array[lb], array[rb]);

         while( p >= array[lb]&& lb < rb)     
         {
              lb++;                      
         }
         swap(array[rb], array[lb]);

    }
    return lb;                            


}
  • 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-05-24T22:51:36+00:00Added an answer on May 24, 2026 at 10:51 pm

    Your code was way too complex for this simple algorithm, check code below:

    void QuickSortMedian(int a[],int start,int end) {
        int q;
        count++;
        if (end-start<2) return;
        q=MedianOfThreePartition(a,start,end);
        QuickSortMedian(a,start,q);
        QuickSortMedian(a,q,end);
    }
    
    int MedianOfThreePartition(int a[],int p, int r) {
        int x=a[p],y=a[(r-p)/2+p],z=a[r-1],i=p-1,j=r;
        if (y>x && y<z || y>z && y<x ) x=y;
        else if (z>x && z<y || z>y && z<x ) x=z;
        while (1) {
            do  {j--;count++;} while (a[j] > x);
            do  {i++;count++;} while (a[i] < x);
            if  (i < j) swap(&a[i],&a[j]);
            else return j+1;
        }
    }
    
    
    void QuickSortRandomAndMedian(int a[],int start,int end) {
        int q;
        count++;
        if (end-start<2) return;
        q=RandomAndMedianPartition(a,start,end);
        QuickSortRandomAndMedian(a,start,q);
        QuickSortRandomAndMedian(a,q,end);
    }
    
    int RandomAndMedianPartition(int a[],int p, int r) {
        int t,x=a[t=((rand()%(r-p))/2)+p+(r-p)/4],y=a[t+1],z=a[t-1],i=p-1,j=r;
        if (y>x && y<z || y>z && y<x ) x=y;
        else if (z>x && z<y || z>y && z<x ) x=z;
        while (1) {
            do  {j--;count++;} while (a[j] > x);
            do  {i++;count++;} while (a[i] < x);
            if  (i < j) swap(&a[i],&a[j]);
            else return j+1;
        }
    }
    

    The second algorithm is just a boost that I wrote for the optimization of quick sort, for example on a 40000 elements array regular quick sorts did about 800k actions, the median one did 650k and the random median one did about 620k. That’s the best I got so far. 🙂

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

Sidebar

Related Questions

Please can anyone help me out with this problem? This code below works perfectly
Currently, I have a barebones implementation of the quicksort algorithm to sort some randomly
I'm trying to solve interviewstreet's median challenge. I saw a similar question posted here:
I have a number array and I'd like to calculate the median. When the
I need to calculate the median value in MySQL. I saw the solution here
Formulate the steps of identifying the median from five unique numbers and visualize them
n00b here (first Android project). I have been given a custom video codec that
I am relatively new to Django and have really been struggling with an implementation
Does anyone know if there is a clean implementation of the Turlach rolling median
Below given code is for video streaming. This is fine with IE9 and firefox

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.