I'm using a programming language that, inexplicably, doesn't have a "sort" command built in. However, it does let me send commands to the shell and get the output. So my idea is that I could send my data to the sort command in the Unix shell, and get back the sorted output.
But I don't know the syntax to feed it my data. I know how to use sort if the data is a file, and how to type in data to be sorted manually in the shell (type a bunch of lines and then CTRL-D to end the input), but I'm not sure how to pass data from a program. I couldn't find anything in the search engines about this.
Maybe I need to send a CTRL-D from my program as the final character of the data I'm sending? I'd be willing to try, but I don't know how to generate a CTRL-D as text that I could send....
Preparing all your data for output to the sort program, defining the command line for sort and reading the data back in seems a lot more work than writing yourself a simple sort algorithm in your programming language.
You didn't mention which programming language you use, but a simple sort algorithm with execution time equal to the square of the number of items could be written in practically any programming language as:
FOR a=1 to N-1 DO FOR b=a+1 to N DO IF item[a] > item[b] THEN swap( item[a], item[b] ); ENDIF; ENDFOR; ENDFOR;
More efficient sort algorithms like heapsort or quicksort require about 25 lines of code to write.
Thanks for the reply. The language is SuperTalk, but I just found out that it comes with an add-on that allows sorting ability, so my sorting problem is solved.
I had looked into writing my own sort, but it kind of scared me, since I have a hard time understanding the concepts. I think what you suggested is a "bubble" sort. I was looking at an "insertion" algorithm because it appeared to have better performance without being an order of magnitude more complex. [sorting-algorithms.com...]
Fortunately I didn't have to try to figure out how to code it in SuperTalk!
Good to hear that the problem can be solved inside your programming environment.
The example I proposed was indeed bubble sort. Not the most efficient algorithm, but it needs the least amount of code. It suffices for a maximum of a few thousand items, but with larger data sets gets really slow.