Functions in Mathematica - sort function

In summary, the conversation discusses how to write a function called sort[list_] that uses Pattern Matching to sort a list of numbers. It is suggested to use a For loop with an If statement, with the last argument being optional. The correct syntax for the If statement is also mentioned.
  • #1
Physics_rocks
12
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
  • #2
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
 
  • #3
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; ]

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

My bad!
 

Related to Functions in Mathematica - sort function

1. What is the purpose of the "sort" function in Mathematica?

The "sort" function is used to arrange a list of items in either ascending or descending order, based on a given criteria. It is commonly used to organize data or to find the highest or lowest values in a set of numbers.

2. How do you use the "sort" function in Mathematica?

To use the "sort" function, you must first have a list of items that you want to organize. Then, you can use the syntax Sort[list, criteria], where "list" is your list of items and "criteria" is the sorting rule you want to apply. For example, Sort[{5, 2, 8, 3}, Greater] will arrange the items in descending order.

3. Can the "sort" function be used on different types of data in Mathematica?

Yes, the "sort" function can be used on a variety of data types in Mathematica, including numbers, strings, and even expressions. However, the sorting criteria may vary depending on the data type. For example, sorting strings will use alphabetical order, while sorting expressions will use the default order of operations.

4. What happens if I use the "sort" function on a list with duplicate elements?

If your list contains duplicate elements, the "sort" function will arrange them in the order they appear in the original list. For example, Sort[{5, 2, 5, 3}] will result in {2, 3, 5, 5}.

5. Are there any other functions in Mathematica that work similarly to the "sort" function?

Yes, there are several other functions in Mathematica that can be used for sorting, such as SortBy, Ordering, and SortByDescending. These functions offer more flexibility in terms of the criteria used for sorting and can be useful for more complex data sets.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
419
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
0
Views
434
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
Back
Top