Message Boards Message Boards

0
|
4936 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

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?
POSTED BY: Nestor Grace
3 Replies
I noticed you write 
i < Length
But Length is a function, that needs an argument. see ?Length
POSTED BY: Nasser M. Abbasi
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]
POSTED BY: Andrei Bobrov
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract