Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Help with merge sort

807591Apr 17 2008 — edited Apr 20 2008
I have everything working except the last method were I am supposed to combine the two sorted halfs of the first and second half of the array into a new sorted array.
Thanks in advance.

Item Class
public class Item{
  private int myId;
  private int myInv;
  
  public Item(int id, int inv){
    myId = id;
    myInv = inv;
  }
   
  public int getId()
  	{ 
  		return myId;
  	}
   
  public int getInv()
  	{
  		return myInv;
  	}

  public int compareTo(Item other)
  	{
  		 return(int)(myId) - (int) ((((Item)other).myId));
  	}

  public boolean equals(Item other)
  	{
  		 if (myId == other.myId)
  		 {
  		 	return true;
  		 }
  		 else
  		 {
  		 	return false;
  		 }
  	}

  public String toString()
  	{
  	return	(myId + "    " +  myInv);
  	}
}
Store Class
import java.util.*;
import java.io.*;
public class Store{
  private ArrayList <Item> myStore = new ArrayList <Item>();
  private ArrayList<Item> a = new ArrayList<Item>();
  private ArrayList<Item> list = new ArrayList<Item>();
  int first,mid, last;
 
  public Store(String fName){ }
  public Store(){}
  public void loadFile()
      {  
           mid = myStore.size()/2;
         first = 0;
         last = myStore.size()-1;
          int Id;
          int Inv;
      try
      {    
          Scanner in = new Scanner(new File ("file50.txt"));
          while(in.hasNext())
          {
              Id = in.nextInt();
              Inv = in.nextInt();
              myStore.add(new Item(Id, Inv));
          }
      }
      catch(IOException e)
      {
          System.out.println(e.getMessage());
      }    
          
          
      }
  public void displayStore()
      {
          for(int i = 0; i<myStore.size(); i ++)
          {
              System.out.printf("%-10s", i +1);
              System.out.printf("%-5s", myStore.get(i));
              System.out.println();
              if ( i%10== 0 && i != 1 && i != 0)
              {
                  System.out.println();
              }
          }
      }
  
 
       public void Sort1()
       {
            mid = myStore.size()/2;
        first = 0;
   last = myStore.size()-1;
    for (int outer = 0; outer <mid; outer++){
     for (int inner = 0; inner < mid-outer-1; inner++){
       if (myStore.get(inner).compareTo(myStore.get(inner + 1)) > 0){
         Item temp = myStore.get(inner);
         myStore.set(inner,myStore.get(inner + 1));
         myStore.set(inner + 1,temp);
       }
     }//inner loop
   
     }//outer loop
    }
    public void Sort2()
 {
    mid = myStore.size()/2;
        first = 0;
   last = myStore.size()-1;
   for (int outer = mid; outer <=last; outer++)
    {
     for (int inner = mid; inner <last; inner++)
      {
       if (myStore.get(inner).compareTo(myStore.get(inner + 1)) > 0)
        {
         Item temp = myStore.get(inner);
         myStore.set(inner,myStore.get(inner + 1));
         myStore.set(inner + 1,temp);
          }
         
      }//inner loop
  }//outer loop
 
 }
  
 public void merge()
 {
 int i = 0;
 int j = mid;
  int h = 0;
 while (i < mid && j<=last)
  {
   if(myStore.get(i).compareTo(myStore.get(j))<0)
   {
    list.set(h,myStore.get(i));
    i++;
    h++; 
   }
   else if ((myStore.get(i).compareTo(myStore.get(j))>0))
   {
    list.set(h,myStore.get(j));
    j++;
    h++;
   }
  /* else if ((myStore.get(i).compareTo(myStore.get(j))==0))
   {
    list.set(h,myStore.get(i));
    i++;
    h++;
    list.set(h,myStore.get(j));
    j++;
    h++;
   */
   }
 }
 
 }
Driver
public class Lab19_1
{

    public Lab19_1()
    {
    }
    
    public static void main (String[] args)
    {
    	Store bob = new Store();
    	bob.loadFile();
    	bob.displayStore();
    	bob.Sort1();
    	bob.Sort2();
    	bob.displayStore();
    	bob.merge();
    	bob.displayStore();
    }
}
Output
1         3679    87
2         196    60
3         17914    12
4         18618    64
5         2370    65
6         584    85
7         18524    34
8         12024    5
9         6992    76
10        18410    56
11        9267    68

12        18995    56
13        6265    58
14        6835    94
15        14151    82
16        11485    39
17        15559    33
18        18465    27
19        19967    45
20        13520    38
21        5529    11

22        3433    5
23        17911    96
24        18181    60
25        11549    88
26        14896    81
27        184    14
28        4329    64
29        18871    69
30        15141    87
31        11584    32

32        14088    92
33        18061    3
34        206    31
35        13066    8
36        19623    88
37        12705    14
38        9351    8
39        17753    70
40        15917    51
41        768    85

42        15814    60
43        15320    82
44        8303    90
45        7282    73
46        14092    48
47        10629    19
48        12917    63
49        15006    53
50        12328    63
1         196    60
2         584    85
3         2370    65
4         3433    5
5         3679    87
6         5529    11
7         6265    58
8         6835    94
9         6992    76
10        9267    68
11        11485    39

12        11549    88
13        12024    5
14        13520    38
15        14151    82
16        15559    33
17        17911    96
18        17914    12
19        18181    60
20        18410    56
21        18465    27

22        18524    34
23        18618    64
24        18995    56
25        19967    45
26        184    14
27        206    31
28        768    85
29        4329    64
30        7282    73
31        8303    90

32        9351    8
33        10629    19
34        11584    32
35        12328    63
36        12705    14
37        12917    63
38        13066    8
39        14088    92
40        14092    48
41        14896    81

42        15006    53
43        15141    87
44        15320    82
45        15814    60
46        15917    51
47        17753    70
48        18061    3
49        18871    69
50        19623    88
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.RangeCheck(ArrayList.java:547)
    at java.util.ArrayList.set(ArrayList.java:337)
    at Store.merge(Store.java:103)
    at Lab19_1.main(Lab19_1.java:25)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 18 2008
Added on Apr 17 2008
7 comments
183 views