Discussion Forums > Technology

C++ help

<< < (5/5)

geoffreak:

--- Quote from: ant900 on July 23, 2009, 01:48:00 AM ---Also that bubble sort is pretty ugly IMO, you should try using a faster algorithm or clean that up.

--- End quote ---
Try writing a bubble sort in assembly. Now THAT is fun ;)

Even though you can use pointers in C++, I would recommend that you pass by reference (&) to make your life simpler. This will also make your programming feel a bit more like Java, in case you have any experience with it.

ant900:

--- Quote from: geoffreak on July 24, 2009, 03:13:26 AM ---
--- Quote from: ant900 on July 23, 2009, 01:48:00 AM ---Also that bubble sort is pretty ugly IMO, you should try using a faster algorithm or clean that up.

--- End quote ---
Try writing a bubble sort in assembly. Now THAT is fun ;)

--- End quote ---


bah childs play, try quick sort :)

per:
As for the sort, are you allowed to use the standard libraries? If so, there are plenty of sort methods available..

It's generally speaking better to use one than write your own, unless you need to.

Or roll a heapsort using the heap classes.

Or a simple qsort.

Hm.


--- Code: ---
void qsort( double arr[], int num )
{
    double tmp;
    if( num <= 1 )
       return;
 
    for( int i=1, int j = 0; i<num;  i++ )
       if( arr[i] < arr[0] )
       {
           tmp = arr[i];
           arr[i] = arr[++j];
           arr[j] = tmp;
       }
   tmp = arr[j];
   arr[j] = arr[0];
   arr[0] = tmp;

   qsort( x, j );
   qsort(x+j+1, n-j-1);
}

--- End code ---

Verifying if it actually works (I wrote it from memory) is left as an excersise for the reade. :-)

ant900:

--- Quote from: per on August 04, 2009, 09:14:26 PM ---As for the sort, are you allowed to use the standard libraries? If so, there are plenty of sort methods available..

It's generally speaking better to use one than write your own, unless you need to.

Or roll a heapsort using the heap classes.

Or a simple qsort.

Hm.


--- Code: ---
void qsort( double arr[], int num )
{
    double tmp;
    if( num <= 1 )
       return;
 
    for( int i=1, int j = 0; i<num;  i++ )
       if( arr[i] < arr[0] )
       {
           tmp = arr[i];
           arr[i] = arr[++j];
           arr[j] = tmp;
       }
   tmp = arr[j];
   arr[j] = arr[0];
   arr[0] = tmp;

   qsort( x, j );
   qsort(x+j+1, n-j-1);
}

--- End code ---

Verifying if it actually works (I wrote it from memory) is left as an excersise for the reade. :-)

--- End quote ---


--- Code: ---
void QuickSort(int* _Array,int low, int high)
{
// i==low end iterator
// j==high end iterator
// cur=pivot
int i=low,j=high,cur=_Array[(low+high)/2];

// sort into 2 piles
while(i<=j)
{
while(_Array[i]>cur) i++;
while(_Array[j]<cur) j--;

if(i<=j)
Swap(&_Array[i++],&_Array[j--]);
}

// sort small pile
if(low<j)  QuickSort(_Array,low,j);
// sort large pile
if(i<high) QuickSort(_Array,i,high);
}

--- End code ---

here is one I wrote a while ago, and i know that it does work, so have fun

Navigation

[0] Message Index

[*] Previous page

Go to full version