forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Selection_Sort.dart
63 lines (49 loc) · 1009 Bytes
/
Selection_Sort.dart
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
//selection sort
import 'dart:io';
void selection_sort (List<int> arr, int n)
{
int temp, min_index;
for (int i = 0; i < n - 1; i++)
{
min_index = i;
for (int j = i + 1; j < n; j++)
if ( arr[j] < arr[min_index] )
min_index = j;
if (i != min_index)
{
temp = arr[i];
arr[i] = arr[min_index];
arr[min_index] = temp;
}
}
}
main()
{
print ("Enter the size of array ");
int size = int.parse (stdin.readLineSync());
List <int> array = List();
for (int i = 0; i < size; i++)
{
print ("Enter $i element ");
var ele = int.parse (stdin.readLineSync());
array.add (ele);
}
selection_sort (array, size);
print (array);
}
/*
Enter the size of array
5
Enter 0 element
50
Enter 1 element
40
Enter 2 element
30
Enter 3 element
20
Enter 4 element
10
Sorted array is
[10, 20, 30, 40, 50]
*/