Results 1 to 2 of 2

Thread: I am bugged !!!

  1. #1
    ajaygargnsit is offline Member
    Join Date
    Dec 2007
    Posts
    9
    Rep Power
    0

    Default I am bugged !!!

    Kindly explain why the following mergesort code does not give the desired result? Specifically, the array 'after-it-is-supposed-to-be-sorted' is exactly the same as the original array. Plzzzzzzzzzz help !!!


    package src;
    import java.util.*;

    class Sorti
    {
    public int a[] = {287,30,58,122,390,4,21,65,2987,459};
    public int f = 0;
    public int l = 9;

    public void display()
    {
    for(int m = 0; m < a.length; m++)
    {
    System.out.println(a[m]);
    }
    }
    }


    class Mergesort extends Sorti
    {
    public int mid = 0;
    public int i, j, k;
    public int[] temp1, temp2;

    public void mergesort(int[] arr, int start, int end)
    {
    mid = (start + end) / 2;
    if(start < end)
    {
    mergesort(arr,start,mid);
    mergesort(arr,mid+1,end);
    merge(arr,start,mid,end);
    }

    /*if(end > 0)
    {
    System.out.println(end);
    mergesort(arr,start,end-1);

    }*/
    }

    public void merge(int[] arr, int start, int mid, int end)
    {
    int tema[] = new int[mid - start + 2];
    int temb[] = new int[end - mid + 1];

    System.out.println(" First : " + (mid-start+2));
    System.out.println("Second : " + (end-mid+1));

    for(i = 0; i < (mid - start + 1); i++)
    {
    tema[i] = arr[start + i];
    }
    tema[i] = 10000;


    for(i = 0; i < (end - mid); i++)
    {
    temb[i] = arr[mid + 1 + i];
    }
    temb[i] = 10000;

    i = 0;
    j = 0;

    for(k = 0; k < (end - start + 1); k++)
    {
    if(tema[i] < temb[j])
    {
    arr[k + start] = tema[i];
    i++;
    }
    else
    {
    System.out.println("arrrrrrrr");
    arr[k + start] = temb[j];
    j++;
    }
    }
    }
    }



    public class sort
    {
    public static void main(String[] args)
    {
    Mergesort obj = new Mergesort();

    obj.display();

    obj.mergesort(obj.a, obj.f, obj.l);

    obj.display();
    }
    }

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Is there a reason why you're using the original array as you are and not a random array? Just curious.

    I wouldn't have written it as you did.. trying to decipher your code is a little time consuming when you're not used to someone's style of code.. but when I have more time I'd like to see if I can be of further assistance. In the meantime, your code is very similar to that found here, maybe you can figure it out before I write again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •