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

  • SEARCH
  • Home
  • 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 8609145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:48:23+00:00 2026-06-12T03:48:23+00:00

Well, I’m having trouble with this code, it’s about writing the Selection Sort alghorithm

  • 0

Well, I’m having trouble with this code, it’s about writing the Selection Sort alghorithm in Mathematica, but inverted, I mean, instead of searching for the smallest number and place it in the first position of a list, I need to search for the biggest one and place it in the last position.
I’ve written this code but as I’m new to Mathematica, I can’t find the solution. It doesn’t sort the list. Thank you very much for reading, your answers will be helpfull!

      L = {};
n = Input["Input the size of the list (a number): "];
For[i = 1, i <= n, m = Input["Input a number to place in the list:"]; 
 L = Append[L, m]; i++]
SelectSort[L] := 
 Module[{n = 1, temp, xi = L, j}, While[n <= Length@L, temp = xi[[n]];
   For[j = n, j <= Length@L, j++, If[xi[[j]] < temp, temp = xi[[j]]];];
   xi[[n ;;]] = {temp}~Join~
     Delete[xi[[n ;;]], First@Position[xi[[n ;;]], temp]];
   n++;];
  xi]
Print[L]
  • 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-06-12T03:48:25+00:00Added an answer on June 12, 2026 at 3:48 am

    Here is a working version. In the SelectSort[] function I only had to change the function variable to a pattern variable, i.e. L_. Other than that it seems to work.

    (* Function definition *)
    SelectSort[L_] := Module[{n = 1, temp, xi = L, j},
      While[n <= Length@L,
       temp = xi[[n]];
       For[j = n, j <= Length@L, j++,
        If[xi[[j]] < temp, temp = xi[[j]]];
        ];
       xi[[n ;;]] = {temp}~Join~
         Delete[xi[[n ;;]], First@Position[xi[[n ;;]], temp]];
       n++;];
      xi]
    
    (* Run section *)
    L = {};
    n = Input["Input the size of the list (a number): "];
    For[i = 1, i <= n, m = Input["Input a number to place in the list:"];
     L = Append[L, m]; i++]
    SelectSort[L]
    Print[L]
    

    {3, 3, 5, 7, 8}

    {8, 3, 5, 7, 3}

    The output is first the sorted list from SelectSort[L], then the original input list,L.

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

Sidebar

Related Questions

Well, I know there is a lots of posts about it, but I have
well I'm starting to understand Android Fragments but this is still confusing for me.
Well, I have this bit of code that is slowing down the program hugely
Well the problem is that I was using code like this: new Date().toJSON().slice(0, 10)
Well I think this is kinda dumb but, I wonder if there's any way
Well, first I know that this question was already asked several times. But I
Well, it may actually be a simple case but I'm having a tough time
Well friends, I have got this query which works but is very long for
well i have a configuration like this in the components part of my config
Well, I have a application which somehow requires some system resources, but how do

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.