Writing insertion sort in an alternative way.

I was able to come up with a successful algorithm to sort a lnked list's index.

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
void listSort(struct listNode *head)
{
    struct listNode *i,
                    *j,
                    *min;
    i = head->next;
    while (i->next != NULL)
    {
        min = i;
        j = i->next;
        while (j->next != NULL)
        {
            if (*(j->next) < *(min->next))
                min = j;

            j = j->next;
        }
        
        listNode *temp1, *temp2, *temp3;
        
        temp1 = i->next;
        i->next = min->next;
        temp2 = min->next;
        temp3 = min->next->next;

        min->next = temp1;    

        temp2->next = temp1->next;
        temp1->next = temp3;
        i = i->next;
    
    }        
}


Now what I would like to do is: use a break statement wihtin the, if statement, to escape the while loop.

But I'm really stuck.

This is the farthest that I have been able to reach:

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
void listSort(struct listNode *head)
{
    struct listNode *i,
                    *j,
                    *min;
    i = head->next;
    while (i->next != NULL)
    {
        min = i;
        j = i->next;
        while (j->next != NULL)
        {
            if (*(j->next) < *(min->next))
                break;

            min = j->next;        //This may be wrong.
            //min = j->next;    //no difference.
        }
        
        //listNode *temp1, *temp2, *temp3;
        
        //temp1 = i->next;
        i->next = min->next;
        //temp2 = min->next;
        //temp3 = min->next->next;

        min->next = j->next; // temp1;    

        //temp2->next = temp1->next;
        //temp1->next = temp3;
        i = i->next;
    
    }        
}


This is what the "sorted list" display: 0 1 4 8 7

Instead of the following 9 total number: 0 1 2 3 4 5 6 7 8 9

This is the full 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

struct listNode {
    int val;
    struct listNode *next;
    bool operator<(const struct listNode &n2) const {
        return this->val < n2.val;
    }
    bool operator>(const struct listNode &n2) const {
        return this->val > n2.val;
    }
};

struct listNode *newListNode(int val);
void deleteList(struct listNode *);
bool nodeSwap(struct listNode *, struct listNode *);
void listSort(struct listNode *head);
void printList(struct listNode *);
 
int main(int argc, char *argv[]) {
    struct listNode *head,*p;

	int SIZE = 10;
	int n[] = { 0, 2, 1, 5, 4, 3, 6, 8, 9, 7};


	p = head = newListNode(-1);
	for(int i = 0; i < SIZE; i++)
	{
        p->next = newListNode(n[i]);
        p = p->next;
	}


    cout << endl;
    printList(head);
    cout << "sorted ascending ..." << endl;
    listSort(head);
    printList(head);

	deleteList(head);

	system("pause");
    return 0;
}



void listSort(struct listNode *head)
{
    struct listNode *i,
					*j,
					*min;
	i = head;
	while (i->next != NULL)
	{
        min = i;
		j = i->next;
		while (j->next != NULL)
		{
			if (*(j->next) < *(min->next)) 
				break;

			min = j->next;		//This may be wrong.
			//min = j->next;	//no difference.
		}
		
		//listNode *temp1, *temp2, *temp3;
		
		//temp1 = i->next;
        i->next = min->next;
		//temp2 = min->next;
		//temp3 = min->next->next;

        min->next = j->next; // temp1;	

		//temp2->next = temp1->next;
		//temp1->next = temp3;
		i = i->next;
	
    }    	
}

struct listNode *newListNode(int val) 
{
    struct listNode *n = new listNode;
    n->val = val;
    n->next = NULL;
    return n;
}

void deleteList(struct listNode *head) {
    struct listNode *p = head, *q;
    while (p != NULL) 
	{
        q = p->next;
        free(p);
        p = q;
    }
}


Last edited on
Your first snip looks like `selection sort'
However, I would like an explanation of the structure as
1
2
    i = head->next;
    while (i->next != NULL)
looks incorrect (¿why are you sure that can dereference head->next?)

For the second snip ¿what are you trying to do?
I'm trying to sort:0, 2, 1, 5, 4, 3, 6, 8, 9, 7, in ascending order.

Doing: while (i != NULL) results in an error, with the nested while loop failing to execute.
So, I stick with, due to better results: while (i->next != NULL)

You could say I'm trying to do insrtion sort, after having success with selection sort.

Only half the the numbers display.
Last edited on
Doing: while (i != NULL) results in an error, with the nested while loop failing to execute.
So, I stick with, due to better results: while (i->next != NULL)
Test with an empty list
Test with the first element out of order.

Second snip
1
2
3
while (j->next != NULL){
   //code that does not modify `j' or `j->next'
}
The logic is way off. Do this auxiliar function void inorder_insert( listNode *begin, listNode *end, listNode *value );
that would insert the value in the list that goes from begin to end (that's sorted) in the place where it should be.


As you are not doing `selection sort', having a `min' variable is obfuscated.
The assignment requires the pre-existing nodes in the linked list to be re-arranged, by a sort function.

Selection/Insertion sort or whatever I don't care. My point is, I'm trying to get the algorithm to work using a break statement in the if statement.

I already, I got the algorithm to work perfectly, without a break statement, in my first post.
Now I'd like to learn how to get the algorithm to work using a break statement.

By the way in line 58, I have changed it to: i = head;
Last edited on
Topic archived. No new replies allowed.