Terse Method to Swap Lowest for Highest?Efficient method for Inserting arrays into arraysSwap elements in...

Invalid date error by date command

How should I address a possible mistake to co-authors in a submitted paper

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

Calculate sum of polynomial roots

Biological Blimps: Propulsion

What is the highest possible scrabble score for placing a single tile

How to cover method return statement in Apex Class?

Does Doodling or Improvising on the Piano Have Any Benefits?

Yosemite Fire Rings - What to Expect?

How does a computer interpret real numbers?

Why is it that I can sometimes guess the next note?

What are the advantages of simplicial model categories over non-simplicial ones?

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

It grows, but water kills it

Calculating total slots

Lowest total scrabble score

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

Does the UK parliament need to pass secondary legislation to accept the Article 50 extension

Do the primes contain an infinite almost arithmetic progression?

Why is the "ls" command showing permissions of files in a FAT32 partition?

Why does a simple loop result in ASYNC_NETWORK_IO waits?

Terse Method to Swap Lowest for Highest?

Mimic lecturing on blackboard, facing audience

Creepy dinosaur pc game identification



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 26 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
            44 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
            44 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
            44 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 49 mins ago









          Carl WollCarl Woll

          71.2k394185




          71.2k394185












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


















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
















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




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

          迭戈·戈丁...

          A phrase ”follow into" in a context The 2019 Stack Overflow Developer Survey Results Are...

          1960s short story making fun of James Bond-style spy fiction The 2019 Stack Overflow Developer...