Finding Second Smallest Element in Array

bclayman

I'm trying to find the second smallest element in an array of n elements using only n + ceil(lg n) - 2 comparisons. The hint in CLRS says to find the smallest element.

This takes n - 1 comparisons so I'm left with ceil(lg n) - 1 comparisons to find the second smallest, once I know the largest.

Any ideas?

Thanks,

bclayman

biziclop

Let's say we've got a list a1...an with n being a power of 2.

First pair the elements up, let's say a1 with a2, a3 with a4 and so on, and compare them with each other. This gives you n/2 comparisons.

Advance all the winners to the next round, which only has n/2 elements now, and repeat the same process. This requires n/4 more comparisons.

Repeat the above until you've only got 1 element left, the ultimate winner. To get there you had to do n/2 + n/4 + ... + 1 = n-1 comparisons.

That's great but which one could be the second smallest? Well, it has to be one of the elements your winner had beaten along the way to the top. There are lg n such losers, so you need to compare them amongst each other to find the smallest (requiring a further lg n - 1 comparisons).

And the smallest of the losers is the second smallest overall.


It's easy to prove why the above method always finds the second smallest: since it's smaller than every element but the ultimate winner, it would win every round apart from the one against the champion.

If n isn't a power of 2, the process is almost exactly the same, except the list of losers will be as long as it would be for the next exact power of 2 which is why you end up with ceil(lg n).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Finding the second smallest integer in array

From Dev

Finding the smallest and second smallest value in an array Java

From Dev

Finding the smallest element in multidimensional array

From Dev

finding smallest and second smallest value of an array in C++

From Dev

finding smallest and second smallest number

From Dev

C++: Finding second max element in array

From Dev

Finding index of largest negative and smallest positive element in array

From Dev

Second smallest element in a list

From Dev

Haskell - finding smallest Element in list

From Dev

Find smallest & second smallest element in every k elements of an array with size N

From Dev

Finding the second smallest number using loops in python

From Dev

Incorrect output when finding second smallest integer

From Dev

c program for smallest and second smallest in array

From Dev

Recursion for finding the smallest path of numbers in an array

From Dev

Finding smallest neighbour in a 2D array

From Dev

Java - Finding Largest and Smallest Numbers using an Array

From Dev

Finding smallest value from array in java

From Dev

Finding smallest neighbour in a 2D array

From Dev

Java - Finding Largest and Smallest Numbers using an Array

From Dev

How to get the second smallest/largest element in a list

From Dev

java binary search tree finding second smallest node

From Dev

java binary search tree finding second smallest node

From Dev

Finding the middle element of an array

From Dev

finding array element in an object

From Dev

Finding the middle element of an array

From Dev

Finding First and Second in an Array of Values?

From Dev

Finding First and Second in an Array of Values?

From Dev

Second smallest number in array - incorrect output

From Dev

Given an array of integers, find the second largest and second smallest within the array

Related Related

  1. 1

    Finding the second smallest integer in array

  2. 2

    Finding the smallest and second smallest value in an array Java

  3. 3

    Finding the smallest element in multidimensional array

  4. 4

    finding smallest and second smallest value of an array in C++

  5. 5

    finding smallest and second smallest number

  6. 6

    C++: Finding second max element in array

  7. 7

    Finding index of largest negative and smallest positive element in array

  8. 8

    Second smallest element in a list

  9. 9

    Haskell - finding smallest Element in list

  10. 10

    Find smallest & second smallest element in every k elements of an array with size N

  11. 11

    Finding the second smallest number using loops in python

  12. 12

    Incorrect output when finding second smallest integer

  13. 13

    c program for smallest and second smallest in array

  14. 14

    Recursion for finding the smallest path of numbers in an array

  15. 15

    Finding smallest neighbour in a 2D array

  16. 16

    Java - Finding Largest and Smallest Numbers using an Array

  17. 17

    Finding smallest value from array in java

  18. 18

    Finding smallest neighbour in a 2D array

  19. 19

    Java - Finding Largest and Smallest Numbers using an Array

  20. 20

    How to get the second smallest/largest element in a list

  21. 21

    java binary search tree finding second smallest node

  22. 22

    java binary search tree finding second smallest node

  23. 23

    Finding the middle element of an array

  24. 24

    finding array element in an object

  25. 25

    Finding the middle element of an array

  26. 26

    Finding First and Second in an Array of Values?

  27. 27

    Finding First and Second in an Array of Values?

  28. 28

    Second smallest number in array - incorrect output

  29. 29

    Given an array of integers, find the second largest and second smallest within the array

HotTag

Archive