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?

Mimic lecturing on blackboard, facing audience

How much character growth crosses the line into breaking the character

Multiplicative persistence

Lowest total scrabble score

Calculate sum of polynomial roots

How to cover method return statement in Apex Class?

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

Why "had" in "[something] we would have made had we used [something]"?

Does IPv6 have similar concept of network mask?

Is this toilet slogan correct usage of the English language?

Is there a RAID 0 Equivalent for RAM?

Did arcade monitors have same pixel aspect ratio as TV sets?

Make a Bowl of Alphabet Soup

How do apertures which seem too large to physically fit work?

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

Pre-mixing cryogenic fuels and using only one fuel tank

putting logo on same line but after title, latex

System.QueryException unexpected token

Do we have to expect a queue for the shuttle from Watford Junction to Harry Potter Studio?

Temporarily disable WLAN internet access for children, but allow it for adults

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

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

What is Cash Advance APR?

Biological Blimps: Propulsion



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 39 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
            57 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
            57 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
            57 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 1 hour ago









          Carl WollCarl Woll

          71.2k394185




          71.2k394185











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
















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















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




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

          Благоевград Съдържание География | История | Население | Политика | Икономика и инфрастуктура | Здравеопазване | Образование и наука | Култура и забавления | Забележителности | Личности | Литература | Външни препратки | Бележки | Навигация42°01′18.99″ с. ш. 23°05′51″ и. д. / 42.021944° с. ш. 23.0975° и. д.*БлагоевградразширитередактиранеОфициален уебсайт на община БлагоевградНовинарски портал на Благоевград – blagoevgrad.euСайтове за БлагоевградНационален статистически институтdariknews.bgГригоровичъ, Викторъ. „Очеркъ путешествія по Европейской Турціи“. Москва, 1877.Стрезов, Георги. Два санджака от Източна Македония. Периодично списание на Българското книжовно дружество в Средец, кн. XXXVII и XXXVIII, 1891, стр. 18 – 19.Македония. Етнография и статистикаГаджанов, Димитър Г. Мюсюлманското население в Новоосвободените земи, в: Научна експедиция в Македония и Поморавието 1916, Военноиздателски комплекс „Св. Георги Победоносец“, Университетско издателство „Св. Климент Охридски“, София, 1993, стр. 244.паметник на незнайния четник&cd=18&hl=en&ct=clnk&client=firefox-a „История на днешен Благоевград“, взето от www.museumblg.com на 16 март 2010 г.„Справка за населението на град Благоевград, община Благоевград, област Благоевград, НСИ“„The population of all towns and villages in Blagoevgrad Province with 50 inhabitants or more according to census results and latest official estimates“„Ethnic composition, all places: 2011 census“История на Неврокопска епархия.Национален статистически институтМюсюлманско изповедание. Главно мюфтийствоНационален публичен регистър на храмовете в БългарияМюсюлманско изповедание. Главно мюфтийствоwww.dnes.bg Джамията в Благоевград не била паленаwww.sesc-bg.orgСписък на побратимени градовеТехническо побратимяванеГУМ грейва в цветовете на нощен Лас Вегас под името „Largo“, „МОЛ Благоевград“..., в. „Струма“grabo.bgwww.cinemaxbg.comррр4238731-067cad53a-0546-417b-a3d3-51e49b1d2232147736077147736077

          What is the best defense strategy for Survival in Grand Theft Auto Online?What is JP used for in Grand Theft Auto Online?How do I setup a Crew HQ in Grand Theft Auto Online?How does stealth work in Grand Theft Auto Online?Is it possible to own more than 10 cars in Grand Theft Auto online?Where to find truck/trailers in Grand Theft Auto OnlineWhat are some of the best missions to do on Grand Theft Auto 5 onlineFastest Car in Grand Theft Auto V PCHow to setup a Crew vs Crew online session in Grand Theft Auto Online?Grand theft auto 5 crossplayingRestart Grand Theft Auto V Online?

          How does Billy Russo acquire his 'Jigsaw' mask? Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Favourite questions and answers from the 1st quarter of 2019Why does Bane wear the mask?Why does Kylo Ren wear a mask?Why did Captain America remove his mask while fighting Batroc the Leaper?How did the OA acquire her wisdom?Is Billy Breckenridge gay?How does Adrian Toomes hide his earnings from the IRS?What is the state of affairs on Nootka Sound by the end of season 1?How did Tia Dalma acquire Captain Barbossa's body?How is one “Deemed Worthy”, to acquire the Greatsword “Dawn”?How did Karen acquire the handgun?