简体   繁体   中英

C++ bubble sort function with pointers

I'd like to write a program that contains bubble sorting with pointers inside a function. This is my code:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>

void rendez(int* t, int n){
    for(int i=0; i<n-1; i++){
        for(int j=i+1; j<n; j++){
            if(*t+i<*t+j){
                int temp = *t+i;
                *t+i = *t+j;
                *t+j = temp;
            }
        }
    }
}

int main(){

    setlocale(LC_ALL,"");

    int t[10] = {2,3,4,5,6,7,8,9,10,11};

    rendez(&t,sizeof(t)); 

    printf("\n");
    system("pause");
}

It's giving me these errors:

C:\Users\metal\gyakorlás1211.cpp    In function 'void rendez(int*, int)':
C:\Users\metal\gyakorlás1211.cpp    [Error] lvalue required as left operand of assignment
C:\Users\metal\gyakorlás1211.cpp    [Error] lvalue required as left operand of assignment
C:\Users\metal\gyakorlás1211.cpp    In function 'int main()':
C:\Users\metal\gyakorlás1211.cpp    [Error] cannot convert 'int (*)[10]' to 'int*' for argument '1' to 'void rendez(int*, int)'

Thanks!

Two changes you need

      if(*(t+i)<*(t+j)){
            int temp = *(t+i);
            *(t+i) = *(t+j);
            *(t+j) = temp;
        }

And also

rendez(t,sizeof(t)/sizeof(t[0])); 

Now look what you did earlier, first of all your compiler must have bursted with you lots of warning.

&t is int (*)[10] which is not what you want in this place.

rather you simply want to pass the array which will eventually decay into pointer and then any changes you do that will reflect to the array.

Earlier your *t+i was doing something like this, (*t)+i is this what you wanted? And moreover t was then int(*)[10] so you are basically adding i or j to it. It was not right. You were working with addresses but you wanted to work with values.

And the second parameter to the function, you wanted to pass the size of the array but not in number of bytes but rather number of elements.

sizeof (arr) is basically say 10*sizeof(int) as it contains int . But is this what you want? No. You want to pass the number of int elements. So just divided it by size of each int . That's what we did in sizeof(t)/sizeof(t[0]) .

When you are sending &t in arguments, you're not actually sending the first address of the array, but the address of the whole array.

Let's understand this thing
Let's say the array is allocated as 1000, 1004, 1008. . . so, the address of the first element will be 1000 as when you add 1 in it, it will give you 1004 . and the address of the whole array will be 1000 as when you add 1 in it, it will give the next address of the last element in the array.

t is the pointer to the first element and &t is the pointer to the array. So to pass the array to the other function just send t in the argument.

Now, sizeof operator does not return the length of the array, but the size it allocated.

Read this and write your function call as.

rendez(t, sizeof(t)/sizeof(t[0]));


Now about the lvalue error.

* has higher priority than + so * executes first. So writing like this *t + i will give you the ith next address from the first element of the array.
So you need to write like *(t + i) in assignment operation.

This call

rendez(&t,sizeof(t)); 

is wrong because the expression &t has type int (*)[10] instead of the type int * that is the type of the first parameter of the function rendez .

You should call the function like

rendez( t, sizeof(t) / sizeof( *t ) ); 

And the second parameter of the function should have type size_t .

also within the function this if statement

        if(*t+i<*t+j){
            int temp = *t+i;
            *t+i = *t+j;
            *t+j = temp;
        }

is also wrong.

It must to look like

        if( *( t + i ) < *( t + j ) ){
            int temp = *( t + i );
            *( t + i ) = *( t + j );
            *( t + j ) = temp;
        }

Take into account that the bubble sort algorithm compares adjacent elements.

Your function implementation looks like an inefficient selection sort.

If you want to use only pointers then the function can look as it is shown in the demonstrative program

#include <iostream>

void rendez( int *a, int n )
{
    for ( int *first = a, *swapped = a, *last = a + n; !( last - first < 2 ); first = a, last = swapped )
    {
        swapped = a;
        while ( ++first != last )
        {
            if ( *( first - 1 ) < *first )
            {
                swapped = first;
                int tmp = *first;
                *first = *( first - 1 );
                *( first - 1 ) = tmp;
            }
        }
    }        
}


int main() 
{
    int a[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };

    for ( int x : a ) std::cout << x << ' ';
    std::cout << std::endl;

    rendez( a, sizeof( a ) / sizeof( *a ) );

    for ( int x : a ) std::cout << x << ' ';
    std::cout << std::endl;

    return 0;
}

Its output is

2 3 4 5 6 7 8 9 10 11 
11 10 9 8 7 6 5 4 3 2 

The function indeed implements the bubble sort algorithm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM