Thread: problem with sorting array according to different array

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    241

    problem with sorting array according to different array

    hey guys,
    I tried to sort an array according to another array, and although nothing wrong was in the syntex nor the logic,
    the application did not work.
    for example, If I try to sort an array with bubble sorting , it works fine:

    Code:
      
    #include <stdio.h>
    int main (void){
    int arr[]={5,3,7,9,10};
    int i,j;
    
    for (i=0;i<5;i++)
    for (j=0;j<4;j++) //I know it could be optimized, it's good enough for the example
    if (arr[j]>arr[j+1]){
    int x=arr[j];
    arr[j]=arr[j+1];
    arr[j+1]=x;
    }
    
    return 0;

    but now I have 2 arrays - one is strings array (2 dimentional array of char, or array of cahr pointers, I tried them both) and the other
    is integers array.
    I try to sort the integer array according to the first char of the string array . there IS something happening there - but
    aventually the array is sorted wrong.
    I googled this problem and I saw many people refering to this issue as common bug in C language. the code itself is good and should
    work. I don't know how to pass this issue.
    help is needed please.

    Code:
    #include <stdio.h>
    
    int main (void){
    
    char *arr[]={"zebra","owl","giraff","ants","pony"};
    int arr2[]={   1,      2,     3,      4,      5};
    int i,j;
    
    for (i=0;i<5;i++)
        for (j=0;j<4;j++) //I know it could be optimized, it's good enough for the example
            if ((int)arr[j][0]>(int)arr[j+1][0]){
                int x=arr2[j];
                arr2[j]=arr2[j+1];
                arr2[j+1]=x;
                }//the only difference from the last example that this is not the same array.
    
    for (i=0;i<5;i++)
        printf ("%d\n",arr2[i]);
    
    return 0;
    }
    the output :
    2
    3
    4
    1
    5

    thanks in advanced

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You've got to swap things around in both arrays, or else you're immediately breaking the connection between zebra<->1, owl<->2, and so on.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    The if statement needs to use the arr2 values as indexes to arr in order to compare by the the first characters in arr[], but it's arr[arr2[] -1]][0] since you initialized arr2 with numbers from 1 to n instead of from 0 to n-1. You could have used 0 to n-1 in arr2[], then added 1 to those values during the output of arr2.

    Code:
                if (arr[arr2[j]-1][0] > arr[arr2[j+1]-1][0]){
    Last edited by rcgldr; 03-04-2014 at 09:56 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting array problem :)
    By BEST in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2009, 01:57 PM
  2. sorting structured array problem
    By matrix0978 in forum C Programming
    Replies: 7
    Last Post: 11-12-2009, 01:42 AM
  3. Array Sorting problem
    By ___________ in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 12:17 AM
  4. array sorting problem?
    By vutek0328 in forum C Programming
    Replies: 14
    Last Post: 09-12-2006, 11:07 AM
  5. Problem with sorting an array
    By lostminds in forum Game Programming
    Replies: 2
    Last Post: 04-24-2002, 09:27 AM