Problem: Gnome Sort After getting to know your roommate better, you\'ve discover
ID: 3689281 • Letter: P
Question
Problem: Gnome Sort
After getting to know your roommate better, you've discovered a rather strange secret about her family: they own the largest collections of gnomes in North America!!! The reason this came up is that recently some neighborhood hoodlums moved around all the gnomes in your roommates' parents' yard. Her parents desperately want to get the gnomes back into their proper numerical order. Luckily, you just learned about sorting in your programming course and your roommate has decided that it's only fitting that you write a program implementing Gnome sort to help her!
She kindly pointed you to the wiki page on Gnome sort:
https://en.wikipedia.org/wiki/Gnome_sort
Complete the function below so that it performs a Gnome sort on the input array of integers. The function prototype is given to you below and you can download the program to add your function to:
// Pre-condition: n is the size of the array gnomes.
// Post-condition: gnomes will be sorted in numerical order, from
// smallest to largest.
void gnomeSort(int gnomes[], int n);
The given code scaffold will contain all necessary testing code.
(Here is the code)
gnomesort-scaffold.c
(In the fill in code part please use this)
swap(&a[pos],&a[pos-1]);
(wiki page on Gnome sort)
procedure gnomeSort(a[]):
Explanation / Answer
void gnomesort(int length, int ar[])
{
int i = 0;
while (i < length)
{
if (i == 0 || ar[i-1] <= ar[i])
{
i++;
} else { int temp = ar[i]; ar[i] = ar[i-1]; ar[--i] = temp;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.