Program to implement Insertion Sort in C | Wave the world

Program to implement Insertion Sort in C

/*
   Insertion Sort
   Created By: Pirate
*/

#include<stdio.h>
#include<conio.h>
void insertion(int items[],int n);
void main(){
    int items[500],n,i;

    printf("*** Insertion Sort ***\n");
    printf("\nEnter the number of elements: ");
    scanf("%d",&n);

    printf("\nEnter the %d elements: \n",n);
    for(i = 0; i < n; i++){
        scanf("%d",&items[i]);
    }

    printf("\n\nBefore Sorting:\n");
    for(i = 0; i < n; i++){
        printf("%d\t",items[i]);
    }

    insertion(items,n);

    printf("\n\nAfter Sorting:\n");
    for(i = 0; i < n; i++){
        printf("%d\t",items[i]);
    }
    getch();
}

void insertion(int items[],int n){
    int i,j,temp;
    for(i=0;i<n;i++){
        temp = items[i];
        for(j = i-1; j >= 0; j--){
            if(temp<items[j]){
                items[j+1]=items[j];
            }
            else{
                break;
            }
        }
        items[j+1] = temp;
    }

}






Output :



0 comments:

Post a Comment

 

Pro

About