/*********************************************************************
* Program to sort numbers using counting sort and save
* the o/p in a file using FILE handling
**********************************************************************/
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 500
void selection(int elements[], int maxsize);
int elements[MAXSIZE],maxsize;
void main()
{
int i;
FILE *f;
printf("\nHow many elements you want to sort: ");
scanf("%d",&maxsize);
printf("\nEnter the values one by one: ");
for (i = 0; i < maxsize; i++)
{
printf ("\nEnter element %i :",i);
scanf("%d",&elements[i]);
}
f=fopen("a.txt","w");
if(f==NULL)
{
printf("\nUnable to write in to the file\n");
exit(1);
}
//fprintf(f,"\nArray before sorting:\n");
//for (i = 0; i < maxsize; i++)
//fprintf(f,"[%i], ",elements[i]);
//fprintf (f,"\n");
selection(elements,maxsize);
//fprintf(f,"\nArray after sorting:\n");
fprintf(f,"\nSorted order is:\n");
for (i = 0; i < maxsize; i++)
fprintf(f,"[%i], ", elements[i]);
}
void selection(int elements[], int array_size)
{
int i, j, k;
int min, temp;
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;
}
}
* Program to sort numbers using counting sort and save
* the o/p in a file using FILE handling
**********************************************************************/
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 500
void selection(int elements[], int maxsize);
int elements[MAXSIZE],maxsize;
void main()
{
int i;
FILE *f;
printf("\nHow many elements you want to sort: ");
scanf("%d",&maxsize);
printf("\nEnter the values one by one: ");
for (i = 0; i < maxsize; i++)
{
printf ("\nEnter element %i :",i);
scanf("%d",&elements[i]);
}
f=fopen("a.txt","w");
if(f==NULL)
{
printf("\nUnable to write in to the file\n");
exit(1);
}
//fprintf(f,"\nArray before sorting:\n");
//for (i = 0; i < maxsize; i++)
//fprintf(f,"[%i], ",elements[i]);
//fprintf (f,"\n");
selection(elements,maxsize);
//fprintf(f,"\nArray after sorting:\n");
fprintf(f,"\nSorted order is:\n");
for (i = 0; i < maxsize; i++)
fprintf(f,"[%i], ", elements[i]);
}
void selection(int elements[], int array_size)
{
int i, j, k;
int min, temp;
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;
}
}