quick sort would be the way to go imho. You can use the built in qsort function but if you have troubles with the passing of function pointers you could always do a bubble sort. If the array size is always going to be small, you wont see that much of a performance difference. Bubble sort is the simplest sort that will traverse the array in the best case one time (array already sorted) or n-1 times in the worst case (n=# of elements; array sorted in reverse order)
Bubble sort works as follows
// n = # of elements
for(int x = 0;i<n-1;x++)
{
if(array[x]>array[x+])
{
int y = array[x];
array[x] = array[x+1];
array[x+1] = y;
}
}
This is the easiest to follow sorting algorithm and once you understand it, it can be modified to work quicker.
For each traversal of the array it moves the largest element to its proper position
Thank you guys but someone told me that I need two
things. The first is the pointer (which I have), the second is the use of the new operator. For example: int *a = new int[Size]; But it is not working for me.
John, function "fast_swap()" should be "slow_swap()" because it is slower than the 'normal' method. Instead of 3 assignments you need 3 assignments plus 3 XORs.
To side with john, binary methods are lower level and should be executed quicker. I guess it depends on the compiler. However, I have not timed the execution of the process myself and would be curious what the end result was. If I have time today I will check it out.
>To side with john, binary methods are lower level and >should be executed quicker.
That's true, but you need the assignments anyway. The binary operations are just overhead. I tested it with several compilers and did not find one where xor-swap is faster. Please reply if you get different results.
rpet, have you forgotten about *? My procedure is faster, because it works without stars, but directly. And also it does not create in stack local variables, which also takes processor time.
//fastest for using in a function
x^=y^=x^=y; John Fill
>rpet, have you forgotten about *? My procedure is faster, because it works without stars, but directly.
MSVC produces the same code for f(int *) and f(int &).
There's no difference.
>And also it does not create in stack local variables, which also takes processor time.
That's true. Anyway, the XOR code is less efficient.
Please check out this little test program. With VC++6
(release mode) I get the following results:
elapsed xor: ~ 80
elapsed mov: ~ 58
If my test program is correct, and think it is, this shows that swap_mov() is faster. Just a little bit, but it is faster. I tested a while ago with Borland TC++ and get similar results. Please try to reproduce this, John.
#include <time.h>
#include <iostream>
void swap_xor(int &, int &);
void swap_mov(int *, int *);
const unsigned long MAX_LONG = 0xFFFFFFFFL;
int main(int argc, char ** argv, char ** envp)
{
clock_t t0;
int x = 100000, y = 45678;
>Will count that I created a 10 element array on the heap?
No. You assign the address of a[] to ArrayPtr. You should not do this because
a) ArrayPtr does not point to an array on the heap but to an array on the stack
b) The memory that you allocated using "new" is lost. You cannot access it because the pointer is overwritten. If you try to use delete[] ArrayPtr, you try to delete static memory and this may let your program crash.
If you want to fill a dynamic array with a pre-defined set of values you can do the following instead:
[...]
// create array on the heap
const int arraySize=10;
int * ArrayPtr = new int[arraySize];
[...]
// copy values into the array
int a[]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
memcpy(ArrayPtr, a, sizeof a);
[...]
// free the dynamic memory
delete[] ArrayPtr;
>rpet, did you see disassembler code? I believe not.
John, I think you get me wrong. Of course I did. I compiled the test program from my previous post with Visual C++ 6, SP5 on Windows 2000. Setting: Win32 Release, Optimization: Maximize Speed. The following code is from the assembler listing. It is obvious that
a) There is no difference betwenn func(int *) and func(int &)
b) There's an overhead in swap_xor.
>Would you like me to put it in the forum for you to believe me? I saw it.
Please do it. And tell me which compiler, os and settings you have used.
regards,
-- ralf
?swap_xor@@YAXAAH0@Z PROC NEAR ; swap_xor, COMDAT
; 33 : i ^= j ^= i ^= j;
mov ecx, DWORD PTR _j$[esp-4]
mov eax, DWORD PTR _i$[esp-4]
push esi
mov edx, DWORD PTR [ecx]
mov esi, DWORD PTR [eax]
xor esi, edx
mov DWORD PTR [eax], esi
mov edx, esi
mov esi, DWORD PTR [ecx]
xor esi, edx
mov DWORD PTR [ecx], esi
mov edx, DWORD PTR [eax]
mov ecx, esi
pop esi
xor edx, ecx
mov DWORD PTR [eax], edx
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.