Skip to content

Commit

Permalink
added ascending shell sort
Browse files Browse the repository at this point in the history
  • Loading branch information
GauravWalia19 committed Nov 26, 2024
1 parent ef45efc commit 5f76bf7
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions CodeBase/ShellSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* PROBLEM: Ascending Shell Sort
* AUTHOR: GauravWalia19
**/
import java.util.*;
public class ShellSort
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n; //size of the array
System.out.println("Enter the size of the array");
n = in.nextInt();
int[] intArray = new int[n];
for(int i=0;i<n;i++)
{
intArray[i] = in.nextInt();
}
shellsort(intArray);
//printing array
System.out.println("The sorted array: ");
for(int i=0;i<intArray.length;i++)
{
System.out.print(intArray[i]+" ");
}
System.out.println();
in.close();
}

public static void shellsort(int[] array)
{
for(int gap=array.length/2;gap>0;gap/=2) //using different gap values
{
for(int i=gap;i<array.length;i++)
{
int raw = array[i];
int j=i;
while(j>=gap && array[j-gap]>raw)
{
//shifting
array[j] = array[j-gap];
j=j-gap;
}
array[j] = raw;
}
}
}
}

0 comments on commit 5f76bf7

Please sign in to comment.