Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Merge sort help

  1. Sep 11, 2012 #1
    please help me with a c code for the merge sort...... and i am getting a segmentation fault in running this selection sort,,, please help......

    #include <stdio.h>

    int main( )
    {
    int arr[10];
    int i,j,temp;
    for ( i = 0 ; i<10 ; i++ )
    {
    printf("enter the numbers: \n");
    scanf("%d \n", arr);
    }
    for ( i = 0 ; i <8 ; i++ )
    {
    for ( j = i + 1 ; j <9 ; j++ )
    {
    if ( arr > arr[j] )
    {
    temp = arr ;
    arr = arr[j] ;
    arr[j] = temp ;
    }
    }
    }
    for ( i = 0 ; i <10 ; i++ )
    printf("the sorted array is: %d \n", &arr);
    return 0;
    }
     
  2. jcsd
  3. Sep 11, 2012 #2

    uart

    User Avatar
    Science Advisor

    Re: [C] variable system in programming


    The problem is in your scanf statement. Scanf needs a reference and "arr" is already de-referenced. You need to prefix it with "&".
     
  4. Sep 11, 2012 #3

    rcgldr

    User Avatar
    Homework Helper

    You probably wanted those loops to be ... ; i < 9; ... and ... ; j < 10; .... Also this algorithm is not a merge sort, it's a bubble (down) sort.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Merge sort help
  1. Merge sorting (Replies: 2)

  2. Merge sort (Replies: 1)

Loading...