-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection-sort.c
More file actions
58 lines (49 loc) · 1.51 KB
/
selection-sort.c
File metadata and controls
58 lines (49 loc) · 1.51 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
57
58
//========================================================================
// selection-sort.c
//========================================================================
// Implementation of the selection sort function
//
//--------------------!!! IMPORTANT NOTE !!!------------------------------
//
// You need to use ece2400_malloc and ece2400_free in your implementation
// in order to get memory usage for eval!
//
//------------------------------------------------------------------------
//
#include <stdio.h>
#include "selection-sort.h"
//------------------------------------------------------------------------
// find_min
//------------------------------------------------------------------------
// find the index of the minimum number in the array.
size_t find_min( int* arr, size_t begin, size_t end )
{
int min_value = arr[begin];
unsigned int min_idx = begin;
for( unsigned int i = begin; i < end; i++ ) {
if ( arr[i] < min_value ) {
min_value = arr[i];
min_idx = i;
}
}
return min_idx;
}
//------------------------------------------------------------------------
// selection_sort
//------------------------------------------------------------------------
// Sorts the array with selection sort
void selection_sort( int* arr, size_t size )
{
int temp = 0;
unsigned int idx = arr[0];
if ( size <= 1 ) {
return;
}
for( unsigned int i = 0; i < size; i++ ) {
idx = find_min( arr, i, size );
temp = arr[i];
arr[i] = arr[idx];
arr[idx] = temp;
}
return;
}