Arranging numbers in an Array

Discussion in 'C' started by kens, Jul 18, 2007.

  1. kens

    kens New Member

    Joined:
    Jul 17, 2007
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Write an algorithm to arrange an array such that the largest element comes first , second largest is at the end , third largest is the second from beginning and so on.

    I thought that the array could be sorted first. Then, we could use array=array[2*i] for the first half of the elements, storing the values at odd positions (assuming array starts at array[0]) in a second array. Then, the stored values can be written back at the end of the first array in the reverse order.

    But, here sorting is required initially and requires extra space for the second array. Could you suggest some method that is more efficient?
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    You can only swap two elements at a time, so just use one temp variable repeatedly to make the swap.
     
  3. kens

    kens New Member

    Joined:
    Jul 17, 2007
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice