Can't think of what this would be called but I don't think it is a bubble sort that you've got there (maybe it is, I can't remember the particular behavior of all the sorts I had to study in school: bubble, selection, insertion, radix, merge, quick sort, etc...). Yes you can do this. Say you have an array of 4 integers to sort. With some modifications, your code could be put into a function like so:
Code:
void MySort( int* pointer)
{
int i, p, temp;
for( i = 0; i < 3; i++ ) // Needs to be one less than size of array
for( p = i+1; p < 4; p++ )
if( pointer[i] > pointer[p] ) // Or "if( *(pointer+i) > *(pointer+p) )
{
temp = pointer[i]; // Or "temp = *(pointer+i);"
pointer[i] = pointer[p]; // Or "*(pointer+i) = *(pointer+p);"
pointer[p] = temp; // Or "*(pointer+p) = temp;"
}
}
You can call it like so:
Code:
int Array[4] = { 18, 12, 14, 16 };
MySort( Array ); // Array will now be 12, 14, 16, 18 after call