-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSelectionSort.hl
More file actions
46 lines (39 loc) · 1.11 KB
/
Copy pathSelectionSort.hl
File metadata and controls
46 lines (39 loc) · 1.11 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
/@ Selection Sort Algorithm with User Input @/
fx selection_sort(list<int> numbers) {
int n = numbers.length();
for (i, 0, n ) {
int min_index = i;
for (j, i + 1, n) {
if (numbers[j] < numbers[min_index]) {
min_index = j;
}
}
@ Swap numbers[i] and numbers[min_index]
int temp = numbers[i];
numbers[i] = numbers[min_index];
numbers[min_index] = temp;
}
return numbers;
}
@ Main Program @
fx main() {
@ Input the size of the list
int size = INT(input("Enter the number of elements in the list: "));
list<int> numbers = [];
@ Populate the list with user inputs
for (i, 1, size + 1) {
int element = INT(input("Enter element " + STR(i) + ": "));
numbers.append(element);
}
print("Original List:");
for (i, 0, numbers.length()) {
print(numbers[i]);
}
@ Sort the list using selection sort
list<int> sorted_numbers = selection_sort(numbers);
print("Sorted List:");
for (i, 0, sorted_numbers.length()) {
print(sorted_numbers[i]);
}
}
main();