Hi. guys.
I am trying to implement shell sort which is, to my understanding, just a bubble sort with gaps. This is what I wrote :
output of random array:Code:int shellSort(int *arr, int n){ int i=0, j; printf("\nstarting shell sort", n); j=(n%2)?n-2:n-3; // Make shore j is odd and less then n. while(j>0){ while(n-(i+j)){ // Like buble sort but with gaps. if(arr[i]>arr[i+j]){ arr[i+j]+=arr[i]; // Swap the two members of the array arr[i]=arr[i+j]-arr[i];// if nesesry. arr[i+j]=arr[i+j]-arr[i]; } i++; } j-=2; // Continue im steps of 2 i=0; } return 0; }
andsorting array:
1, 36, 27, 30, 15, 29, 32, 42, 4, 9, 20, 18, 21, 20, 46, 34,
starting shell sort
1, 4, 9, 15, 20, 18, 21, 27, 20, 30, 29, 32, 34, 36, 42, 46,
as you can see I am not quite there yet, it is almost sorted but not completely.sorting array:
33, 2, 34, 22, 3, 18, 12,
starting shell sort
2, 18, 12, 3, 33, 22, 34,
What am I doing wrong? maybe someone can explain shell sort better to me?
thanks