# How to sort second half of array in ascending order using Shell Sort?

#### Nightmare

//Code
for(step=n; step>n/2; step/=2){
for(i=step; i<n; i++){
int temp = arr;
for(j=i; j>=step && arr[j-step]>temp; j-=step){
arr[j] = arr[j-step];
}
arr[j] = temp;
}
}

#### John Joe

What is your expected output? Any errors?

#### Nightmare

I sort first half array in descending order, but I want to sort second half in ascending order using Shell Sort, but my code doesn't work.

My full code class ShellSort:

Code:
``````class ShellSort{
public void sortShell(int arr[]){
int n = arr.length;
int i,j,step;

for(step=n/2; step>0; step/=2){
for(i=step; i<n/2; i++){
int temp = arr[i];
for(j=i; j>=step && arr[j-step]<temp; j-=step){
arr[j] = arr[j-step];
}
arr[j] = temp;
}
}

for(step=n; step>n/2; step/=2){
for(i=step; i<n; i++){
int temp = arr[i];
for(j=i; j>=step && arr[j-step]>temp; j-=step){
arr[j] = arr[j-step];
}
arr[j] = temp;
}
}
}

public void printShellSort(int arr[]){
int n = arr.length;
System.out.println("\nShell Sort: ");
for(int i=0; i<n; i++){
System.out.print(arr[i]+"\t");
}
}
}``````

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

### Members online

No members online now.