-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.c
More file actions
39 lines (39 loc) · 737 Bytes
/
Copy pathSelectionSort.c
File metadata and controls
39 lines (39 loc) · 737 Bytes
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
#include <stdio.h>
void printArray(int *A, int n)
{
int i;
for (int i = 0; i < n; i++)
{
printf("%d ", A[i]);
}
printf("\n");
}
void selectionSort(int *A, int n)
{
int indexOfMin;
int temp;
for (int i = 0; i < n - 1; i++)
{
indexOfMin = i;
for (int j = i + 1; j < n; j++)
{
if (A[j] < A[indexOfMin])
{
indexOfMin = j;
}
}
temp = A[i];
A[i] = A[indexOfMin];
A[indexOfMin] = temp;
printf("After pass %d:- ", i+1);
printArray(A, n);
}
}
int main()
{
int A[] = {3, 5, 2, 12, 13};
int n = 5;
printArray(A, n);
selectionSort(A, n);
printArray(A, n);
}