-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdoubleSelectionSort.c
More file actions
31 lines (24 loc) · 824 Bytes
/
doubleSelectionSort.c
File metadata and controls
31 lines (24 loc) · 824 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
#include "selection.h"
// Worst case performance O(n**2) comparisons and O(n) swaps
// Best case performance O(n**2) comparisons and O(n) swaps
// Average performance O(n**2) comparisons and O(n) swaps
//(n**2 - n)/4 comparisons
void doubleSelectionSort(long int *array, int length){
long int *i = array, *j = array + length - 1, *min = i, *max = j, aux;
for(; i <= j; j--, i++, min = i, max = j){
for(long int *k = i; k <= j; k++){
if(*k > *max) // Find the max element in the range
max = k;
if(*k < *min) // Find the min element in the range
min = k;
}
if(max == i) // Check if has conflict with the index of the left
max = min;
aux = *i;
*i = *min;
*min = aux;
aux = *j;
*j = *max;
*max = aux;
}
}