list.c
/* Sort list in descending order using selection sort */
Sort list in descending order using selection sort
Sort list in descending order using selection sort
void list_sort(list_t *_list, comp_func comp_fn)
{
	if (_list->len < 2) return;
	list_node_t *head = _list->head, *i, *j;
	for (j=head;list_node_get_next(j)!=NULL;j=list_node_get_next(j)) {
		list_node_t *iMax = j;
		for (i = list_node_get_next(j);i!=NULL;i=list_node_get_next(i)) {
			if (comp_fn(list_node_get_data(i),list_node_get_data(iMax))>0) iMax = i;
		}
		if (iMax != j) {
			void *tmp = list_node_get_data(j);
			list_node_set_data(j,list_node_get_data(iMax));
			list_node_set_data(iMax,tmp);
		}
	}
}
void list_sort ( list_t * _list , comp_func comp_fn ) { if ( _list -> len < 2 ) return ; list_node_t * head = _list -> head , * i , * j ; for ( j = head ; list_node_get_next ( j ) != NULL ; j = list_node_get_next ( j ) ) { list_node_t * iMax = j ; for ( i = list_node_get_next ( j ) ; i != NULL ; i = list_node_get_next ( i ) ) { if ( comp_fn ( list_node_get_data ( i ) , list_node_get_data ( iMax ) ) > 0 ) iMax = i ; } if ( iMax != j ) { void * tmp = list_node_get_data ( j ) ; list_node_set_data ( j , list_node_get_data ( iMax ) ) ; list_node_set_data ( iMax , tmp ) ; } } }
Sort list in descending order using selection sort
Sort list in descending order using selection sort
// Find the maximum element in the unsorted sublist of indexes j..n-1