If you want to remove the duplicate elements use the below code
Code:
#include <stdio.h>
#include <stdlib.h>
void sort(int a[],int n,int);
int i=1;
int data[100];
int n;
main()
{
printf("Enter the maximum number of elements you want to sort\n");
scanf("%d",&n);
printf("Enter the numbers one by one\n");
for (i=0;i<n;i++)
{
scanf("%d",&data[i]);
}
sort(data,n,i);
printf("After sorting the elements are as follows\n");
printf("i val:%d\n",i);
for (i=0;i<n;i++)
{
printf("%d\n",data[i]);
}
n=unique(data,n);
printf("After removing the duplicate elements\n");
for (i=0;i<n; i++)
{
printf("data[%d] = %d\n", i, data[i]);
}
}
void sort(int a[],int n,int i)
{
int j,value;
for (i=1;i<n;i++)
{
value = a[i]; //here it goes to the second element
j = i-1; //go to the 0th index
while(j>=0 && a[j] >value)
{
a[j+1] = a[j];
j = j-1;
}
a[j+1] = value;
}
//removing the duplicate values
}
int unique(int *a, int n)
{
int i, k;
k = 0;
for (i = 1; i < n; i++) {
if (a[k] != a[i]) {
a[k+1] = a[i];
k++;
}
}
return (k+1);
}