1

I have written this code for Bubble Sort using Pointers, I'm not getting output. Here is my code. Please fix this code. I am getting error basically in swapping syntax. Please help.

int sort_array(int n, int *a[]){
    for (int i = 1; i <= n - 1; i++) {
        for (int j = 0; j <= n - i - 1; j++) {
            if (*(a[i]) < *(a[j + 1])) {
                int temp = *(a[j]);
                *(a[j]) = *(a[j + 1]);
                *(a[j + 1]) = temp;
            }
        }
    }
}

int main() {
    int n;
    scanf("%d", &n);
    long int a[n];
    for (int i = 0; i <= n; i++) {
        scanf("%d", &a[i]);
    }
    sort_array(n, &a[n]);
    for (int i = 0; i <= n; i++) {
        printf("%d", a[i]);
    }
}
4
  • 4
    int *a[] is already wrong; that's synonymous with int **a which is not what you're providing. (and your indexing in main and sort_array is invoking undefined behavior). You need to turn up your warnings and treat all of them as errors. Commented Sep 14, 2022 at 10:23
  • 1
    You know that array indexes are zero-based, but you seem to have forgotten that means the indexes end with one less than the size. So an array of n elements will have indexes from 0 to n - 1. If you use n as an index that will be out of bounds and lead to undefined behavior. Commented Sep 14, 2022 at 10:23
  • And types matter. You have a long int[] in main but try to pass a long* on to an int** Commented Sep 14, 2022 at 10:36
  • 2
    Do they not teach you how to use the keyboard's space bar? It's not worth trying to read code written this densely... Commented Sep 14, 2022 at 10:37

2 Answers 2

2

The code is full of bugs.

You declared a variable length array

long int a[n];

with n elements of the type long int.

Thus in this loop

for(int i=0;i<=n;i++){
    scanf("%d",&a[i]);
}

the index i equal to n will be a reason of accessing memory outside the defined array.

Also the conversion specification %d is used for objects of the type int instead of long int.

So the loop must look like

for(int i=0;i < n;i++){
    scanf("%ld",&a[i]);
}

Similar problems exist in this for loop

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

that must be rewritten like

for(int i=0;i < n;i++){
    printf("%ld ",a[i]);
}

In this call

sort_array(n,&a[n]);

the second argument expression has the type long int * and points to a non-existent element of the array with the index equal to n. That is the pointer expression points outside the array.

On the other hand, the corresponding function parameter

int sort_array(int n,int *a[]){

in fact has the type int **. The compiler shall issue an error message that the argument expression can not be converted to the type of the parameter.

Also the function has the return type int but returns nothing.

The function should be declared like

void sort_array( long int a[], size_t n );

Due to this for loop

for(int i=1;i<=n-1;i++){

the function will not even try to sort an array containing two elements because this if statement

if(*(a[i])<*(a[j+1])){.

will look like

if(*(a[1])<*(a[1])){

In any case the if statement

if(*(a[i])<*(a[j+1])){
    int temp=*(a[j]);
    *(a[j])=*(a[j+1]);
    *(a[j+1])=temp;
}

does not make a sense. There are compared elements with indices i and j + 1 but there are swapped elements with indices j and j + 1.

Thus the code shall be entirely rewritten. You shall do that yourself.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for helping me i'm still learning about pointers, now i can understand my mistakes
0

In addition to all valid remarks by Vlad from Moscow:

  • you should always test the return value of scanf() to detect missing or invalid input,
  • temp must be defined with type long int,
  • the test in the nested loop should be if (a[j] > a[j + 1])

Here is a modified version:

#include <stdio.h>

void sort_array(long int a[], size_t n) {
    for (size_t i = 0; i < n; i++) {
        for (size_t j = 0; j < n - i; j++) {
            if (a[j] > a[j + 1]) {
                long int temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
            }
        }
    }
}

int main() {
    int n;
    if (scanf("%d", &n) != 1 || n <= 0) {
        fprintf(stderr, "invalid input\n");
        return 1;
    }
    long int a[n];
    for (int i = 0; i < n; i++) {
        if (scanf("%ld", &a[i]) != 1) {
            fprintf(stderr, "invalid input\n");
            return 1;
        }
    }
    sort_array(a, n);
    for (int i = 0; i < n; i++) {
        printf("%ld ", a[i]);
    }
    printf("\n");
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.