### Sweet mother of magical sort functions, can somebody explain this?

I wrote this sort function while absolutely sleep deprived, and it makes absolutely no sense to me. It seems to go off the back end of the array (list) when the len is 0 and do other illegal things, but it just plain works perfectly in every case I have tried. Perhaps I am mistaken, but this code should not work. Use it to sort a list of int or double variables. Just include initialize the proper variables etc/drop it in a function. Was compiled in Dev C++ 4.9.9.2

 ``123456789101112131415161718192021222324`` ``````//all of this contained in a function bool fin = 0; double temp; int len = 0; double list[1000]; // or however many you need ifstream input; //input.open("fileNameGoesHere.txt"); while(!fin) { input >> inVal; if (!input) fin = 1; else { list[len] = inVal; for (int i = len; i >= 0; i--) { if (list[i] < inVal) { //fixed temp = list[i]; list[i] = inVal; list[i+1] = temp; } } len++; } }``````

Please halp! This is black magic to me... and I wrote it.
Last edited on
You got me excited man. I was really hoping that line `if (list < inVal) {` would work, but unfortunately

 error: ISO C++ forbids comparison between pointer and integer

This is how I tested it:
 ``123456789101112131415161718192021222324252627`` ``````#include using namespace std; int main() { int list[] = {4, 5, 6, 7, 3, 2, -1, -34, 9, 6, 8, 3}, len = 0, temp, k = 0; int s = sizeof list / sizeof (int); for (int i = len; i >= 0; i--) { if (list < list[len]) { cout << "The value of 'List' is " << list << endl; temp = list[i]; list[i] = list[len]; list[i+1] = temp; } } len++; while(k < s) cout <
Terribly sorry, the line in question was

if (list[i] < inVal);

I straight up copied that and pasted it.. Computers just vexing me again today...

But yeah take that for a spin.

edit... temp is just a double variable, not an array of doubles.

Here is the whole sloppy program for anyone who wants exactly what I have.. also I just learned about the code tag so it will look better on these forums now.

full bailey sort code

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` ``````//version 1.2 works as of 3:32 Sunday morning. //Compiled in Dev C++ 4.9.9.2 #include #include using namespace std; int main() { double inVal; int len = 0; double temp; double list[100000]; bool fin = 0; ifstream input; ofstream output; input.open("newRandomDoubleVals.txt"); output.open("sortedDerps.txt"); while(!fin) { input >> inVal; if (!input) fin = 1; else { list[len] = inVal; for (int i = len; i >= 0; i--) { if (list[i] < inVal) { temp = list[i]; list[i] = inVal; list[i+1] = temp; } } len++; } } for (int i = 0; i < len; i++) { cout << list[i]; cout << "\n"; output << list[i]; output << "\n"; } input.close(); output.close(); system("pause"); return 0; } ``````
Last edited on
Hey, I thought people would be more interested in this. I think it deserves a second opinion. Thanks!
Topic archived. No new replies allowed.