Thread: Merge Sort

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    20

    Merge Sort

    Sorry... but I need help with a problem I have I'm writing a Merge Sort for a class presentation but I'm having a problem running it.
    Code:
     
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    #define SIZE 10
    
    
    void mergeSort(int set[] , int length);
    void sortSubArray(int set[] , int low , int high);
    void merge(int set[] , int left , int middle1 , int middle2 , int right);
    void displayElements(int set[] , const int length);
    void displaySubArray(int set[] , int left , int right);
    
    
    int main()
    {
        int set[SIZE];
        int i;
        srand(time(NULL));
    
    
        for(i = 0; i < SIZE; i++)
        {
            set[i] = 10 + rand() % 99;
        }
    
    
        printf("Unsorted array: \n");
        displayElements(set , SIZE);
        printf("\n\n");
        mergeSort(set , SIZE);
        printf("Sorted array:\n");
        displayElements(set , SIZE);
        system("PAUSE");
        return 0;
    }
    
    
    void mergeSort(int set[] , int length)
    {
        sortSubArray(set , 0 , length - 1);
    }
    
    
    void sortSubArray(int set[] , int low , int high)
    {
        int middle1 , middle2;
    
    
        if((high - low) >= 1)
        {
            middle1 = (low + high) / 2;
            middle2 = middle1 + 1;
    
    
            printf("split:   ");
            displaySubArray(set , low , high);
            printf("\n                  ");
            displaySubArray(set , middle2 , high);
            printf("\n\n");
    
    
            sortSubArray(set , low , middle1);
            sortSubArray(set , middle2 , high);
    
    
            merge(set , low , middle1 , middle2 , high);
        }
    }
    
    
    void merge(int set[] , int left , int middle1 , int middle2 , int right)
    {
        int leftIndex = left;
        int rightIndex = middle2;
        int combinedIndex = left;
        int tempArray[SIZE];
        int i;
    
    
        printf("merge:   ");
        displaySubArray(set , left , middle1);
        printf("\n                   ");
        displaySubArray(set , middle2 , right);
        printf("\n");
    
    
        while(leftIndex <= middle1 && rightIndex <= right)
        {
            if(set[leftIndex] <= set[rightIndex])
            {
                tempArray[combinedIndex++] = set[leftIndex++];
            }
            else
            {
                tempArray[combinedIndex++] = set[rightIndex++];
            }
        }
    
    
        if(leftIndex == middle2)
        {
            while(rightIndex <= right)
            {
                tempArray[combinedIndex++] = set[rightIndex++];
            }
        }
        else
        {
            while(leftIndex <= middle1)
            {
                tempArray[combinedIndex++] = set[leftIndex++];
            }
        }
    
    
        for(i = left; i <= right; i++)
        {
            set[i] = tempArray[i];
        }
        printf("          ");
        displaySubArray(set , left , right);
        printf("\n\n");
    }
    
    
    void displayElement(int set[] , const int length)
    {
        displaySubArray(set , 0 , length - 1);
    }
    
    
    void displaySubArray(int set[] , int left , int right)
    {
        int i;
    
    
        for(i = 0; i < left; i++)
        {
            printf(" ");
    
    
            for(i = left; i <= right; i++)
            {
                printf("%d" , set[i]);
            }
        }
    }
    Sorry if that's too long or complicated but the problem is actuall with the displayElement procedure. (I reference it twice in main). I keep receiving a linker error wich relates to that function and I can't understand why. If anyone has the time could they help me out please. I'm in no rush , so no need to put yourself through any trouble.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    So... post the errors you're getting... "a linker error" is about as nebulous as saying "it doesn't work"...

    We're gonna need real information if you want our help.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    1>Merge Sort Demonstration.obj : error LNK2028: unresolved token (0A000012) "void __cdecl displayElements(int * const,int)" (?displayElements@@$$FYAXQAHH@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)


    1>Merge Sort Demonstration.obj : error LNK2019: unresolved external symbol "void __cdecl displayElements(int * const,int)" (?displayElements@@$$FYAXQAHH@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)

    Those 2 errors unresolved symbol and unresolved token , not sure what it means.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Referring to message #1 ...
    Compare line 11, line 33 and line 127 ... notice anything?

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    you have a name mismatch for your function. displayElement vs displayElements

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    Wow, I missed that. Thank you guys

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tsmith94 View Post
    Wow, I missed that. Thank you guys
    That's why we pay attention to compiler and linker errors... 99 of 100 times the error message leads us directly to the problem. In this case the fact that it could not find the displayelements() function told me you'd never defined it, even though you had declared it... most likely cause? Typos.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Merge sort fails to sort
    By Cresh07 in forum C++ Programming
    Replies: 3
    Last Post: 09-23-2009, 11:17 AM
  2. Quick Sort or Merge Sort???
    By swanley007 in forum C++ Programming
    Replies: 6
    Last Post: 11-10-2005, 06:48 PM
  3. Merge Sort
    By osal in forum C++ Programming
    Replies: 3
    Last Post: 06-05-2005, 12:31 PM
  4. Quick sort VS Merge Sort
    By sachitha in forum Tech Board
    Replies: 7
    Last Post: 09-03-2004, 11:57 PM
  5. merge sort and selection sort and time spent on both
    By misswaleleia in forum C Programming
    Replies: 3
    Last Post: 06-04-2003, 02:24 PM

Tags for this Thread