-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTri_Selection.c
More file actions
41 lines (36 loc) · 1.03 KB
/
Tri_Selection.c
File metadata and controls
41 lines (36 loc) · 1.03 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
#include <stdio.h>
#include <stdlib.h>
/**
* Sort array by selection
*
*
* int array[] :: array to be sorted
* int len_array :: array size
*
* return void
**/
void TriSelection( int array[], int len_array )
{
/* condition to stop recursion */
//if( len_array <= 1 ){ return; }
int i, j, max_index; // declaring counters:: i for 1st loop and j for 2nd
for (i = 0; i < len_array - 1; i++)
{
max_index = 0; // to start lets say 1st value is the highest
/* find maximum value in unsorted array */
for (j = 0; j < len_array - i - 1; j++)// i stands for the total of value sorted
{
if (array[j] > array[max_index])
{
max_index = j;
}
swap(array, len_array - 1, max_index); // move highest value at the end of the unsorted array
}
}
/*
* array gets smaller as last values are sorted
* recursion not working and replaced by a for loop
* problem encounter :: infinite recursive that i can't fix
*/
//TriSelection(array, len_array-1);
}