c - Sorting algorithm with 10 elements -
i have make program asks user input array of 10
elements numbers 0
9
.
then array sent function. function sort each number in array follows:
i don't know how reposition numbers.
here implementation of joel gregory's comment:
this function creates unedited array, copies original's contents it, loops first index last. used basis of checking matches indexes.
in each iteration, function searches unedited array if of it's elements matches index number. if yes, replaces initial value index value. else, 0 assigned. , on until last index.
void swap(int *original, int max_elements) { int index = 0, matcher = 0, unedited[max_elements]; // copies original unedited memcpy(unedited, original, max_elements * sizeof(int)); (--index; ++index < max_elements; ) { // searches original array matches current index // loops until finds match or reaches maximum number of elements (matcher = 0; unedited[matcher] != index && ++matcher < max_elements;); // if there match, initial value changed it's index value, else 0 new value. original[index] = (matcher != max_elements) ? index : 0; } }
output:
original: 1 1 7 8 2 8 1 6 3 2 sorted: 0 1 2 3 0 0 6 7 8 0
Comments
Post a Comment