-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursive selection sort
More file actions
56 lines (45 loc) · 1.44 KB
/
Recursive selection sort
File metadata and controls
56 lines (45 loc) · 1.44 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
#include <stdio.h>
// Function to find the index of the minimum element in the array
int findMinIndex(int arr[], int start, int end) {
int minIndex = start;
for (int i = start + 1; i <= end; i++) {
if (arr[i] < arr[minIndex]) {
minIndex = i;
}
}
return minIndex;
}
// Recursive function to perform selection sort
void recursiveSelectionSort(int arr[], int start, int end) {
if (start >= end) {
return; // Base case: when start index is greater than or equal to end index
}
// Find the index of the minimum element in the remaining unsorted portion
int minIndex = findMinIndex(arr, start, end);
// Swap the found minimum element with the first element of the unsorted portion
if (minIndex != start) {
int temp = arr[start];
arr[start] = arr[minIndex];
arr[minIndex] = temp;
}
// Recursively sort the remaining unsorted portion
recursiveSelectionSort(arr, start + 1, end);
}
// Function to print the array
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Original array:\n");
printArray(arr, size);
// Perform recursive selection sort
recursiveSelectionSort(arr, 0, size - 1);
printf("Sorted array:\n");
printArray(arr, size);
return 0;
}