0
|
4700 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
GROUPS:

# How to create a sorting algorithm that sorts like Sort[] function?

Posted 11 years ago
 I have been trying to write a program to sort a list and cannot seem to get it right...:Triliste[a_] :=  Module[{min, x, i, j}, For[i = 1, i < Length[a], min := i;      For[j = i + 1, j <= Length[a],      If[a[[j]] < a[[min]], min := j]]; x,      If[min! i, x := a[[i]]; a[[i]] = a[[min]]; a[[min]] := x]]    ] a;what is wrong?
3 Replies
Sort By:
Posted 11 years ago
 I noticed you write  i < LengthBut Length is a function, that needs an argument. see ?Length
Posted 11 years ago
 See this tutorial: Implement the Bubble Sort Algorithm with Patterns and Rules.
Posted 11 years ago
 Please try this is code:Triliste[a_] := Module[{x = a}, m = Length[a]; For[i = 1, i < m, i++,For[j = i + 1, j <= m, j++, If[x[[i]] > x[[j]], x[[{i, j}]] = x[[{j, i}]]]]]; x]
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.