Skip to main content
added 37 characters in body
Source Link
user73941
user73941

You are using an iterative approach which is normally faster than a recursive. That's a good optimization.


            if (array[i].CompareTo(pivotValue) > 0)
            {
                continue;
            }
            Swap(ref array[i], ref array[startIndex]);
            startIndex++;

Why not just:

      if (array[i].CompareTo(pivotValue) < 0)
      {
        Swap(ref array[i], ref array[startIndex]);
        startIndex++;
      }

      pivotIndex = r.Next(startIndex, endIndex);

The wiki is doing so but I don't see why instead of just taking the middle:

pivotIndex = startIndex + (endIndex - startIndex) / 2;

You could measure on a larger data set to see if there is any average difference in performance?


$"The res[i] {res[i]} was not greater than six"

Strictly speaking: you are not finding values lesser than 6 but the six smallest values in the data set. (n or k is an index not a value)

You are using an iterative approach which is normally faster than a recursive. That's a good optimization.


            if (array[i].CompareTo(pivotValue) > 0)
            {
                continue;
            }
            Swap(ref array[i], ref array[startIndex]);
            startIndex++;

Why not just:

      if (array[i].CompareTo(pivotValue) < 0)
      {
        Swap(ref array[i], ref array[startIndex]);
        startIndex++;
      }

      pivotIndex = r.Next(startIndex, endIndex);

The wiki is doing so but I don't see why instead of just taking the middle:

pivotIndex = startIndex + (endIndex - startIndex) / 2;

You could measure on a larger data set to see if there is any average difference in performance?


$"The res[i] {res[i]} was not greater than six"

Strictly speaking: you are not finding values lesser than 6 but the six smallest values in the data set.

You are using an iterative approach which is normally faster than a recursive. That's a good optimization.


            if (array[i].CompareTo(pivotValue) > 0)
            {
                continue;
            }
            Swap(ref array[i], ref array[startIndex]);
            startIndex++;

Why not just:

      if (array[i].CompareTo(pivotValue) < 0)
      {
        Swap(ref array[i], ref array[startIndex]);
        startIndex++;
      }

      pivotIndex = r.Next(startIndex, endIndex);

The wiki is doing so but I don't see why instead of just taking the middle:

pivotIndex = startIndex + (endIndex - startIndex) / 2;

You could measure on a larger data set to see if there is any average difference in performance?


$"The res[i] {res[i]} was not greater than six"

Strictly speaking: you are not finding values lesser than 6 but the six smallest values in the data set. (n or k is an index not a value)

Source Link
user73941
user73941

You are using an iterative approach which is normally faster than a recursive. That's a good optimization.


            if (array[i].CompareTo(pivotValue) > 0)
            {
                continue;
            }
            Swap(ref array[i], ref array[startIndex]);
            startIndex++;

Why not just:

      if (array[i].CompareTo(pivotValue) < 0)
      {
        Swap(ref array[i], ref array[startIndex]);
        startIndex++;
      }

      pivotIndex = r.Next(startIndex, endIndex);

The wiki is doing so but I don't see why instead of just taking the middle:

pivotIndex = startIndex + (endIndex - startIndex) / 2;

You could measure on a larger data set to see if there is any average difference in performance?


$"The res[i] {res[i]} was not greater than six"

Strictly speaking: you are not finding values lesser than 6 but the six smallest values in the data set.