generated from GauravWalia19/mernboilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef45efc
commit 5f76bf7
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |