Share This

Tech Schtuff

Sort function

BubbleSortworks by repeatedly stepping through the list to be sorted,
comparing two items at a time and swapping them if they are in the wrong order.
The algorithm sort from smaller to bigger element.

maya version(mel)

global proc float[] bubbleSort(float $numbers[])
{
	int $i, $j;
 	float $temp;
 	$array_size = size($numbers);

 	for ($i = ($array_size - 1); $i >= 0; $i--)
 	{
 		for ($j = 1; $j <= $i; $j++)
 		{
 			if ($numbers[$j-1] > $numbers[$j])
 			{
 				$temp = $numbers[$j-1];
 				$numbers[$j-1] = $numbers[$j];
 				$numbers[$j] = $temp;
 			}
 		}
 	}
 	return $numbers;
 }

float $nums[] = {10,4,2};
bubbleSort($nums);
// Result:2 4 10//

float $nums[] = {10,4,2};
bubbleSort($nums);
// Result:2 4 10//
python version(which you dont really need since you can use array.sort())

 def bubblesort(list):
 	max = len(list)-1 
 	for i in range(max,0,-1):
 		for k in range(i):
 			if list[k] > list[k+1]:
 				temp = list[k]
 				list[k] = list[k+1]
 				list[k+1] = temp
 	return list