HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Find K biggest numbers in the array

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thearraynumbersbiggestfind

Problem

I was trying to implement method number 2, from this article.


Method 2 (Use temporary array) K largest elements from arr[0..n-1]



  • Store the first k elements in a temporary array temp[0..k-1].



  • Find the smallest element in temp[], let the smallest element be min.



  • For each element x in arr[k] to arr[n-1] If x is greater than the


min then remove min from temp[] and insert x.

  • Print final k elements of temp[]





Time Complexity: O((n-k)*k). If we want the output sorted then
O((n-k)*k + klogk)

Please comment on the complexity and runtime speed. Can I improve my implementation in that sense? I was trying to get O((n-k)*k).

```
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace JobInterviewTests
{

//Design an algorithm to find K biggest numbers in the array.
[TestClass]
public class KBiggestNumbers
{
[TestMethod]
public void FindKBiggestNumbersTest()
{
int[] testArray = new[] { 7, 2, 4, 4, 3, 6, 1, 8, 9, 10, 11 };
int k = 5;
int[] result = FindKBiggestNumbers(testArray, k);
int[] expectedResult = new[] { 7,8,9,10,11 };
CollectionAssert.AreEqual(expectedResult, result);
}

private int[] FindKBiggestNumbers(int[] testArray, int k)
{
int[] result = new int[k];
for (int i = 0; i result[k - 1])
{
for (int l = 0; l result[j] && testArray[i] place 33
while (indexRight- indexLeft >1)
{
currIndex = (indexRight + indexLeft)/2;
if (testArray[i] >= result[currIndex])
{
indexLeft = currIndex;
}
else
{
indexRight = currIndex;

Solution

Store the first k elements in a temporary array temp[0..k-1]

You are not doing that


Find the smallest element in temp[], let the smallest element be min

You are not doing that


For each element x in arr[k] to arr[n-1]...

You are starting on 0 because you skipped step 1

Nothing in there about a binary search

Not less operations the with the shifts

This

if (testArray[i] <= result[0])
{
    continue;
}
else
{


Can be

if (testArray[i] > result[0])
{


Other than that looks good

Just following what I think are the instructions

It is \$\mathcal{O}(n \times k)\$ as the first k elements are not free

private int[] FindKBiggestNumbersM(int[] testArray, int k)
{
    int[] result = new int[k];
    int indexMin = 0;
    result[indexMin] = testArray[0];
    int min = result[indexMin];

    for (int i = 1; i  min)
        {
            min = testArray[i];
            result[indexMin] = min;
            for (int r = 0; r < k; r++)
            {
                if (result[r] < min)
                {
                    min = result[r];
                    indexMin = r;
                }
            }
        }
    }
    return result;
}

Code Snippets

if (testArray[i] <= result[0])
{
    continue;
}
else
{
if (testArray[i] > result[0])
{
private int[] FindKBiggestNumbersM(int[] testArray, int k)
{
    int[] result = new int[k];
    int indexMin = 0;
    result[indexMin] = testArray[0];
    int min = result[indexMin];

    for (int i = 1; i < testArray.Length; i++)
    {
        if(i < k)
        {
            result[i] = testArray[i];
            if (result[i] < min)
            {
                min = result[i];
                indexMin = i;
            }
        }
        else if (testArray[i] > min)
        {
            min = testArray[i];
            result[indexMin] = min;
            for (int r = 0; r < k; r++)
            {
                if (result[r] < min)
                {
                    min = result[r];
                    indexMin = r;
                }
            }
        }
    }
    return result;
}

Context

StackExchange Code Review Q#153254, answer score: 4

Revisions (0)

No revisions yet.