5 Replies - 541 Views - Last Post: 05 February 2012 - 04:29 AM Rate Topic: -----

#1 usmansajid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-February 12

Sorting array with multiple algorithms - Problem

Posted 04 February 2012 - 10:32 AM

Hi there,

I'm trying to sort a random array with multiple algorithms and print running times. Each algorithm is supposed to sort the random array. Lets just say I have 3 algorithms; bubble sort, insertion sort and selection sort.

The problem is:
The first algorithm (e.g. bubble sort) in my code sorts the random array and saves it. The other algorithms cant sort it because its already sorted. They just run through sorted arrays print running times. This is not what I want.

What I want is for each algorithm to sort the array. Not only one.


Here is where the problem in my code lies looks like;

	// I dont know if i should upload all my code but here is the outline. 
	for (int i=0; i<noOfTimesToRun; i++) {
		
		// This is supposed to be the random array.
		int[] array; 
		
		// At this point array is STILL RANDOM.
		// Begin algorithm 1. 
		Start timer; 
		BubbleSort(array);
		End timer; 
		Elapsed time = End timer - start timer; 
		
		//Algorithm 2. 
		// By now the array is sorted. Its not random. I do not want that. 
		// So the second algorithm and so forth, runs on sorted arrays.
		// === I want to use the SAME RANDOM ARRAY, SO I CAN COMPARE TIMES.
		Start timer; 
		InsertionSort(array);
		End timer; 
		Elapsed time = End timer - start timer; 	
	}
	
	//Print out times here
	System.out.println("Bubble Sort Time: " + timeForBubbleSort);




Is This A Good Question/Topic? 0
  • +

Replies To: Sorting array with multiple algorithms - Problem

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Sorting array with multiple algorithms - Problem

Posted 04 February 2012 - 10:37 AM

You can copy your random test array into another temporary array that is then sent to your sort methods. Check out the Java Array.copyOf() method.
Was This Post Helpful? 0
  • +
  • -

#3 usmansajid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-February 12

Re: Sorting array with multiple algorithms - Problem

Posted 04 February 2012 - 10:41 AM

But say if I am running it 40 times with 3 or more algorithms. Would it become inefficient as I may have a large group of arrays?


Anyways, where would this copyOf method go? In the loop or outside of loop?
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Sorting array with multiple algorithms - Problem

Posted 04 February 2012 - 10:51 AM

Inefficient? No. You're using your various sort algorithms to sort exactly the same array. 2 arrays are involved, your Golden Array that doesn't change, and a copy of it that is sent to the sort routines.

The general structure would be:

public class OutOfSorts
{
  // some method
  {
   // define the Golden test array
   int[] goldenArray = new int[howBig];

   // fill the Golden array with random values
   fillRandomly( goldenArray );

   // copy the Golden array to a temp array for sorting
   int[] tempArray = Arrays.copyOf( goldenArray, 0, goldenArray.length - 1 );

   // run your first sort algorithm
   sortArray1( tempArray );

   // refresh the tempArray
   tempArray = Arrays.copyOf( goldenArray, 0, goldenArray.length - 1 );

   // run your next sort algorith
   sortArray2( tempArray );

   // continue the previous 2 steps until done.

  } // end some method

} // end class

This post has been edited by GregBrannon: 04 February 2012 - 10:54 AM

Was This Post Helpful? 1
  • +
  • -

#5 usmansajid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-February 12

Re: Sorting array with multiple algorithms - Problem

Posted 05 February 2012 - 04:00 AM

hi GregBrannon,

Thanks a lot. This has solved my problem. If anyone else wants to use this, the only thing I have to point out is that; Arrays copy of takes two parameters not three.

 Arrays.copyOf(int array, int array.length);



But thanks to GregBrannon for suggesting the copyOf idea.
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Sorting array with multiple algorithms - Problem

Posted 05 February 2012 - 04:29 AM

Glad it worked. Of course you're right about the parameters. I went cross-eyed and grabbed the parameters from copyOfRange().

Keep coding.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1