Mojo Mojo - 11 days ago 5
C++ Question

Trouble manually sorting an array using pointers

I understand this may be a simple process for some of you, but I have been working on this task for the last two weeks and have tried multiple different ways to implement a sorting procedure of an array, in a function, without passing the entire array into the function. I am only allowed to pass a pointer and the size of the array. I'm not looking for the answer to be given to me just some general tips and understanding of passing pointers into functions that control arrays. I've searched the internet and my books but most everything recommends using the premade sort function. however I have to create one myself. Below is, what i feel, the closest i've gotten to the correct code. Any help is greatly appreciated.

#include <iostream>
using namespace std ;
void ptr_sort(int *aptr, int size) ;

int main()
{
int array[5] = { 28, 87, -3, 45, 19 } ;
int size = sizeof(array)/sizeof(array[0]) ;
int a = array[0] ;

cout << "Original array: " ;

for(a = 0 ; a < size ; a++)
{
cout << array[a] << " " ;
}
cout << endl ;

ptr_sort(&a, size) ;

cout << "Sorted array: " ;

for(a = 0 ; a < size ; a++)
{
cout << array[a] << " " ;
}
cout << endl ;

return 0 ;
}


void ptr_sort(int *aptr, int size)
{
int temp ;
int b = *aptr += 1 ;

for(*aptr = 0 ; *aptr < size ; aptr++)
{
if ( *aptr < b)
{
temp = *aptr ;
*aptr = b ;
b = temp ;
}
}
}

Answer

So there are a few problems in your code:

int main()
{
    int array[5] = { 28, 87, -3, 45, 19 } ;
    int size = sizeof(array)/sizeof(array[0]) ;
    int a = array[0] ;

    cout << "Original array: " ;

    for(a = 0 ; a < size ; a++)
    {
        cout << array[a] << " " ;
    }
    cout << endl ;

    ptr_sort(&a, size) ;

You had defined int a and then you assigned to it first item of your array. I guess this is not what you wanted, because at the end of code I've cited You pass pointer to a variable into sorting function. You probably wanted to pass pointer to array to sorting function. Also you use your a variable in for and you completly change its initial value.

void ptr_sort(int *aptr, int size)
{
int temp ;
int b = *aptr += 1 ;

for(*aptr = 0 ; *aptr < size ; aptr++)
{
    if ( *aptr < b)
    {
        temp = *aptr ;
        *aptr = b ;
        b = temp ;
    }

}

Here above there also a few problems:

  1. b actually never changes, so comparing it in in for loop for sorting is a little bit pointless.
  2. you use table's element as iterator in your loop. Not good.
  3. your sorting algoritm seems to be bubnle sort, but lacks of one extra for loop.

Below I attach my version of your program which does a little bit better than your implementation. ptr_sort is not finished - as I seed you need extra for. Compare it with your work and ask if You have any doubts.

#include <iostream>
using namespace std ;
void ptr_sort(int *aptr, int size) ;

int main()
{
int array[5] = { 28, 87, -3, 45, 19 } ;
int size = sizeof(array)/sizeof(array[0]) ;

cout << "Original array: " ;

for(int i = 0 ; i < size ; i++)
{
    cout << array[i] << " " ;
}
cout << endl ;

ptr_sort(array, size) ;

cout << "Sorted array: " ;

for(int i = 0 ; i < size ; i++)
{
    cout << array[i] << " " ;
}
cout << endl ;

return 0 ;
}


void ptr_sort(int *aptr, int size)
{

for(int i = 0 ; i < size - 1; i++)
{
    if ( aptr[i] < aptr[i + 1] )
    {
        std::swap(aptr[i], aptr[i + 1]);
    }
}
}
Comments