Created
October 29, 2017 16:48
-
-
Save ranguli/c6d13f10b52ba560cab9990ea9015305 to your computer and use it in GitHub Desktop.
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
import java.util.Random; | |
public class ShellArray { | |
public static void main (String args[]) { | |
int[] data = new int[100]; | |
Random rng = new Random(); | |
//Fill the array | |
for (int i = 0; i < data.length; i++) { | |
data[i] = rng.nextInt(200); | |
} | |
System.out.println("Unsorted array:"); | |
for (int i = 0; i < data.length; i++) { | |
System.out.print(data[i] + ", "); | |
} | |
shellSort(data); | |
System.out.println("\n" + "Shell sorted array:"); | |
for (int i = 0; i < data.length; i++) { | |
System.out.print(data[i] + ", "); | |
} | |
} | |
public static void shellSort(int data[]) { | |
int tmp; | |
int gap = data.length / 2; | |
boolean swapflag = true; | |
do { | |
swapflag = true; | |
do { | |
swapflag = false; | |
for (int s = 0; s < data.length - gap; s++) { | |
if (data[s] > data[s+gap]) { | |
tmp = data[s]; | |
data[s] = data[s+gap]; | |
data[s+gap] = tmp; | |
swapflag = true; | |
}//end-if | |
}//end-for | |
} while(swapflag == true); //end-do | |
gap = gap / 2; | |
} while(gap <= 0); //end-do | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment