sorting through arrays

This is a discussion on sorting through arrays within the C Programming forums, part of the General Programming Boards category; Hello, I need some help sorting through the values of an array and 'deleting' duplicate values. I'm working with a ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    3

    Post 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.

    Thanks, in advance, for your help =)

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    Probably you can post this question is some other MAC related forums
    like Character arrays in Xcode - Mac Forums

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    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. #4
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64

    Question

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

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    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];
            }
            }
    Last edited by Alexander jack; 03-04-2010 at 11:56 PM.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    37
    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. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    15
    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]);
    }
    Last edited by murugaperumal; 03-05-2010 at 01:15 AM.

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    37
    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. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    3

    Smile

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 12:27 PM
  2. Sorting Arrays
    By DaniiChris in forum C Programming
    Replies: 11
    Last Post: 08-03-2008, 09:23 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  5. sorting arrays
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2001, 06:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21