# Sorting array with multiple algorithms - Problem

Page 1 of 1

## 5 Replies - 1167 Views - Last Post: 05 February 2012 - 04:29 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=265387&amp;s=48ebdc16a51dbbe2d5a93276e612355e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 usmansajid

Reputation: 0
• 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

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• 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.

### #3 usmansajid

Reputation: 0
• 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?

### #4 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• 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

### #5 usmansajid

Reputation: 0
• 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.

### #6 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• 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.