Merge and sort array.

This is a discussion on Merge and sort array. within the C Programming forums, part of the General Programming Boards category; Hi all. basically I need to hand up a project on merge and sort array in char. First input: a ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    28

    Merge and sort array.

    Hi all. basically I need to hand up a project on merge and sort array in char.
    First input: a c e g
    Second input: b d f h
    Output: a b c d e f g h
    I found this code using bubble sort. But i don't understand some of the line mean. so can someone explain out for me? or is there any easy way i can code myself without using bubble sort?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    void sort(char data[], int length)
    {
       int end = length - 1;
       for (int i = 0; i < length; ++i)
       {
           for (int j = 0; j < end; ++j)
           {
               if (data[j] > data[j+1])
               {
                   char tmp = data[j];
                   data[j] = data[j+1];
                   data[j+1] = tmp;
               }
           }      
           --end;
       }
    }
    
    int main(void)
    {
       char firstArray[100];
       char secondArray[100];
       char mergedArray[200];
    
       int i;
       int mergedIndex;
    
       printf("Put in the first array\n");
       fgets(firstArray, sizeof(firstArray), stdin);
    
       printf("Put in the second array\n");
       fgets(secondArray, sizeof(secondArray), stdin);
    
       // Copy the first array into the merged array
       for(i = 0; firstArray[i] != '\n'; ++i)
       {
           mergedArray[i] = firstArray[i];
       }
       mergedIndex = i;
       for(i = 0; secondArray[i] != '\n'; ++i)
       {
           mergedArray[mergedIndex++] = secondArray[i];
       }
       mergedArray[mergedIndex] = 0;
    
       sort(mergedArray, mergedIndex);
       printf("Merged array is '%s'\n", mergedArray);
       system("pause");
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Forget Bubble sort - Merge sort is a different species of sorting algorithm.

    Best if you read up on the different ways to have a Merge sort, on Wikipedia, and then give it a try. If you run into trouble, post back.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    But you said to merge, so don't you need merge sort? Check the links on top for explanation.

    For the bubblesort, a nice explanation can be found in the links here.

    On the code you provided, what you do not understand?

    Also mind, that you can use qsort, which is implemented in stdlib.h, but I wouldn't recommend it, if you are a beginner
    Last edited by std10093; 01-30-2013 at 08:30 AM. Reason: broken url
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It has to be Merge sort. He can't use qsort() or Bubble sort, or any other sort, for this assignment.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    That's why I asked him in my first line of the post. I guess you are right.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,070
    The standard way is qsort. If there is not a reason to implement your own sorting algorithm, it's best to use that.

    The algorithm used by qsort is not specified, by the way, but it's most likely either merge sort or randomized quicksort. Also note that the OP said to "merge and sort" the arrays. It doesn't say "use merge sort to sort and merge two arrays". Using qsort would therefore fulfill the requirements.

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    well i found merging number is way simple than character. i have no idea how to start coding.

    Ops: sorry for misunderstanding. any method will do as what my lecturer told me. But I'm looking for the simple method to code out if i can.
    Last edited by Alexie; 01-30-2013 at 08:24 AM.

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    First, state if you need to use merge sort or any sorting algorithm.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    "Any method", sounds like Bubble sort would be fine. Maybe sort both arrays with Bubble sort, and then merge the two arrays together?

    Is that the assignment?
    Last edited by Adak; 01-30-2013 at 08:30 AM.

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    Adak is right.

    See the pseudocode here and start coding. This way, you will learn. If you don't want to learn, you can click on the link I provided in post #2 and cheat, copying paste it. But that way you won't learn something. I recommend you to try coding it yourself by looking at the pseudocode and then if you are in severe trouble, take a look at the link I provided on post #2 and then post back.

    See this video for a simulation.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  11. #11
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    Yup it is a assignment. But i want to code out a easiest way cause i don't really understand the top part.
    Code:
       int end = length - 1;
       for (int i = 0; i < length; ++i)
    
       {
           for (int j = 0; j < end; ++j)
           {
               if (data[j] > data[j+1])
               {
                   char tmp = data[j];
                   data[j] = data[j+1];
                   data[j+1] = tmp;
               }
           }      
           --end;
    Last edited by Alexie; 01-30-2013 at 08:48 AM. Reason: typo

  12. #12
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    Okay thanks. I will try to code out myself first. It is good to learn something new.

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    Quote Originally Posted by Alexie View Post
    Okay thanks. I will try to code out myself first. It is good to learn something new.
    That's the spirit!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  14. #14
    Registered User
    Join Date
    Nov 2012
    Posts
    1,070
    Quote Originally Posted by Alexie View Post
    Yup it is a assignment. But i want to code out a easiest way cause i really don't understand the top part.
    That is a sorting algorithm. To see how it works, write it out in terms of "pseudocode" and then try it out with pencil and paper

    Code:
    for i = 0...length-1
       for j = 0...length-1-i
          if data[i] > data[j+1]:
             swap data[i] and data[j+1]
          end
       end
    end
    On paper you can draw i and j as two different arrows that point to one of the array items, each one marked with a number 0, 1, 2, ...., length-1

  15. #15
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    NYP huh~ same here =D same project need to hand up this week

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Runtime Hangup with Merge Sort of 2D array
    By orlileithian in forum C Programming
    Replies: 12
    Last Post: 10-28-2012, 12:25 PM
  2. Merge Sort (Array)
    By Jack Hammer in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2011, 01:37 PM
  3. Quick Sort or Merge Sort???
    By swanley007 in forum C++ Programming
    Replies: 6
    Last Post: 11-10-2005, 05:48 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

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