forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Linked_List_Selection_Sort.java
99 lines (85 loc) · 1.46 KB
/
Linked_List_Selection_Sort.java
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
/*Linked List implementation of Selection sort in java*/
public class selection_sort {
node head;
public static class node
{
int data;
node next;
node(int value)
{
data = value;
next = null;
}
}
public void selectionSort()
{
node beg=head;
node min=head;
while(beg.next!=null)
{
min=beg;
node current=beg.next;
while(current!=null)
{
if(current.data<min.data)
{
min=current;
}
current=current.next;
}
int temp = beg.data;
beg.data = min.data;
min.data = temp;
beg=beg.next;
}
}
public void create(int value)
{
node temp = new node(value);
node current= head;
if(head==null)
{
head=temp;
return;
}
while(current.next!=null)
current = current.next;
current.next = temp;
}
public void display()
{
node current=head;
if(current==null)
{
System.out.println("Empty list");
return;
}
while(current.next!=null)
{
System.out.print(current.data+" -> ");
current=current.next;
}
System.out.print(current.data);
System.out.println();
}
public static void main(String[] args) {
selection_sort ob=new selection_sort();
ob.create(11);
ob.create(5);
ob.create(3);
ob.create(6);
ob.create(13);
ob.create(10);
System.out.println("Output:");
ob.display();
System.out.println("After selection sort : ");
ob.selectionSort();
ob.display();
}
}
/*
Output:
11 -> 5 -> 3 -> 6 -> 13 -> 10
After selection sort :
3 -> 5 -> 6 -> 10 -> 11 -> 13
*/