Bubble sort

Updated tux4life 0 Tallied Votes 150 Views Share

Probably one of the poorest sorting algorithms ever written but I provide it here and anyone who wants to use it can use it in any way he/she wants :P ...

Usage: bubble_sort(your_array, number_of_elements);

int arr[] = {52,85,2,1,-56,802};
bubble_sort(arr, 6); /* order of elements in arr: {-56, 1, 2, 52, 85, 802} */
/***************************************
     Written by Mathias Van Malderen
***************************************/

template<class T>
void bubble_sort(T *p, int count)
{
     int t;
     for(int i=0; i<count; i++)
     {
	 for(int j=count-1; j>0; j--)
	 {
	      if(p[j] < p[j-1])
	      {
	           // exchange elements
		   t = p[j-1];
		   p[j-1] = p[j];
		   p[j] = t;
	      }
	  }
      }
}