Saturday, May 21, 2011

Evaluating Test Cases for the Sort Function 2

I had to spend twenty minutes stumbling around because I forgot how dot sourcing works. Sigh.

Well, I don't try to keep everything about PowerShell in my head. I look things up and do searches and stuff.

Let's see what I did.

My basic bubble search was very brute force. Since a bubble sort starts by moving the greatest value to the very last position, you don't need to keep checking that position. Bubble sorts usually skip these compares, doing something like the following:

$bubble02 = {

[LT]#

.SYNOPSIS

Does a bubble sort on a provided array.


.DESCRIPTION

Basic bubble sort, runs through the array, skipping the number that was just moved into last position

in previous sweep.

.PARAMETER $sourceArray

The array to be sorted.

#[GT]

param ($sourceArray) # Takes any array - not tested for mixed arrays (strings and ints, for instance

$global:countCompares = 0 # values for testing compares and swaps.

$global:countSwaps = 0

for ($j = 0; $j -lt $sourceArray.length; $j++)

{

for ($i = 0; $i -lt ($sourceArray.length - 1 - $j); $i++) # checks each position against the position to its right

{

$global:countCompares++ # increment compares variable each time two values are compared

if ($sourceArray[$i] -gt $sourceArray[$i+1]) # uses Greater Than.

{

$global:countSwaps++ # increment swaps variable each time two values are swapped.

$temp = $sourceArray[$i] #swap if necessary.

$sourceArray[$i] = $sourceArray[$i+1]

$sourceArray[$i+1] = $temp

}

}

}

return $sourceArray # return array

}

Here's my revised testing:


function setTestCases {

$global:testCase = 1..5

$global:testCase[0] = 1,2,3,4,5

$global:testCase[1] = 1,1,1,1,1

$global:testCase[2] = 1,2,3,4,3

$global:testCase[3] = 3,2,3,4,5

$global:testCase[4] = 3,2,3,4,3

}

setTestCases

"BubbleSort01:"


foreach ($a in $global:testCase)

{

if (!(&$isSorted (&$bubble01 $a))) {"FAIL: List is sorted: $a"}

"Number of compares for $a is $global:countCompares."

"Number of swaps for $a is $global:countSwaps."

}


setTestCases

"BubbleSort02:"

foreach ($a in $global:testCase)

{

if (!(&$isSorted (&$bubble02 $a))) {"FAIL: List is sorted: $a"}

"Number of compares for $a is $global:countCompares."

"Number of swaps for $a is $global:countSwaps."

}


And here's the result:


PowerShell: .\tests

BubbleSort01:

Number of compares for 1 2 3 4 5 is 20.

Number of swaps for 1 2 3 4 5 is 0.

Number of compares for 1 1 1 1 1 is 20.

Number of swaps for 1 1 1 1 1 is 0.

Number of compares for 1 2 3 3 4 is 20.

Number of swaps for 1 2 3 3 4 is 1.

Number of compares for 2 3 3 4 5 is 20.

Number of swaps for 2 3 3 4 5 is 1.

Number of compares for 2 3 3 3 4 is 20.

Number of swaps for 2 3 3 3 4 is 2.

BubbleSort02:

Number of compares for 1 2 3 4 5 is 10.

Number of swaps for 1 2 3 4 5 is 0.

Number of compares for 1 1 1 1 1 is 10.

Number of swaps for 1 1 1 1 1 is 0.

Number of compares for 1 2 3 3 4 is 10.

Number of swaps for 1 2 3 3 4 is 1.

Number of compares for 2 3 3 4 5 is 10.

Number of swaps for 2 3 3 4 5 is 1.

Number of compares for 2 3 3 3 4 is 10.

Number of swaps for 2 3 3 3 4 is 2.


I added a function to reset the test values, but I don't like that. The test does not display the results for the different functions side-by-side.

I tried to re-write it – but I don't know how to get a deep copy of an array.

I'll work on this more next time.

No comments:

Post a Comment