-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselectionSortLinkedList.c
80 lines (60 loc) · 1.4 KB
/
selectionSortLinkedList.c
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
#include <stdio.h>
#include <stdlib.h>
struct node {
int val;
struct node * next;
};
typedef struct node * NodeAddress;
NodeAddress makeLinked(int *A, int num) {
NodeAddress head, tmp = NULL;
if(num>0) {
head = malloc( sizeof(struct node) );
head->val = A[0];
head->next = NULL;
tmp = head;
}
for(int i=1; i<num; i++) {
tmp->next = malloc( sizeof(struct node) );
tmp = tmp->next;
tmp->val = A[i];
tmp->next = NULL;
}
return head;
}
void selectionSort(int *A, int n) {
int biggest;
for(int i=n; i>1; i=i-1) {
if(i<1) {
biggest = -1;
}
biggest = 0;
for(int j=1; j<i; j++) {
if( A[j] > A[biggest] ) {
biggest = j;
}
}
int tmp = A[biggest];
A[biggest] = A[i-1];
A[i-1] = tmp;
}
}
int main() {
int num;
NodeAddress linkedList;
printf("Enter size of array : ");
scanf("%d", &num);
int A[num];
for (int i=0; i<num; i++) {
int x;
printf("Enter an element : ");
scanf("%d", &x);
A[i] = x;
}
linkedList = makeLinked(A,num);
selectionSort(A,num);
printf("Linked List after Selection Sort :-\n");
for (int i=0; i<num; i++) {
printf("%d\n", A[i]);
}
return 0;
}