Selection Sort Data Structure Array C Program

This Program is for Selection Sort on Array. This is part of Mumbai University MCA Colleges Data Structures C program


#include<stdio.h>
#include<conio.h>

void selection(int elements[], int maxsize);
void display(int elements[],int maxsize);
int elements[]={100,25,88,13,76,31,45,68,94,53};
int maxsize=10;

void main()
{
          int i;
          clrscr();
          printf("\nArray before sorting:\n\n");
          for (i = 0; i < maxsize; i++)
                   printf("%d ",elements[i]);
          printf ("\n");

          selection(elements, maxsize);

          printf("\nArray after sorting:\n\n");
          for (i = 0; i < maxsize; i++)
                   printf("%d ", elements[i]);
          getch();
}

void selection(int elements[], int maxsize)
{
          int i, j, k;
          int min, temp,cnt=0;
          for (i = 0; i < maxsize-1; i++)
          {
                   min = i;
                   for (j = i+1; j < maxsize; j++)
                   {
                             if (elements[j] < elements[min])
                             min = j;
                   }
                   temp = elements[i];
                   elements[i] = elements[min];
                   elements[min] = temp;
                   cnt++;
                   if(cnt<=3)
                   {
                             printf("\nElement after pass %d:",cnt);
                             display(elements,maxsize);
                   }


          }
}

void display(int elements[],int maxsize)
{
          int i;

          for(i=0;i<maxsize;i++)
                   printf("  %d ",elements[i]);
          printf("\n");
}

Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

No comments:

Post a comment