-->

Monday, March 24, 2014

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

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

‘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;
    }    
}

Read other related articles

Also read other articles

© Copyright 2013 Computer Programming | All Right Reserved