Let Us C (Chapter 8 : Arrays) : Program-3

(c) Implement the Selection Sort, Bubble Sort and Insertion sort algorithms on a set of 25 numbers.

let us c chapter 8 arrays problem 3
// Let Us C (Chapter 8 : Arrays) : Program-3
/* Implement the Selection Sort, Bubble Sort and Insertion sort algorithms on a set of 25 numbers */
#include<stdio.h>
#define N 5
int arr[10],arr_SS[10],arr_BS[10],arr_IS[10],i,j,k,m,temp,lowest;
void main()
{
    clrscr();
    printf("Program to implement Selection/Bubble/Insertion sort\n"); 
    printf("Enter %d No.s separated by space\n",N);
    for(i=0;i<N;i++)
        scanf("%d",&arr[i]);

//*************** Selection sort ******************
    for(i=0;i<N;i++)
        arr_SS[i] = arr[i];
    for(i=0;i<N-1;i++)
    {
        for(j=i+1;j<N;j++)
        {
            if(arr_SS[j] < arr_SS[i])
            {
                temp=arr_SS[i];
                arr_SS[i]=arr_SS[j];
                arr_SS[j]=temp;
            }
        }
    }
    printf("\nAfter Selection Sort, array is\n");
    for(i=0;i<N;i++)
        printf("%d,",arr_SS[i]);

//************ Bubble Sort  *****************
    for(i=0;i<N;i++)
        arr_BS[i] = arr[i];
    for(i=0;i<N-1;i++)
    {
        for(j=0;j<N-1;j++)
        {
            if(arr_BS[j+1] < arr_BS[j])
            {
                temp=arr_BS[j];
                arr_BS[j]=arr_BS[j+1];
                arr_BS[j+1]=temp;
            }
        }
    }
    printf("\nAfter Bubble Sort, array is\n");
    for(i=0;i<N;i++)
        printf("%d,",arr_BS[i]);

//************ Insertion Sort ****************
    for(i=0;i<N;i++)
        arr_IS[i] = arr[i];
    for(i=0;i<N;i++)
    {
        for(j=i;j>0;j--)
        {
            if(arr_IS[j] < arr_IS[j-1])
            {
                temp = arr_IS[j];
                arr_IS[j] = arr_IS[j-1];
                arr_IS[j-1] = temp;
            }            
        }
    }
    printf("\nAfter Insertion Sort, array is\n");
    for(i=0;i<N;i++)
        printf("%d,",arr_IS[i]);
}


OUTPUT
let us c chapter 8 arrays problem 3 output





Comments

Popular posts from this blog

Chapter 5 : Functions & Pointers (Let us C)

Let Us C (Chapter 8 : Arrays) : Program-9

Chapter 3: The Loop Control Structure (Let Us C)