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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
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 <<list[k++] << " ";
    cout << endl;
    
    return 0;
}
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//version 1.2 works as of 3:32 Sunday morning. 
//Compiled in Dev C++ 4.9.9.2
#include <iostream>
#include <fstream>
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.