A few sorting algorithms implemented in C I was reading about the elementary sorting algorithms like insertion Sort, selection sort and bubble Sort in Introduction to Algorithms by Cormen and I decided to try and implement it in C.

#include&lt;stdio.h&gt;
#define MAXSIZE 100  //  Maximum number of elements permissible in array

void swap(int*,int*);
void print(int*,int);

int main(void) {
    int arr[MAXSIZE],n,i,j,ch,ord;
    void insertion(int*,int,int),selection(int*,int,int),bubble(int*,int,int);
    printf("\nEnter number of elements for array = ");
    scanf("%d",&amp;n);
    printf("\n\nEnter the array elements one by one : \n\n");
    for(i=0;i&lt;n;i++){
        scanf("%d",&amp;arr[i]);
    }
    printf("\n\nWhat sorting you want to use ?\n\n Insertion (1) , Selection (2) or Bubble (3)\n\nEnter choice = ");
    scanf("%d",&amp;ch);
    printf("\n\nSorting order ? \n\n Ascending (1) or Descending (2)\n\nEnter choice = ");
    scanf("%d",&amp;ord);
    printf("\n\n");
    switch(ch){
        case 1:
            insertion(arr,n,ord);
            break;
        case 2:
            selection(arr,n,ord);
            break;
        case 3:
            bubble(arr,n,ord);
    }
    print(arr,n);
printf("\n\n");
return 0;
}

void swap(int *x,int *y){
    int t=*x;
    *x=*y;
    *y=t;
}

void insertion(int *a,int l,int ord){
    int i,j;
    for(i=1;i&lt;l;i++){
        for(j=i;j&gt;0;j--){
            if((ord==1 &amp;&amp; a[j]&lt;a[j-1]) || (ord==2 &amp;&amp; a[j]&gt;a[j-1]))
                swap(&amp;a[j],&amp;a[j-1]);
            else
                break;
        }
    }
}

void selection(int *a,int l,int ord){
    int index,i,j;
    for(i=0;i&lt;l;i++){
        index=i;
        for(j=i;j&lt;l;j++){
            if((ord==1 &amp;&amp; a[j]&lt;a[index]) || (ord==2 &amp;&amp; a[j]&gt;a[index]))
                index=j;
        }
    swap(&amp;a[i],&amp;a[index]);
    }       
}

void bubble(int *a,int l,int ord){
    int i,j;
    for(i=l;i&gt;0;i--){
        for(j=0;j&lt;i-1;j++){
            if((ord==1 &amp;&amp; a[j]&gt;a[j+1]) || (ord==2 &amp;&amp; a[j]&lt;a[j+1]))
                swap(&amp;a[j],&amp;a[j+1]);
        }
    }
}

void print(int *a,int l){
    int i;
    for(i=0;i&lt;l;i++)
        printf("%d\t",a[i]);
}


I'm looking for feedback / improvements on my code and whether the implementation of the sorting algorithms in the respective function definitions are proper or not.
This is a pretty good straightforward implementation of these algorithms. It's really nice that they're all implemented in-place so no extra memory is needed. It looks pretty good, but I have a few suggestions.

Better Naming

Your function names are pretty good. (I might add "sort" to the various sorting routine names, but it's not a big deal.) However, the variable names are a bit too terse. Normally, I'd object to using arr as the name for an array and name it after what's stored in the array. (Something like grades or names or whatever was being sorted.) In this example case, it's acceptable, but keep that in mind for the future.

It's tradition for i and j to be used for loop control variables, and I don't mind those. But I would change n to numElements. (Or if you have a better name for the array, name it num&lt;whatever's in the array&gt; such as numGrades, numNames, etc.) 

The function parameters a and l should be named with more readable names, like arr and length. ord is reasonable, but I'd prefer order.

Use enum when appropriate

ch is presumably supposed to hold a character, which would be a reasonable name, except that it holds an int. Likewise, ord is supposed to hold the order.

Both of those should be an enumerated type. I'd make one for the sort type and one for the sort order. Something like this:

typedef enum {
    SO_Ascending = 1,
    SO_Descending
} SortOrder;

typedef enum {
    ST_Insertion = 1,
    ST_Selection,
    ST_Bubble
} SortType;


You can then create variables of those types and read the values into them.

Error handling

You may have noticed that the user can enter invalid values, too. You should verify that the values they entered are reasonable and prompt them again if not. To do that, it would probably make sense to break out some of the functionality in main() into separate functions. I suggest something like this:

void promptForArray(int arr [ MAXSIZE ], int* numElements)
{
    do {
        printf("\nEnter number of elements for array = ");
        scanf("%d", numElements);
        if ((*numElements &lt;= 0) || (*numElements &gt; MAXSIZE))
        {
            printf("\nYou entered an invalid value for the number of elements. Please choose a value between 1 and %d\n", MAXSIZE);
        }
    } while ((*numElements &lt;= 0) || (*numElements &gt; MAXSIZE));

    printf("\n\nEnter the array elements one by one : \n\n");
    for(int i = 0; i &lt; *numElements; i++){
        scanf("%d",&amp;arr [ i ]);
    }
}


You could then do a similar thing for getting the sort function and sort order. Something like this:

void promptForSortType(SortType* sortType)
{
    do {
        printf("\n\nWhat sorting you want to use ?\n\n Insertion (1) , Selection (2) or Bubble (3)\n\nEnter choice = ");
        scanf("%d", sortType);
        if ((*sortType &lt; ST_Insertion) || (*sortType &gt; ST_Bubble))
        {
            printf("\nYou entered an invalid sort type. Please enter a value between 1 and 3.\n");
        }
    } while ((*sortType &lt; ST_Insertion) || (*sortType &gt; ST_Bubble));
}


Readability

Do the above things should improve the readability of your code, but you can do more. I recommend leaving spaces around operators (+, -, =, etc.).

This method of introducing function prototypes within main() is very odd:

void insertion(int*,int,int),selection(int*,int,int),bubble(int*,int,int);


Instead, you should put them above main and put them all on separate lines, and you should name the parameters so someone reading them knows what the parameters mean. Something like this:

void insertion(int *arr, const int length, const SortOrder ord);
void selection(int *arr, const int length, const SortOrder ord);
void bubble(int *arr, const int length, const SortOrder ord);


Use const where appropriate

You may have noticed that I made length and ord const in the above examples. They don't change, so it's best to mark them as unchanging. Then the reader and the compiler know more about the intent of the function. You could also make arr const in the print() function.