1. ## sorting through arrays

Hello,

I need some help sorting through the values of an array and 'deleting' duplicate values.

I'm working with a 1-d array for now, programming with Xcode 3.2.1, and obviously am not an advanced C-Programmer.

2. Probably you can post this question is some other MAC related forums
like Character arrays in Xcode - Mac Forums

3. Thanks for the reply & link (i'll be sure to check it out), but my question is actually pretty general.. I just said I'm using Xcode for curiosity's sake.

4. Always loops are used to sort array.Whether you want to remove the duplicate elements using the same loop which sorting the array.

5. Hope this code would give some general idea to achieve your requirement ,
Code:
```// simple sorting algorithm
void bubble(int a[],int n)
{
int i,j,t;
for(i=n-2;i>=0;i--)
{
for(j=0;j<=i;j++)

{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}

}//end for 1.

}//end function.

main()
{

int a[100],n,i;

int final_array[100];

// Initialise the array by -1
for( i=0;i<=100;i++)  {
final_array[i]=-1 ;
}
printf("\n\n Enter integer value for total no.s of elements to be sorted: ");
scanf("%d",&n);

for( i=0;i<=n-1;i++)
{ printf("\n\n Enter integer value for element no.%d : ",i+1);
scanf("%d",&a[i]);
}

bubble(a,n);

printf("\n\n Finally sorted array is: ");
int max=a[n-1] ;
printf("max : %3d\n",max);

// Get the value and remove duplicate elements
for( i=0;i<=n-1;i++)  {
printf("%3d" , a[i]);
}

int var = 0 ;
printf("\n------------\n" );
// remove duplicate value
for( i=0;i<=n-1;i++)  {

//printf("comparing : %d == %d \n" , var , a[i-1]);
if ( i!=0 && var!=a[i] )
{
printf("%3d" , a[i]);
var =a[i];
}
else if(i==0) {
printf("%3d" , a[i]);
}
var=a[i];
}
}```

6. Using insertion sorting algorithm we can sort in the following way.
Code:
```#include <stdio.h>
#include <stdlib.h>
void sort(int a[],int n,int);

int i=1;
int data[100];
main()
{
int n;
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]);
}
}
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;
}

}```

7. Code:
```#include<stdio.h>
void bubble(int [],int n );
main()
{
int n;
int i;
int a[100];
int val;
printf("enter the n values :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the value:");
scanf("%d",&val);
a[i]=val;
}
bubble(a,n)
}

void bubble(int arr[],int n )
{
int i , j ;
int temp;
for(j=0;j<n;j++)
{
for(i=j;i<n-1;i++)
if(arr[j]>arr[i+1])
{
temp=arr[j];
arr[j]=arr[i+1];
arr[i+1]=temp;
}
}
for(i=0;i<n;i++)
printf("%d\n",arr[i]);
}```

8. 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);
}```

9. Wow this is awesome, thank you guys so much for posting such helpful stuff. Time to have some fun analyzing xD