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

Joined
Nov 4, 2022
Messages
2
Reaction score
0
Please help me, if you know how to solve this issue because my code doesn't work :)

//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;
}
}
 
Joined
Nov 4, 2022
Messages
2
Reaction score
0
1667651835916.png


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");
    }
  }
}
 

Ask a Question

Want to reply to this thread or ask your own question?

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.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top