^^
I'm new to c++ and wish to learn this subject and im trying to practice with my c++. I was given the task that requires input from the user and sorting it out in descending order. I thought the above code should work but it doesnt sort out the numbers. Could anyone give me a clue to where I've gone wrong?
You are entering the realm of sorting algorithms. There are plenty. Its actually an entire subject of its own. Since you are a beginner start with the "insertion sort algorithm" AVOID BUBBLESORT.
Do you know anything about for loops, functions and arrays? You have to know about those to understand the code for sorting numbers in the order you want
#include <stdio.h>
/* shellsort: sort v[0]...v[n-1] into increasing order */
void shellsort(int v[], int n)
{
int gap, i, j, temp;
for (gap = n/2; gap > 0; gap /= 2)
for (i = gap; i < n; i++)
for (j=i-gap; j>=0 && v[j]>v[j+gap]; j-=gap) {
temp = v[j];
v[j] = v[j+gap];
v[j+gap] = temp;
}
}
int main (){
int array[10],i,j;
for (i=0;i<=9;i++)
scanf("%d",&array[i]);
shellsort(array, 10);
for (j=0;j<=9;j++)
printf("%d",array[j]);
}
There you go. Enjoy! And yes, its in C not C++
This should really only be attempted if you are very comfortable with for loops and basic shell sort algorithm. Otherwise, dont even try. Familiarize yourself with these first.
I have a basic understanding of loops, functions and arrays but i havent mastered them fully yet though. But thank you for telling me these stuffs. Im still trying to get a hang of it :)
Anyways the above code doesnt work sir. It just accepts the input numbers but doesnt print the numbers out in descending order. :(