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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:37:50+00:00 2026-06-09T02:37:50+00:00

Use QUICKSORT, sort the integer array contains even number in ascending order and uneven

  • 0

Use QUICKSORT, sort the integer array contains even number in ascending order and uneven number in descending order.

ex: input int a[8]={4,6,1,2,5,3,8,7}
=>output is {2,4,6,8,7,5,3,1}.


I think the QSort function will give me look like this B={4,6,2,8,1,3,7,5} and i’ll split B array to two array is C and D.


C array contains even number {4,6,2,8} and i’ll use QuickSort to sort C array like this {2,4,6,8}


D array contains uven number {5,3,1,7} and i’ll use QuickSort to sort D array like this {7,5,3,1}
After that i’ll plus C and D. Finally my expected result is {2,4,6,8,7,5,3,1}
and this is my code :(! Thanks alot!

    void Input(int a[], int n)
    {
        for(int i = 0; i<n;i++)
        {
            cout<<"a["<<i<<"]: ";
                cin>>a[i];
        }
    }
    void Display(int a[],int n)
    {
        for(int i = 0;i < n; i++)
        {
            cout<<a[i]<<" ";
        }
        cout<<endl;
    }
    void Swap(int &a, int &b)
    {
        int temp = a;
        a=b;
        b=temp;
    }
    void QuickSort(int a[],int Left ,int Right)
    {
        int i, j, x;
        x = a[(Left + Right)/2];
        i = Left;j = Right;
        do
        {
            while( a[i]<x) i++;
            while(a[j]>x) j--;
            if(i<=j)
            {
                Swap(a[i],a[j]);
                    i++; j--;
            }
        }while (i<=j);
        if(i<Right) QuickSort(a,i,Right);
        if(Left<j) QuickSort(a,Left,j);

    }
   //after sort the array with function QuickSort, i was suggested to use 1 more       QuickSort 
   // to move evennumber to the top of the array
    void QSort(int a[],int Left,int Right)
    {
        int i, j, x;
        x = a[(Left + Right)/2];
        i = Left;j = Right;

        {
            while(a[i]%2==0 && a[i]<x ) i++;
            while(a[j]%2==1 && a[j]>x) j--;
            if(i<=j)
            {
                Swap(a[i],a[j]);
                    i++; j--;
            }
        }while (i<=j);
        for (i = 0; i<r;i++)
        {

        }
        if(i<Right) QSort(a,i,Right);
        if(Left<j) QSort(a,Left,j);
    }


    int main()
    {
          //n is numbers of integer array
        int a[Max],n;
            cout<<"Insert numbers of array: ";
            cin>>n;
            Input(a,n);
            cout<<"Array:\n";
            Display(a,n);
            cout<<endl;

            cout<<"Array after arrange: \n";
            QuickSort(a,0,n-1);
            Display(a,n);
            cout<<endl;

            cout<<"move even number to the top:\n";
            QSort(a,0,n-1);
            Display(a,n);

        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-09T02:37:52+00:00Added an answer on June 9, 2026 at 2:37 am

    So, the numbers 1 to 10 should be sorted as {2,4,6,8,10,9,7,5,3,1}?

    Without looking at your code, I can tell you that you don’t need two quicksort functions.

    Just let even numbers compare less than odd numbers. Numbers of the same parity compare as specified.

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

Sidebar

Related Questions

I finally finished a home-grown version of quicksort to sort an array of structures
I am having issues implementing a quicksort to sort an array of strings. I
I have an array with arrays in it, where I want to sort the
SO Posts When to use merge sort and when to use quick sort? Quick
I'm doing a simple StringList.sort, but Delphi uses a QuickSort that is not a
I need to create a random array of int and have it sorted by
Currently, I have a barebones implementation of the quicksort algorithm to sort some randomly
I'm trying to use quick sort for my project but it doesn't work. And
So I have a assignment ask me to use randomized quick sort, and I
I have an input file that I want to sort based on timestamp which

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.