Search

Monday, March 24, 2014
0 comments

'C' Function to implement 'selection sort' in ascending and descending order

9:11 AMMonday, March 24, 2014

‘C’ function to implement ‘selection sort’ in ascending order.


void se1_sort_ao(int arr[],int n)
{
    int i, j, min, mpos;
    for(i=0;i<n-1;i++)
    {
    min = arr[i];    mpos = i;
    for(j=i+1;j<n;j++)    /*selection of min element */
     if(arr[j] < min)
     {
      min = arr[j]; mpos =j;
     }
    arr[mpos] = arr[i];         /*replacing min element */
    arr[i] = min;
    }    
}

‘C’ function to implement ‘selection sort’ in descending order.

void se1_sort_ao(int arr[],int n)
{
    int i, j, max, mpos;
    for(i=0;i<n-1;i++)
    {
    max = arr[i];    mpos = i;
    for(j=i+1;j<n;j++)    /*selection of max element */
     if(arr[j] > max)
     {
      max = arr[j]; mpos =j;
     }
    arr[mpos] = arr[i];         /*replacing max element */
    arr[i] = max;
    }    
}

Protected by Copyscape Online Copyright Protection Software

0 comments :

Post a Comment

 
Toggle Footer
Top