Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as C by aaa ( 6 years ago )
#include <stdio.h>
void print_int_arr (int *print_arr, int len)
{
/*prints all array values with commas between them*/
for (int i = 0; i < len; i++)
{
printf ("%d, ", print_arr[i]);
}
printf ("\n");
}
void place_swap (int *arr, int index)
{
/* swaps two array places, index with index-1 */
int temp;
temp = arr[index - 1];
arr[index - 1] = arr[index];
arr[index] = temp;
}
void gnome_sort (int *arr, int len)
{
int index = 1;
/* sort array */
while (index < len)
{
/* if the two places are in order, or the index have reached zero and
will start checking memory outside of the array, advance the index */
if ((arr[index] >= arr[index - 1]) || (index == 0))
{
index++;
}
/* if the two places are not in order, swap them and go back*/
else
{
place_swap (arr, index);
index --;
}
}
}
void main ()
{
int sort_arr[] =
{ 5, 2, 11, 8, 6, 0, 8, 4, 6, 77, 32, 145, 2, 85, 40, 462, 7, 2, 3, 4, 6, 8, 1, 20, 35, 6, 4, 2, 88, 10};
int len = sizeof (sort_arr) / sizeof(int);
/* print unsorted array */
print_int_arr (sort_arr, len);
/* sort array */
gnome_sort (sort_arr, len);
/* print sorted array */
print_int_arr (sort_arr, len);
}
Revise this Paste