Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
05/15/08 18:59
Read: times


 
#154800 - Sorts
Responding to: ???'s previous message
Here are some sorts I only post them because I found them on Goggle since I was on a short dead line

You should really read up on the concepts since there are many sorts with there own pluses and minuses

void bubblesort(BYTE *array, INT8U elements)
{
	INT8U i;
	INT8U j;
	INT8U temp;

	for(i = (elements - 1); i> 0; i--)
	{
		for(j=0; j < i; j++)
		{
			if(array[j] > array[j+1])
			{
				temp = array[j];
			   array[j] = array[j+1];
				array[j+1] = temp;				
			}	
		} 
	}

}


Then the selection sort ( two versions no I do not remember why):


//***********************************************

//***********************************************
/*
void selectionSorta(INT8U *array, INT8U size)
{
	INT8U i;
	INT8U j;
	INT8U min;
	INT8U temp;

  for (i = 0; i < size - 1; i++)
  {
    min = i;
    for (j = i+1; j < size; j++)
	 {
      if (array[j] < array[min])
        min = j;
	 }
	// swap
	temp = array[j];
	array[j] = array[j+1];
	array[j+1] = temp;				
  }
}
*/

//***********************************************

//***********************************************
/*
void selectionSort(INT8U *array, INT8U size)
{
	INT8U i;
	INT8U j;
	INT8U index_of_max;
	INT8U temp;

	for(i=0; i<size; i++)
	{
		index_of_max = i;
		for(j=i; j<size; j++)
		{
			if(array[index_of_max]>array[j])
			{
				index_of_max = j;
			}
		}
		temp = array[i];
		array[i] = array[index_of_max];
		array[index_of_max] = temp;

	}
}


I too was doing median and settled on the insertion sort Note tha you can do this sort one at a time while you are waiting for the next A/D Sample. Additionally you only need to store half the items for a median. Anything above or below the median value falls off the value you need is now the first or last in the array. ( As suggested by the C Group on USNET

//***********************************************
// Insert into array assending, Highest element falls off
// Preclear to Lowest value
// 8,9,10,11,12,...
//***********************************************
/*
void insertionSortAscending(INT8U value, INT8U *array, INT8U size)
{
	INT8U i, j;

	i = 0;
	do
	{
		if(value < array[i])			// find proper place
		{
			for(j=size-1; j>i; j--)	// Move other elements up to make a space
			{
				array[j] = array[j-1];
			}
			array[i] = value;			//and save save new element
			break;						// done
		}
		i++;
	}
	while(i < size);
}
*/
//***********************************************
// Insert into array Descending, lowest element falls off
// Preclear to Lowest value
// 12,11,10,9,8,...
//***********************************************
void insertionSortDescending(INT8U value, INT8U *array, INT8U size)
{
	INT8U i, j;

	i = 0;
	do
	{
		if(value > array[i])			// find proper place
		{
			for(j=size-1; j>i; j--)	// Move other elements up to make a space
			{
				array[j] = array[j-1];
			}
			array[i] = value;			//and save save new element
			break;						// done
		}
		i++;
	}
	while(i < size);
}





List of 13 messages in thread
TopicAuthorDate
Sort array data elements?            01/01/70 00:00      
   Sort and Search?            01/01/70 00:00      
      Neil.....            01/01/70 00:00      
         64 samples is big for a median filter ...            01/01/70 00:00      
            No I haven't.....only because            01/01/70 00:00      
               Google for "sorting algorithms"            01/01/70 00:00      
               Ok, first things first then.            01/01/70 00:00      
                  The MPU and LDO are all in order            01/01/70 00:00      
                     Quantization error ?            01/01/70 00:00      
                        the step size dependacy makes its problematic            01/01/70 00:00      
         Sorts            01/01/70 00:00      
   ???            01/01/70 00:00      
      Couldn't sort it...            01/01/70 00:00      

Back to Subject List