Functions in Mathematica - sort function

Click For Summary

Discussion Overview

The discussion revolves around implementing a sorting function in Mathematica that sorts a list of numbers by comparing and potentially swapping adjacent elements. The focus is on using pattern matching and understanding control structures like loops and conditional statements in Mathematica.

Discussion Character

  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant seeks to create a sorting function using pattern matching and asks how to iterate through a list and implement conditional swapping.
  • Another participant suggests using a For loop combined with an If statement to achieve the desired functionality.
  • A later reply presents a proposed implementation of the sorting function but raises questions about the syntax used in the If statement.
  • Subsequent responses identify and acknowledge a mistake in the syntax related to the use of semicolons and commas within the If statement.

Areas of Agreement / Disagreement

Participants generally agree on the need for a loop and conditional statements, but there is disagreement regarding the correct syntax for the If statement, which remains unresolved.

Contextual Notes

There are limitations in the proposed implementation, particularly regarding the syntax and structure of the If statement, which may affect the function's execution.

Physics_rocks
Messages
12
Reaction score
0
Hi ,

I'm trying to write down the following function :

sort[list_]

it needs to sort a list of numbers by checking each two adjacent numbers and swap them
if it is needed , but it doesn't work . I need to implement it using Pattern Matching .

but I don't understand how can I interate in "list" , meaning how can I ask an "IF" question
such as If list < list[i+1] then swap(list,list[i+1]) .

10x
 
Physics news on Phys.org
You are looking for a For loop + If statement?

Code:
For[i = 1; i <= Length[list]; i++;
  If[ test; whatToDoIfTestIsTrue; whatToDoIfTestIsFalse]

The last argument (whatToDoIfTestIsFalse) is optional, you can leave it (and the trailing semicolon) out
 
CompuChip said:
You are looking for a For loop + If statement?

Code:
For[i = 1; i <= Length[list]; i++;
  If[ test; whatToDoIfTestIsTrue; whatToDoIfTestIsFalse]

The last argument (whatToDoIfTestIsFalse) is optional, you can leave it (and the trailing semicolon) out

Thank you .
This would do ?

PHP:
sort[list_] := 
 For[i = 1 ; i < Length[list]; i++; 
  If [ list[i] < list[i + 1] ; j = list[i + 1] , 
   list[i + 1] = list[i], list[i] = j; ]

?
 
I think you have the semicolons and commas reversed inside the If statement.
 
Whoops, you do.

My bad!
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 0 ·
Replies
0
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K