Properly Partitioning QuickSort Array

Properly Partitioning QuickSort Array

By : Duongquoc Viet
Date : November 20 2020, 11:01 PM
may help you . I'm a beginner in C and I've been trying to code a Quicksort program that can take a randomly generated array of real numbers and its size as its argument, and then sorts the elements in ascending order. I cannot figure out what to put in the array size field in the first recursive call of the function QuickSort, which is meant to represent the subarray A[0...q-1]. As far as I can tell, the rest of the code is fine because when linked to a driver program that generates the random numbers, the program returns the elements, albeit in the incorrect order. I appreciate any help/suggestions. , You're only problem is you seem to confuse:
code :
int Partition(float *,int);

void QuickSort(float *A,int n) {
  int q;

    q = Partition(A,n);
    QuickSort(A,q); //Trying to figure out what to put in here.
    QuickSort(A+q+1,(n-q-1)); //This recursion sends the subarray A[q+1...n-1] to QuickSort, I think it works fine.

int Partition(float *A,int n){
  int i,j;
  float x;
  float tmp;
  x = A[n-1];
    if(A[j] <= x){
      tmp = A[i];
      i = i+1;
  tmp = A[i];
  return i;

int main() {
    float A[] = {3, 4, -5, 10, 21, -9, -1, 7, 8, 10};
    for(int i = 0; i < 10; i ++)
        printf("%f ",A[i]);
    return 0;

Share : facebook icon twitter icon
cost of partitioning in quicksort

cost of partitioning in quicksort

By : Роман Дубинка
Date : March 29 2020, 07:55 AM
it helps some times Yes, on average the partitioning requires n+1 compares. (Actually n+1-(1/n))
Quicksort with 2 way partitioning

Quicksort with 2 way partitioning

By : user3634425
Date : March 29 2020, 07:55 AM
it helps some times I am trying to implement quicksort algorithm by Sadgewick. The code was taken from here http://www.sorting-algorithms.com/static/QuicksortIsOptimal.pdf , Just put the call to
code :
var v = a[right];
if (right <= left)
Quicksort partitioning

Quicksort partitioning

By : Nedved31
Date : March 29 2020, 07:55 AM
To fix the issue you can do EDIT
My previous answer seemed to fix the issue but wasn't correct.
code :
t = a[i];
a[i++] = a[j];
a[j--] = t; // added a decrement here
Why is the pivot element moved to the first or last position of the array before partitioning in Quicksort?

Why is the pivot element moved to the first or last position of the array before partitioning in Quicksort?

By : Lumimuut
Date : March 29 2020, 07:55 AM
help you fix your problem The Hoare partition scheme is similar in concept to the example in the question, except that the initial pivot value can be taken from any place within the array, and no special end case swapping is performed, as the pivot value and pivot pointer will end up in it's correct position during the Hoare partitioning.
Here is an example of quicksort that uses median of 3, Hoare partition scheme, exclusion of elements adjacent to and equal to pivot (they are already sorted), and only using recursion on the smaller partition to limit worst case stack space to O(log(n)).
code :
void QuickSort(uint32_t a[], size_t lo, size_t hi) {
    while(lo < hi){
        size_t i = lo, j = (lo+hi)/2, k = hi;
        uint32_t p;
        if (a[k] < a[i])            // median of 3
            std::swap(a[k], a[i]);
        if (a[j] < a[i])
            std::swap(a[j], a[i]);
        if (a[k] < a[j])
            std::swap(a[k], a[j]);
        p = a[j];
        i--;                        // Hoare partition
        while (1) {
            while (a[++i] < p);
            while (a[--k] > p);
            if (i >= k)
            std::swap(a[i], a[k]);
        i = k++;
        // at this point, a[i] or a[k] or both == p  (pivot value)
        while(i > lo && a[i] == p)  // exclude middle values == pivot
        while(k < hi && a[k] == p)
        // recurse on smaller part, loop on larger part
        if((i - lo) <= (hi - k)){
            QuickSort(a, lo, i);
            lo = k;
        } else {
            QuickSort(a, k, hi);
            hi = i;
quicksort help, not sure why partitioning returns index and not array

quicksort help, not sure why partitioning returns index and not array

By : Yannick Winters
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , The index that has been returned is only there to identify the end of recurrsion within the QuickSort algorithm. It's mainly the index of the pivot element that is used to identify the smaller and bigger numbers.
AND: You are referring to an enhanced Quick Search Algorithm. In the basic version of the QuickSearch algorithm the returned index won't be needed.
Related Posts Related Posts :
  • Comparison of one element in array to all other elements in same array
  • In C, can we read from pipes without busywaiting, may be using callbacks or other ways?
  • Check current time (without date) is between two times in C
  • I have something like this code and I would like to know how to get the number of elements of the array I pass in, if th
  • Difference between debug and release executable, in example code, in terms of symbols and functions optimised
  • find the unmatched elements at each index in two arrays
  • Structure Output are Different
  • Linux read() call not returning error when i Unplug serial cable
  • fread and ferror don't set errno
  • ATmega32U4: enabling interrupts hangs
  • Printing 2d array box
  • C - Dynamic Memory Allocation
  • How to read a string from a C function into Go?
  • Application is getting killed without any reason. Suspecting high BSS. How to debug it?
  • Dividing with/without using floats in C
  • Preprocessor conditional distributed across `#include` files
  • Typecasting a void pointer in a Linked List
  • Basic Functions in C
  • A for loop that calculates the even or odd then adds them up
  • Pascal String to cstring
  • Assigning to two pointers at the same time in C
  • getchar returns a different character
  • Understanding the enhanced syntax of for loop
  • Comparing char to string - C
  • How to omit the Bios Parameter Block in a bootloader?
  • Search linked list by substring and create a new linkedlist with all structs that contain the substring
  • Procedure and pointers C
  • RegOpenCurrentUser(KEY_WRITE) on newly created user
  • C Program with Pointer Increment False Values
  • Why won't a chunck of this program run in Linux but will on Windows?
  • Flow of the fork, how many forks do I have?
  • C recursive function for product receiving segmentation fault
  • C String concatenation using memcpy not appending
  • Place String Literal Before Variable
  • Sending payload with sigaction
  • using sizeof(void) inside malloc
  • Getting unused variable warning
  • C - Writing on second line of file
  • Unable to copy ip address from string to sockaddr_in
  • Write a in c program using loop control to produce the following output
  • GCC not warning on implicit cast from void * to void **
  • char[] vs char* in scanf C when writing memory
  • Recursive Calculation of a queue
  • How to rotate bits to the left and add rotated bits to the right
  • Print a size_t in a OS-independent architecture independent way
  • Radix Sort for floats in C - negative values get corrupted
  • ANSI C Separating the data from the file
  • Pascal - how sets work?
  • K&R 1.6 Sample Code Displays Long Integers
  • Unexpected Infinite for loop
  • using struct value in realloc gives segmentation fault
  • c - Error: "incomplete type is not allowed" , IAR compiler
  • How use correctly the pointers in a multi dimensional array?
  • Optimize hollow diamond pattern printing in c with minimum loops
  • Store an string on a Shared Memory C
  • How to create a sine function in c
  • Initializing n integers in 2-dimensional array
  • Why is executing callback functions in kernel mode bad?
  • What is wrong with my bubble sort?
  • My Program has a couple of bugs and I dont know how to fix them
  • shadow
    Privacy Policy - Terms - Contact Us © soohba.com