Skip to main content
added 146 characters in body
Source Link
Sjoerd C. de Vries
  • 66.2k
  • 15
  • 189
  • 327

I note that all answers so far try to solve the problem of assigning a potential Null value by manipulating the return value. I feel it would be more appropriate to make the whole assignment conditional. Like this:

If[condition, aa = value]

There's also a small bug in your program (count isn't initialized), and, of course, it doesn't sort at the moment. I assume that you aware of that and that the Return value is used for testing.

The code would then be:

QuickSort[x_List] := 
 Module[{pivot, aa = 0, bb = 0, count = 0}, 
  If[Length@x <= 1, Return[x]];
  pivot = First[x];
  If[Length[Cases[x, j_ /; j < pivot]] > 1,
     aa = Length[Cases[x, j_ /; j < pivot]] - 1
  ];
  If[Length[Cases[x, j_ /; j < pivot]] > 1, 
     bb = Length[Cases[x, j_ /; j > pivot]] - 1
  ];
  count = count + aa + bb;
  Flatten@{QuickSort[Cases[x, j_ /; j < pivot]], 
    Cases[x, j_ /; j == pivot], QuickSort[Cases[x, j_ /; j > pivot]]};
   Return[count]] 

Remove ;Return[count] to let it sort again.

I'm not sure about the j < pivot test in the second If. It depends on your intention with bb, but I guess the test should be j > pivot.

I note that all answers so far try to solve the problem of assigning a potential Null value by manipulating the return value. I feel it would be more appropriate to make the whole assignment conditional. Like this:

If[condition, aa = value]

There's also a small bug in your program (count isn't initialized), and, of course, it doesn't sort at the moment. I assume that you aware of that and that the Return value is used for testing.

The code would then be:

QuickSort[x_List] := 
 Module[{pivot, aa = 0, bb = 0, count = 0}, 
  If[Length@x <= 1, Return[x]];
  pivot = First[x];
  If[Length[Cases[x, j_ /; j < pivot]] > 1,
     aa = Length[Cases[x, j_ /; j < pivot]] - 1
  ];
  If[Length[Cases[x, j_ /; j < pivot]] > 1, 
     bb = Length[Cases[x, j_ /; j > pivot]] - 1
  ];
  count = count + aa + bb;
  Flatten@{QuickSort[Cases[x, j_ /; j < pivot]], 
    Cases[x, j_ /; j == pivot], QuickSort[Cases[x, j_ /; j > pivot]]};
   Return[count]] 

Remove ;Return[count] to let it sort again.

I note that all answers so far try to solve the problem of assigning a potential Null value by manipulating the return value. I feel it would be more appropriate to make the whole assignment conditional. Like this:

If[condition, aa = value]

There's also a small bug in your program (count isn't initialized), and, of course, it doesn't sort at the moment. I assume that you aware of that and that the Return value is used for testing.

The code would then be:

QuickSort[x_List] := 
 Module[{pivot, aa = 0, bb = 0, count = 0}, 
  If[Length@x <= 1, Return[x]];
  pivot = First[x];
  If[Length[Cases[x, j_ /; j < pivot]] > 1,
     aa = Length[Cases[x, j_ /; j < pivot]] - 1
  ];
  If[Length[Cases[x, j_ /; j < pivot]] > 1, 
     bb = Length[Cases[x, j_ /; j > pivot]] - 1
  ];
  count = count + aa + bb;
  Flatten@{QuickSort[Cases[x, j_ /; j < pivot]], 
    Cases[x, j_ /; j == pivot], QuickSort[Cases[x, j_ /; j > pivot]]};
   Return[count]] 

Remove ;Return[count] to let it sort again.

I'm not sure about the j < pivot test in the second If. It depends on your intention with bb, but I guess the test should be j > pivot.

Source Link
Sjoerd C. de Vries
  • 66.2k
  • 15
  • 189
  • 327

I note that all answers so far try to solve the problem of assigning a potential Null value by manipulating the return value. I feel it would be more appropriate to make the whole assignment conditional. Like this:

If[condition, aa = value]

There's also a small bug in your program (count isn't initialized), and, of course, it doesn't sort at the moment. I assume that you aware of that and that the Return value is used for testing.

The code would then be:

QuickSort[x_List] := 
 Module[{pivot, aa = 0, bb = 0, count = 0}, 
  If[Length@x <= 1, Return[x]];
  pivot = First[x];
  If[Length[Cases[x, j_ /; j < pivot]] > 1,
     aa = Length[Cases[x, j_ /; j < pivot]] - 1
  ];
  If[Length[Cases[x, j_ /; j < pivot]] > 1, 
     bb = Length[Cases[x, j_ /; j > pivot]] - 1
  ];
  count = count + aa + bb;
  Flatten@{QuickSort[Cases[x, j_ /; j < pivot]], 
    Cases[x, j_ /; j == pivot], QuickSort[Cases[x, j_ /; j > pivot]]};
   Return[count]] 

Remove ;Return[count] to let it sort again.