Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6617763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:44:34+00:00 2026-05-25T20:44:34+00:00

I have an array A[] with 4 element A={ 8 1 2 4 }.

  • 0

I have an array A[] with 4 element A={
8 1 2 4 }. How to sort it with minimized cost. Criteria is defined as follows-

a. It is possible to swap any 2 element.

b. The cost of any swap is sum of the element value , Like if i swap 8 and 4 the cost is 12 an resultant array is look like A={4 1 2 8}, which is still unsorted so more swap needed.

c. Need to find a way to sort the array with minimum cost.

From my observation greedy will not work, like in each step place any element to its sorted position in array with minimum cost. So a DP solution needed.
Can any one help??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:44:35+00:00Added an answer on May 25, 2026 at 8:44 pm

    Swap 2 and 1, and then 1 and 4, and then 1 and 8? Or is it a general question?

    For a more general approach you could try:

    1. Swapping every pair of 2 elements (with the highest sum) if they are perfect swaps (i.e. swapping them will put them both at their right spot). Th

    2. Use the lowest element as a pivot for swaps (by swapping the element whose spot it occupies), until it reaches its final spot

    3. Then, you have two possibilities:

      1. Repeat step 2: use the lowest element not in its final spot as a pivot until it reaches its final spot, then go back to step 3

      2. Or swap the lowest element not in its final spot (l2) with the lowest element (l1), repeat step 2 until l1 reaches the final spot of l2. Then:

        1. Either swap l1 and l2 again, go to step 3.1
        2. Or go to step 3.2 again, with the next lowest element not in its final spot being used.

    When all this is done, if some opposite swaps are performed one next to another (for example it could happen from going to step 2. to step 3.2.), remove them.

    There are still some things to watch out for, but this is already a pretty good approximation. Step one and two should always work though, step three would be the one to improve in some borderline cases.

    Example of the algorithm being used:

    With {8 4 5 3 2 7}: (target array {2 3 4 5 7 8})

    • Step 2: 2 <> 7, 2 <> 8

    • Array is now {2, 4, 5, 3, 7, 8}

    • Choice between 3.1 and 3.2:

      • 3.1 gives 3 <> 5, 3 <> 4

      • 3.2 gives 2 <> 3, 2 <> 5, 2 <> 4, 2 <> 3

      • 3 <> 5, 3 <> 4 is the better result

    Conclusion: 2 <> 7, 2 <> 8, 3 <> 5, 3 <> 4 is the best answer.

    With {1 8 9 7 6} (resulting array {1 6 7 8 9})

    • You’re beginning at step three already

    • Choice between 3.1 and 3.2:

      • 3.1 gives 6 <> 9, 6 <> 7, 6 <> 8 (total: 42)

      • 3.2 gives 1 <> 6, 1 <> 9, 1 <> 7, 1 <> 8, 1 <> 6 (total: 41)

    So 1 <> 6, 1 <> 9, 1 <> 7, 1 <> 8, 1 <> 6 is the best result

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: PHP Sort a multidimensional array by element containing date I have some
I have a Zend_Form with file element like this: ->addElement('file', 'image', array( 'required' =>
I need some sort of dynamic array in C++ where each element have their
Possible Duplicate: Using Perl, how can I sort an array using the value of
I have an array that can contain any number of elements. Each element contains
I have an array which i need to sort its elements by occurrence then
Suppose I have an array that mimics a database table. Each array element represents
how i can duplicate one element from array: for example, i have this array:
I have an array of Foo objects. How do I remove the second element
I have a two dimensional JSON array where each element contains several attributes. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.