-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathselection_sort.cpp
More file actions
99 lines (74 loc) · 1.6 KB
/
selection_sort.cpp
File metadata and controls
99 lines (74 loc) · 1.6 KB
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
// C++ implementation of recursive selection sort
#include <bits/stdc++.h>
using namespace std;
// A Linked list node
struct Node {
int data;
struct Node* next;
};
void swapNodes(struct Node** head_ref, struct Node* currX,
struct Node* currY, struct Node* prevY)
{
*head_ref = currY;
prevY->next = currX;
struct Node* temp = currY->next;
currY->next = currX->next;
currX->next = temp;
}
struct Node* recurSelectionSort(struct Node* head)
{
// if there is only a single node
if (head->next == NULL)
return head;
struct Node* min = head;
struct Node* beforeMin = NULL;
struct Node* ptr;
for (ptr = head; ptr->next != NULL; ptr = ptr->next) {
if (ptr->next->data < min->data) {
min = ptr->next;
beforeMin = ptr;
}
}
if (min != head)
swapNodes(&head, head, min, beforeMin);
head->next = recurSelectionSort(head->next);
return head;
}
void sort(struct Node** head_ref)
{
// if list is empty
if ((*head_ref) == NULL)
return;
}
void push(struct Node** head_ref, int new_data)
{
struct Node* new_node =
(struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(struct Node* head)
{
while (head != NULL) {
cout << head->data << " ";
head = head->next;
}
}
int main()
{
struct Node* head = NULL;
// create linked list 10->12->8->4->6
push(&head, 6);
push(&head, 4);
push(&head, 8);
push(&head, 12);
push(&head, 10);
cout << "Linked list before sorting:n";
printList(head);
// sort the linked list
sort(&head);
cout << "\nLinked list after sorting:n";
printList(head);
return 0;
}