#Quick Sort program error

14 messages · Page 1 of 1 (latest)

spark cobalt
#

Im getting segmentation fault error
Please help

#include<stdio.h>
 
void swap(int* a, int* b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}
 
int split(int array[], int low , int high)
{
    int pivot = array[high], point = 0;
    
    for ( int i = 0; i < high; i++)
    {
        if (array[i] <= pivot)
        {
            swap(&array[i], &array[point]);
            point++;
        }
    }
    swap(&array[point + 1], &pivot);
 
    return point + 1;
}
 
void sort(int array[], int low, int high)
{
    int pivot;
    if (low < high)
    {
        pivot = split(array, low, high);
        
        sort(array, 0, pivot);
        sort(array, pivot, high);
    }
 
}
 
int main()
{
    int arr[] = {2, 3, 6, 1, 8}, length = 5;
 
    sort(arr, 0, length - 1);
    
    printf("Swapped: ");
 
    for ( int i = 0; i < length; i++)
    {
        printf("%d",arr[i]);
    }
}
grave timberBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

proper beacon
#

When getting segmentation faults, try running the program with address sanitizer

grave timberBOT
#
How To Use Sanitizers

Sanitizers are tools which generate additional code in your program that can catch many common programming mistakes,
such as:

General Advice

Not all sanitizers can be combined, but when they can, use e.g.:
-fsanitize=address,undefined to combine them.
Always compile with debug info to get line numbers, variable names, etc.

MSVC 19.27+ and VS 2019 16.9+
Sample Program
int main(void) {
    int x;
    return x;
}
`-fsanitize=memory -g` Output

SUMMARY: MemorySanitizer: use-of-uninitialized-value /tmp/test.cpp:3:5 in main
Exiting
(3:5 is line and column of return)

flint nexus
#

;compile -fsanitize=address ```c
#include<stdio.h>

void swap(int* a, int* b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}

int split(int array[], int low , int high)
{
int pivot = array[high], point = 0;

for ( int i = 0; i < high; i++)
{
    if (array[i] <= pivot)
    {
        swap(&array[i], &array[point]);
        point++;
    }
}
swap(&array[point + 1], &pivot);

return point + 1;

}

void sort(int array[], int low, int high)
{
int pivot;
if (low < high)
{
pivot = split(array, low, high);

    sort(array, 0, pivot);
    sort(array, pivot, high);
}

}

int main()
{
int arr[] = {2, 3, 6, 1, 8}, length = 5;

sort(arr, 0, length - 1);

printf("Swapped: ");

for ( int i = 0; i < length; i++)
{
    printf("%d",arr[i]);
}

}

sour starBOT
#
Compiler Output
=================================================================
==1==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffed9695dc4 at pc 0x0000004011ed bp 0x7ffed9695c80 sp 0x7ffed9695c78
READ of size 4 at 0x7ffed9695dc4 thread T0
    #0 0x4011ec in swap /app/example.c:6
    #1 0x401448 in split /app/example.c:23
    #2 0x4014be in sort /app/example.c:33
    #3 0x4016b9 in main /app/example.c:45
    #4 0x7f9f3632a082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082)
    #5 0x4010ed in _start (/app/output.s+0x4010ed)

Address 0x7ffed9695dc4 is located in stack of thread T0 at offset 52 in frame
    #0 0x4014fc in main /app/example.c:42

  This frame has 1 object(s):
    [32, 52) 'arr' (line 43) <== Memory access at offset 52 overflows this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
      (longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-buffer-overflow 
grave timberBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.

spark cobalt
#

I cant fix it

#

Where is the problem

tacit prawn
#

@spark cobalt
swap(..., &pivot); doesn't do what you think it does

#

also, point and i should start from low, not from 0

#

and if you're keeping the pointer to the spot after the low elements of the array, then you should not have the +1s. tbh this partition scheme probably doesn't even work if you point to the element after the low elements of the array. i've always seen it done with a pointer to the last low element. but i'm more familiar with Hoare's partition scheme than Lomuto's.

#

i really don't like the insistence on maintaining the range [from, to], it's much more natural for me to use [from, to). regardless, in your implementation,

    sort(array, 0, pivot);
    sort(array, pivot, high);

is wrong for two reasons. again, start from low, not from 0. and pivot is in its proper place, you should not have to include it in any further sorts. that should be

    sort(array, low, pivot-1);
    sort(array, pivot+1, high);
grave timberBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.