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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:39:47+00:00 2026-06-13T12:39:47+00:00

I’ve a problem with my Quicksort. For some values it works but for other

  • 0

I’ve a problem with my Quicksort. For some values it works but for other it doesn’t. For example when the first value is smaller than the last it doesn’t work. I don’t know what is wrong. This is code:

#include <stdio.h>
#include <time.h>

#define lenght_max 1000000
int x;
int tablica[lenght_max];
int q;

int Partition(left, right) {

    int tmp;
    int i;
    int j;

    i = -1;
    j = 0;

    x = tablica[right];
    i = left - 1;

    for(j = left; j < right; j++){
      if(tablica[j] <= x) {
        i++;
        tmp = tablica[i];
        tablica[i] = tablica[j];
        tablica[j] = tmp;
      }
    }

    return i + 1;
}

void Quicksort(left, right) {    
    if(left < right){ 
      q = Partition(left, right);
      Quicksort(left , q - 1);
      Quicksort(q + 1, right);
    }
}

int main(void) {

  int i;
  int temporary;
  int left;
  int right;

  printf("Witaj uzytkowniku. To jest program preferujacy sortowanie szybkie - quicksort.\n");
  printf("Podaj, ile liczb chcialbys posortowac: ");
  scanf("%i", &temporary);

  printf("Podaj liczby do sortowania: \n");

  for(i = 0; i < temporary; i++)
    scanf("%d", &tablica[i]);

    left = 0;
    right = temporary - 1;
    x = temporary / 2;

  Quicksort(left, right);

  printf("\nPROCES:\n");

  for(i = 0; i < temporary; i++)
    printf("%d\n", tablica[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-13T12:39:48+00:00Added an answer on June 13, 2026 at 12:39 pm

    If I didn’t overlook the problem, it seems to me that you forgot to swap the pivot with the first element grater than the pivot at the end of Partition. The fix should be as simple as adding:

        tmp            = tablica[i+1];
        tablica[i+1]   = tablica[right];
        tablica[right] = tmp;
    

    before the return i + 1; statement inside Partition.

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

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.