Terse Method to Swap Lowest for Highest?Efficient method for Inserting arrays into arraysSwap elements in list without copyBetter method to swap the values of two 2-D arraysHow to get this list with a terse methodBuilt-in (or Terse) Method to Combine and Transpose DatasetsAre there more readable and terse method can get this listefficiently method for generating a sequenceSimple method to sort versionsFunction for SortBySwap Elements of a continuous List, possible?

Calculating total slots

What should you do if you miss a job interview (deliberately)?

Can I say "fingers" when referring to toes?

What exactly color does ozone gas have?

What are the balance implications behind making invisible things auto-hide?

How to explain what's wrong with this application of the chain rule?

What features enable the Su-25 Frogfoot to operate with such a wide variety of fuels?

PTIJ: Haman's bad computer

Can a stoichiometric mixture of oxygen and methane exist as a liquid at standard pressure and some (low) temperature?

Non-trope happy ending?

Does malloc reserve more space while allocating memory?

Store Credit Card Information in Password Manager?

Is there a way to get `mathscr' with lower case letters in pdfLaTeX?

Why Shazam when there is already Superman?

What to do when eye contact makes your subordinate uncomfortable?

Why can Carol Danvers change her suit colours in the first place?

What is the evidence for the "tyranny of the majority problem" in a direct democracy context?

Quoting Keynes in a lecture

Yosemite Fire Rings - What to Expect?

Why is short-wave infrared portion of electromagnetic spectrum so sensitive to fire?

Does an advisor owe his/her student anything? Will an advisor keep a PhD student only out of pity?

How should I respond when I lied about my education and the company finds out through background check?

Fear of getting stuck on one programming language / technology that is not used in my country

Why does AES have exactly 10 rounds for a 128-bit key, 12 for 192 bits and 14 for a 256-bit key size?



Terse Method to Swap Lowest for Highest?


Efficient method for Inserting arrays into arraysSwap elements in list without copyBetter method to swap the values of two 2-D arraysHow to get this list with a terse methodBuilt-in (or Terse) Method to Combine and Transpose DatasetsAre there more readable and terse method can get this listefficiently method for generating a sequenceSimple method to sort versionsFunction for SortBySwap Elements of a continuous List, possible?













3












$begingroup$


I have built a solution to swap the lowest values with the highest values in a list.



With



SeedRandom[987]
test = RandomSample@*Join @@ Range @@@ 6, 10, 56, 60, 1, 5, -5, -1



-1, 2, 7, 8, 60, 57, 58, 10, 9, 4, -5, -3, 3, 59, 1, 5, -4, 6, -2, 56



Then



swapPositions =
PermutationReplace[
Ordering@Ordering@test,
With[len = Length@test,
Cycles@
Transpose@Range @@ 1, Floor[len/2], Reverse@*Range @@ Ceiling[len/2] + 1, len
]
];

Sort[test][[swapPositions]]



56, 9, 4, 3, -5, -2, -3, 1, 2, 7, 60, 58, 8, -4, 10, 6, 59, 5, 57, -1



The largest half of the numbers have had their positions swapped with lowest half of the numbers.



However, it feels too verbose and I think Sort might be expensive in this case. Is there a built-in function or more terse method to achieve this. Of course with no loss in speed. The actual case is for list of length 100000 and more.










share|improve this question











$endgroup$
















    3












    $begingroup$


    I have built a solution to swap the lowest values with the highest values in a list.



    With



    SeedRandom[987]
    test = RandomSample@*Join @@ Range @@@ 6, 10, 56, 60, 1, 5, -5, -1



    -1, 2, 7, 8, 60, 57, 58, 10, 9, 4, -5, -3, 3, 59, 1, 5, -4, 6, -2, 56



    Then



    swapPositions =
    PermutationReplace[
    Ordering@Ordering@test,
    With[len = Length@test,
    Cycles@
    Transpose@Range @@ 1, Floor[len/2], Reverse@*Range @@ Ceiling[len/2] + 1, len
    ]
    ];

    Sort[test][[swapPositions]]



    56, 9, 4, 3, -5, -2, -3, 1, 2, 7, 60, 58, 8, -4, 10, 6, 59, 5, 57, -1



    The largest half of the numbers have had their positions swapped with lowest half of the numbers.



    However, it feels too verbose and I think Sort might be expensive in this case. Is there a built-in function or more terse method to achieve this. Of course with no loss in speed. The actual case is for list of length 100000 and more.










    share|improve this question











    $endgroup$














      3












      3








      3





      $begingroup$


      I have built a solution to swap the lowest values with the highest values in a list.



      With



      SeedRandom[987]
      test = RandomSample@*Join @@ Range @@@ 6, 10, 56, 60, 1, 5, -5, -1



      -1, 2, 7, 8, 60, 57, 58, 10, 9, 4, -5, -3, 3, 59, 1, 5, -4, 6, -2, 56



      Then



      swapPositions =
      PermutationReplace[
      Ordering@Ordering@test,
      With[len = Length@test,
      Cycles@
      Transpose@Range @@ 1, Floor[len/2], Reverse@*Range @@ Ceiling[len/2] + 1, len
      ]
      ];

      Sort[test][[swapPositions]]



      56, 9, 4, 3, -5, -2, -3, 1, 2, 7, 60, 58, 8, -4, 10, 6, 59, 5, 57, -1



      The largest half of the numbers have had their positions swapped with lowest half of the numbers.



      However, it feels too verbose and I think Sort might be expensive in this case. Is there a built-in function or more terse method to achieve this. Of course with no loss in speed. The actual case is for list of length 100000 and more.










      share|improve this question











      $endgroup$




      I have built a solution to swap the lowest values with the highest values in a list.



      With



      SeedRandom[987]
      test = RandomSample@*Join @@ Range @@@ 6, 10, 56, 60, 1, 5, -5, -1



      -1, 2, 7, 8, 60, 57, 58, 10, 9, 4, -5, -3, 3, 59, 1, 5, -4, 6, -2, 56



      Then



      swapPositions =
      PermutationReplace[
      Ordering@Ordering@test,
      With[len = Length@test,
      Cycles@
      Transpose@Range @@ 1, Floor[len/2], Reverse@*Range @@ Ceiling[len/2] + 1, len
      ]
      ];

      Sort[test][[swapPositions]]



      56, 9, 4, 3, -5, -2, -3, 1, 2, 7, 60, 58, 8, -4, 10, 6, 59, 5, 57, -1



      The largest half of the numbers have had their positions swapped with lowest half of the numbers.



      However, it feels too verbose and I think Sort might be expensive in this case. Is there a built-in function or more terse method to achieve this. Of course with no loss in speed. The actual case is for list of length 100000 and more.







      list-manipulation performance-tuning sorting






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 13 mins ago









      m_goldberg

      87.7k872198




      87.7k872198










      asked 50 mins ago









      EdmundEdmund

      26.6k330102




      26.6k330102




















          1 Answer
          1






          active

          oldest

          votes


















          5












          $begingroup$

          How about:



          Module[tmp = test,
          With[ord=Ordering[tmp],
          tmp[[ord]] = Reverse @ tmp[[ord]]];
          tmp
          ]



          56, 9, 4, 3, -5, -2, -3, 1, 2, 7, 60, 58, 8, -4, 10, 6, 59, 5, 57, -1







          share|improve this answer









          $endgroup$












          • $begingroup$
            That is so obvious I want to cry. Thanks (+1).
            $endgroup$
            – Edmund
            32 mins ago










          Your Answer





          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "387"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f193790%2fterse-method-to-swap-lowest-for-highest%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5












          $begingroup$

          How about:



          Module[tmp = test,
          With[ord=Ordering[tmp],
          tmp[[ord]] = Reverse @ tmp[[ord]]];
          tmp
          ]



          56, 9, 4, 3, -5, -2, -3, 1, 2, 7, 60, 58, 8, -4, 10, 6, 59, 5, 57, -1







          share|improve this answer









          $endgroup$












          • $begingroup$
            That is so obvious I want to cry. Thanks (+1).
            $endgroup$
            – Edmund
            32 mins ago















          5












          $begingroup$

          How about:



          Module[tmp = test,
          With[ord=Ordering[tmp],
          tmp[[ord]] = Reverse @ tmp[[ord]]];
          tmp
          ]



          56, 9, 4, 3, -5, -2, -3, 1, 2, 7, 60, 58, 8, -4, 10, 6, 59, 5, 57, -1







          share|improve this answer









          $endgroup$












          • $begingroup$
            That is so obvious I want to cry. Thanks (+1).
            $endgroup$
            – Edmund
            32 mins ago













          5












          5








          5





          $begingroup$

          How about:



          Module[tmp = test,
          With[ord=Ordering[tmp],
          tmp[[ord]] = Reverse @ tmp[[ord]]];
          tmp
          ]



          56, 9, 4, 3, -5, -2, -3, 1, 2, 7, 60, 58, 8, -4, 10, 6, 59, 5, 57, -1







          share|improve this answer









          $endgroup$



          How about:



          Module[tmp = test,
          With[ord=Ordering[tmp],
          tmp[[ord]] = Reverse @ tmp[[ord]]];
          tmp
          ]



          56, 9, 4, 3, -5, -2, -3, 1, 2, 7, 60, 58, 8, -4, 10, 6, 59, 5, 57, -1








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 37 mins ago









          Carl WollCarl Woll

          71.2k394185




          71.2k394185











          • $begingroup$
            That is so obvious I want to cry. Thanks (+1).
            $endgroup$
            – Edmund
            32 mins ago
















          • $begingroup$
            That is so obvious I want to cry. Thanks (+1).
            $endgroup$
            – Edmund
            32 mins ago















          $begingroup$
          That is so obvious I want to cry. Thanks (+1).
          $endgroup$
          – Edmund
          32 mins ago




          $begingroup$
          That is so obvious I want to cry. Thanks (+1).
          $endgroup$
          – Edmund
          32 mins ago

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Mathematica Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f193790%2fterse-method-to-swap-lowest-for-highest%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

          Prove that NP is closed under karp reduction?Space(n) not closed under Karp reductions - what about NTime(n)?Class P is closed under rotation?Prove or disprove that $NL$ is closed under polynomial many-one reductions$mathbfNC_2$ is closed under log-space reductionOn Karp reductionwhen can I know if a class (complexity) is closed under reduction (cook/karp)Check if class $PSPACE$ is closed under polyonomially space reductionIs NPSPACE also closed under polynomial-time reduction and under log-space reduction?Prove PSPACE is closed under complement?Prove PSPACE is closed under union?

          Is my guitar’s action too high? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Strings too stiff on a recently purchased acoustic guitar | Cort AD880CEIs the action of my guitar really high?Μy little finger is too weak to play guitarWith guitar, how long should I give my fingers to strengthen / callous?When playing a fret the guitar sounds mutedPlaying (Barre) chords up the guitar neckI think my guitar strings are wound too tight and I can't play barre chordsF barre chord on an SG guitarHow to find to the right strings of a barre chord by feel?High action on higher fret on my steel acoustic guitar