Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 23rd, 2008, 06:14 PM
kolorowy kolorowy is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 27 kolorowy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 37 m 12 sec
Reputation Power: 0
Sorting

Hello there

I need to implement Java program for sorting.

My program should read the input from text files named "inorder.txt"
"reverse.txt"
"random.txt"

It should display to the screen the numbers in the list in sorted order as well as the number of comparisons for each sorting algorithm.

I have to use insertionSort, MergeSort and QuickSort.

So far this is what I came out with. Insertion Sort works fine, it sorts as well it displays number of comparisons.

But when I try to implement the MergeSort or QuickSort similarly, it does not work.

Also in quicksort I have exception thrown

Quote:
Merge Sort sorted reverse
79 79 78 77 74 74 73 72 70 70 69 68 65java.lang.ArrayIndexOutOfBoundsException: 80
at QuickSort.quicksort(QuickSort.java:9)
at SortingDriver.main(SortingDriver.java:95)


Main driver class:

Code:
import java.io.BufferedReader;
import java.io.FileReader;


public class SortingDriver {
		
		public static void main(String[] args) {
			InsertionSort test = new InsertionSort();
			try {
				
				
				System.out.println("Insertion Sort sorted reverse");
				
				
				//BufferedReader for file reverse.txt
				BufferedReader bf = new BufferedReader(new FileReader("reverse.txt")); 
				String number = bf.readLine();
	      
				String s1[] = number.split("\\s");
	      
				int[] array1=new int[s1.length];
	          
				for(int i=0;i<s1.length;i++)
					array1[i]=Integer.parseInt(s1[i]);
	            
				test.insertionSort(array1, array1.length);      
	            test.print(array1);
	            	
			} catch(Exception e) {   
				e.printStackTrace();
	       	}
	            	
	       	try {
	    	      
	    	     System.out.println("Insertion Sort sorted");
	    	
	    	     //BufferedReader for inorder.txt
	    	     BufferedReader bf = new BufferedReader(new FileReader("inorder.txt"));
	    	     String number = bf.readLine();
	    	     
	    	     String s1[] = number.split("\\s");
	    	      
	    	     int[] array1 = new int[s1.length];
	    	      
	    	     for(int i = 0; i < s1.length; i++)
	    	    	 array1[i] = Integer.parseInt(s1[i]);
	    	      
	    	     test.insertionSort(array1, array1.length);
	    	     test.print(array1);
	    	      
	       	} catch(Exception e) {
	    	   e.printStackTrace();
	       	}

	       	try {
	 	      
	       		System.out.println("Insertion Sort random");
	 	     
	       		//BufferedReader for random.txt
	       		BufferedReader bf = new BufferedReader(new FileReader("random.txt"));
	       		String number = bf.readLine();
	 	      
	       		String s1[] = number.split("\\s");
	 	      
	       		int[] array1 = new int[s1.length];
	 	      
	       		for(int i = 0; i < s1.length; i++)
	       			array1[i] = Integer.parseInt(s1[i]);
	 	      
	       		test.insertionSort(array1, array1.length);
	       		test.print(array1);
	 	      
	       	} catch(Exception e) {
	       		e.printStackTrace();
	       	}
	       	
			QuickSort test1 = new QuickSort();
			try {
				
				
				System.out.println("Quick Sort sorted reverse");
			
				
				//BufferedReader for file reverse.txt
				BufferedReader bf = new BufferedReader(new FileReader("reverse.txt")); 
				String number = bf.readLine();
	      
				String s1[] = number.split("\\s");
	      
				int[] array1=new int[s1.length];
	          
				for(int i=0;i<s1.length;i++)
					array1[i]=Integer.parseInt(s1[i]);
	            
				test1.quicksort(array1, array1.length, array1.length);     
	            test1.print(array1);
	            	
			} catch(Exception e) {   
				e.printStackTrace();
	       	}
			Mergesort test2 = new Mergesort();
			try {
				
				
				System.out.println("Merge Sort sorted reverse");
				
				
				//BufferedReader for file reverse.txt
				BufferedReader bf = new BufferedReader(new FileReader("reverse.txt")); 
				String number = bf.readLine();
	      
				String s1[] = number.split("\\s");
	      
				int[] array1=new int[s1.length];
	          
				for(int i=0;i<s1.length;i++)
					array1[i]=Integer.parseInt(s1[i]);
	            
				test2.mergesort(array1, array1.length, array1.length);     
	            test2.print(array1);
	            	
			} catch(Exception e) {   
				e.printStackTrace();
	       	}
			
	}
}




Insertion Sort class:

Code:

//insertion sort
public class InsertionSort {

	public void insertionSort(int numbers[], int array_size)
	{
	  int i, j, index;
	  int counter = 0;     //counter for counting # of comparisons initialized to 0
	  
	  for (i = 1; i < array_size; i++)
	  {
	    index = numbers[i];
	    j = i;
	   
	    while ((j > 0) && (numbers[j-1] > index))
	    {
	      numbers[j] = numbers[j-1];
	      j = j - 1;
	      counter++;		//counter for counting # of comparisons incremented by 1 for each comparison
	    }
	    numbers[j] = index;
	  }
	  System.out.println("Number of comparisons: " + counter);        
	  System.out.println("Number of elements: " + i);
	}
	
	public void print(int[] anArray) {
		
		for (int i : anArray) 
			System.out.print(" " + i);
			
			
		System.out.println();
		System.out.println();
		
			
	}
}


Merge sort class:


Code:
public class Mergesort
{

	public void mergesort(int array[], int i, int j)
	{
	    if(i == j)
	           return;
	      int length = i- j+1;
	      int pivot = (i+j) / 2;
	      mergesort(array, i, pivot);
	      mergesort(array, pivot+1, j);
	      int sorting[] = new int[length];
	      for(i = 0; i < length; i++)
	          sorting[i] = array[i+i];
	      int mergeA = 0;
	      int mergeB = pivot-i+1;
	      for( i = 0; i < length; i++)
	      {
	        if(mergeB <= j-i) {
	        	
	        
	            if(mergeA <= pivot-i) {
	            	
	            
	                if(sorting[mergeA] > sorting[mergeB]) {
	                	
	              
	                    array[i+i] = sorting[mergeB++];
	                } else {
	                    array[i+i] = sorting[mergeA++];
	                }
	            }	 else {
	                	array[i+i] = sorting[mergeB++];
	            }
	        }	 else {
		           array[i+i] = sorting[mergeA++];
	        }



	        System.out.println(i);
	      }
	     
	    }
	public void print(int[] anArray) {
		
		for (int i : anArray) 
			System.out.print(" " + i);
			
			
		System.out.println();
		System.out.println();
		
			
	}
	
}


Quick sort class:


Code:
public class QuickSort {
	
	void quicksort (int[] a, int lo, int hi)
	{
	//  lo is the lower index, hi is the upper index
	//  of the region of array a that is to be sorted
	    int i=lo, j=hi, h;
	    int x=a[(lo+hi)/2];

	    //  partition
	    do
	    {    
	        while (a[i]<x) i++; 
	        while (a[j]>x) j--;
	        if (i<=j)
	        {
	            h=a[i]; a[i]=a[j]; a[j]=h;
	            i++; j--;
	        }
	    } while (i<=j);

	    //  recursion
	    if (lo<j) quicksort(a, lo, j);
	    if (i<hi) quicksort(a, i, hi);
	}
	
	public void print(int[] anArray) {
		
		for (int i : anArray) 
			System.out.print(" " + i);
			
			
		System.out.println();
		System.out.println();
		
			
	}

}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Sorting


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway