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?

This is why we puzzle

Open a doc from terminal, but not by its name

Calculate sum of polynomial roots

Store Credit Card Information in Password Manager?

Electoral considerations aside, what are potential benefits, for the US, of policy changes proposed by the tweet recognizing Golan annexation?

Why does a simple loop result in ASYNC_NETWORK_IO waits?

Why is so much work done on numerical verification of the Riemann Hypothesis?

How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?

Can disgust be a key component of horror?

What are some good ways to treat frozen vegetables such that they behave like fresh vegetables when stir frying them?

Probability that THHT occurs in a sequence of 10 coin tosses

What if you are holding an Iron Flask with a demon inside and walk into Antimagic Field?

Invalid date error by date command

Why is this estimator biased?

Add big quotation marks inside my colorbox

PTIJ: Haman's bad computer

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

Why would a new[] expression ever invoke a destructor?

Unexpected behavior of the procedure `Area` on the object 'Polygon'

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

Why should universal income be universal?

Is there an injective, monotonically increasing, strictly concave function from the reals, to the reals?

The IT department bottlenecks progress, how should I handle this?

Does malloc reserve more space while allocating memory?



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?













4












$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$
















    4












    $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$














      4












      4








      4





      $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 32 mins ago









      m_goldberg

      87.7k872198




      87.7k872198










      asked 1 hour ago









      EdmundEdmund

      26.6k330102




      26.6k330102




















          1 Answer
          1






          active

          oldest

          votes


















          7












          $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
            50 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









          7












          $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
            50 mins ago















          7












          $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
            50 mins ago













          7












          7








          7





          $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 55 mins ago









          Carl WollCarl Woll

          71.2k394185




          71.2k394185











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
















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















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




          $begingroup$
          That is so obvious I want to cry. Thanks (+1).
          $endgroup$
          – Edmund
          50 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

          How should I use the fbox command correctly to avoid producing a Bad Box message?How to put a long piece of text in a box?How to specify height and width of fboxIs there an arrayrulecolor-like command to change the rule color of fbox?What is the command to highlight bad boxes in pdf?Why does fbox sometimes place the box *over* the graphic image?how to put the text in the boxHow to create command for a box where text inside the box can automatically adjust?how can I make an fbox like command with certain color, shape and width of border?how to use fbox in align modeFbox increase the spacing between the box and it content (inner margin)how to change the box height of an equationWhat is the use of the hbox in a newcommand command?

          Doxepinum Nexus interni Notae | Tabula navigationis3158DB01142WHOa682390"Structural Analysis of the Histamine H1 Receptor""Transdermal and Topical Drug Administration in the Treatment of Pain""Antidepressants as antipruritic agents: A review"

          inputenc: Unicode character … not set up for use with LaTeX The Next CEO of Stack OverflowEntering Unicode characters in LaTeXHow to solve the `Package inputenc Error: Unicode char not set up for use with LaTeX` problem?solve “Unicode char is not set up for use with LaTeX” without special handling of every new interesting UTF-8 characterPackage inputenc Error: Unicode character ² (U+B2)(inputenc) not set up for use with LaTeX. acroI2C[I²C]package inputenc error unicode char (u + 190) not set up for use with latexPackage inputenc Error: Unicode char u8:′ not set up for use with LaTeX. 3′inputenc Error: Unicode char u8: not set up for use with LaTeX with G-BriefPackage Inputenc Error: Unicode char u8: not set up for use with LaTeXPackage inputenc Error: Unicode char ́ (U+301)(inputenc) not set up for use with LaTeX. includePackage inputenc Error: Unicode char ̂ (U+302)(inputenc) not set up for use with LaTeX. … $widehatleft (OA,AA' right )$Package inputenc Error: Unicode char â„¡ (U+2121)(inputenc) not set up for use with LaTeX. printbibliography[heading=bibintoc]Package inputenc Error: Unicode char − (U+2212)(inputenc) not set up for use with LaTeXPackage inputenc Error: Unicode character α (U+3B1) not set up for use with LaTeXPackage inputenc Error: Unicode characterError: ! Package inputenc Error: Unicode char ⊘ (U+2298)(inputenc) not set up for use with LaTeX