I remembered how to do a deep copy of an array in PowerShell:
PowerShell: $a = 4,5,6
PowerShell: $a
4
5
6
PowerShell: $a[0]
4
PowerShell: $b = $a
PowerShell: $a[0] = 5
PowerShell: $b
5 == shallow copy
5
6
PowerShell: $b = &{$a}
PowerShell: $b
5
5
6
PowerShell: $a[0] = 9
PowerShell: $b[0]
5 == deep copy
PowerShell:
So, back to my tests:
$testCase = 1..5
$testCase[0] = 1,2,3,4,5
$testCase[1] = 1,1,1,1,1
$testCase[2] = 1,2,3,4,3
$testCase[3] = 3,2,3,4,5
$testCase[4] = 3,2,3,4,3
foreach ($a in $testCase)
{
"Bubble sort 01:"
" "
$temp = &{$a}
if (!(&$isSorted (&$bubble01 $temp))) {"FAIL: List is sorted: $a"}
# "Number of compares for $a is $global:countCompares."
# "Number of swaps for $a is $global:countSwaps."
"Number of swaps per compares for $a is " + $global:countSwaps / $global:countCompares
" "
"Bubble sort 02:"
" "
$temp = &{$a}
if (!(&$isSorted (&$bubble02 $temp))) {"FAIL: List is sorted: $a"}
# "Number of compares for $a is $global:countCompares."
# "Number of swaps for $a is $global:countSwaps."
"Number of swaps per compares for $a is " + $global:countSwaps / $global:countCompares
}
As you can see, I settled on swaps per compares. I'm assuming that the number of swaps necessary to sort a list is the same, no matter how you do it. I don't know if that's true, but I'm assuming. If that's true, then the larger the number (that is, the fewer number of compares), the more efficient the search is.
Bubble sort 01:
Number of swaps per compares for 1 2 3 4 5 is 0
Bubble sort 02:
Number of swaps per compares for 1 2 3 4 5 is 0
Bubble sort 01:
Number of swaps per compares for 1 1 1 1 1 is 0
Bubble sort 02:
Number of swaps per compares for 1 1 1 1 1 is 0
Bubble sort 01:
Number of swaps per compares for 1 2 3 4 3 is 0.05
Bubble sort 02:
Number of swaps per compares for 1 2 3 4 3 is 0.1
Bubble sort 01:
Number of swaps per compares for 3 2 3 4 5 is 0.05
Bubble sort 02:
Number of swaps per compares for 3 2 3 4 5 is 0.1
Bubble sort 01:
Number of swaps per compares for 3 2 3 4 3 is 0.1
Bubble sort 02:
Number of swaps per compares for 3 2 3 4 3 is 0.2
No comments:
Post a Comment