Plotting of graph - java

Given an array of integers, I need to display a graph representing each integer.
For example, given an array {2,4,6,-3,-4,5,-1,-2,3}, the output on the console should be like:
*
* *
* * *
* * * *
* * * * *
* * * * *
* * * *
* * *
* *
*
I took the matrix approach, but so far I have been only able to achieve the horizontal version of the desired graph (90° clockwise rotated version of the desired graph, to be more precise).
How can I rotate my graph 90° in an anticlockwise direction to get the desired result? I tried transposing my result matrix but then the graph becomes the exact inverse of the desired result.
Here is the code,
public static char[][] plot(int[] array) {
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int i = 0; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
if (array[i] < min) {
min = array[i];
}
}
if (min < 0) {
min *= (-1);
}
char[][] tempArray = new char[array.length][max + min + 1];
for (int i = 0; i < tempArray.length; i++) {
if (array[i] > 0) {
for (int j = (min + 1); j <= min + array[i]; j++) {
tempArray[i][j] = '*';
}
System.out.println();
} else {
for (int j = min - 1; j >= min + array[i]; j--) {
tempArray[i][j] = '*';
}
}
}
return tempArray;
}
public static void main(String...s){
int[] arr = {2,4,6,-3,-4,5,-1,-2,3};
System.out.println(Arrays.deepToString(plot(arr)));
}
Also, I'm not able to nicely format the output matrix (I'm still looking at what is going on there), so I used Arrays.deeptoString().

I make own plotter (plot0) which write directly to the output stream char by char, line by line:
private static void plot0(final int... numbers) {
final int cols = numbers.length;
final IntSummaryStatistics stat = Arrays.stream(numbers).summaryStatistics();
final int max = stat.getMax();
final int min = stat.getMin();
final int rows;
if (min < 0) {
rows = Math.max(0, max) + ~min + 2;
} else {
rows = max;
}
for (int i = 0; i < rows; i++) {
final int val = Math.max(0, max) - i;
if (0 == val) {
continue; // do not plot zero
}
for (int j = 0; j < cols; j++) {
final int num = numbers[j];
System.out.print(
0 < num && 0 < val && num >= val
|| 0 > num && 0 > val && num <= val
? '*'
: ' ');
}
System.out.println();
}
}
I made some small changes in your code ( rename to plot1), but basically the original thinking works.
change Arrays.deepToString(tempArray[i]) to new String(tempArray[i])
two dimensional array is not printable, use a line of char instead
fill zero values with ' ' in Arrays.fill(tempArray[i], ' ');
missing skip 0 value from print, the baseline is visible
end of line after fill all '*' values, not before
the '*' values override the ' ', so you cannot write directly to the output.
private static void plot1(final int... array) {
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (final int element : array) {
if (element > max) {
max = element;
}
if (element < min) {
min = element;
}
}
if (min < 0) {
min *= -1;
}
final char[][] tempArray = new char[array.length][max + min + 1];
for (int i = 0; i < tempArray.length; i++) {
Arrays.fill(tempArray[i], ' ');
if (array[i] > 0) {
for (int j = min + 1; j <= min + array[i]; j++) {
tempArray[i][j] = '*';
}
} else if (array[i] < 0) {
for (int j = min - 1; j >= min + array[i]; j--) {
tempArray[i][j] = '*';
}
}
System.out.println(new String(tempArray[i]));
}
}

Related

I am trying to create a diamond shape in Java [duplicate]

This question already has answers here:
How to make a diamond using nested for loops
(19 answers)
Closed 2 years ago.
I currently have this code which allows me to create the left half of the diamond shape. Is there any way to mirror it to complete the right half. Or a completely different way of creating this shape.
public class diamond {
int size = 0; //sets a starting value for size
static int length = 9;
public static void main(String[] args) {
//creates half of our diamond shape :(
for (int i = 0; i < length; i++) {
int j = length - 1 - i;
for (int k = length / 2; k < length; k++) {
if (k == i || k == j || k == length + 7 + i - j)
System.out.print("X");
else
System.out.print(" ");
}
System.out.println("");
}
}
}
Here is another alternative that takes a slightly different approach.
Since certain widths or lines can't be specified exactly and always have a single middle line, this just prompts for a number.
It calculates spacing and adjusts the field of asterisks within two loops. The first does the top and middle line. The second just does the bottom portion.
String symb = "*";
Scanner input = new Scanner(System.in);
int max = 0;
while (max <= 1) {
System.out.print("Enter a positive no > 1: ");
max = input.nextInt();
}
System.out.println("Width and height are " + (2*max -3)+"\n");
for (int i = 1; i < max; i++) {
System.out.println(" ".repeat(max - i) + symb);
symb += "**";
}
for (int i = 2; i < max; i++) {
System.out
.println(" ".repeat(i) + symb.substring(2 * i));
}
When prompted for and provided an input of 5, this prints.
Enter a positive no > 1: 5
Width and height are 7
*
***
*****
*******
*****
***
*
You can modify this to produce the type of diamond you want.
Below code prints the diamond pattern as per your diagram. This program allows the user to input the no of rows for the diamond and also allows the user to select any shape you want to create your diamond with.
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
int n, i, j, space = 1;
System.out.print("Enter the number of rows: ");
Scanner s = new Scanner(System.in);
n = s.nextInt();
System.out.print("Enter Symbol : ");
char c = s.next().charAt(0);
drawDiamond(n,c);
}
static void drawDiamond(int n, char c) {
int i, j, space, k = 0;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n - i; j++) {
System.out.print(" ");
}
while (k != (2 * i - 1)) {
if (k == 0 || k == 2 * i - 2)
System.out.print(c);
else
System.out.print(" ");
k++;
}
k = 0;
System.out.println();
}
n--;
for (i = n; i >= 1; i--) {
for (j = 0; j <= n - i; j++) {
System.out.print(" ");
}
k = 0;
while (k != (2 * i - 1)) {
if (k == 0 || k == 2 * i - 2)
System.out.print(c);
else
System.out.print(" ");
k++;
}
System.out.println();
}
}
}
Sample Output :
Enter the number of rows: 4
Enter Symbol : *
*
* *
* *
* *
* *
* *
*
This Code draws a diamond like your picture but only works with odd numbers.
class Diamond{
int size = 0;
static int length = 9;
public static void main(String[] args) {
int outerSpace = length / 2; //number left to the first x
int count = outerSpace; //same number used to find the mid
int up = 0; //counter for the space between the xx
for (int i = 0; i < length; i++) {
if (i < count) { //this code gets used till you reach the middle
for (int j = 0; j < outerSpace; j++) { //prints space left of x
System.out.print(" ");
}
System.out.print("X");
for (int k = 0; k < up; k++) {
System.out.print(" "); //prints space between the x
}
System.out.println("X");
outerSpace--;
up += 2;
}else{ //code after middle
for (int j = 0; j < outerSpace; j++) {
System.out.print(" ");
}
System.out.print("X");
for (int k = 0; k < up; k++) {
System.out.print(" ");
}
System.out.println("X");
outerSpace++;
up -= 2;
}
}
}
}
To get the exact same design, The input length has to be even number.
public class Main {
private static int legnth = 10;
private static int x = legnth /2;
private static int y = x + 1;
public static void main(String[] args) {
for(int i=1; i <= legnth/2; i++) {
for(int j=1; j <= legnth; j++) {
if(j==x || j==y) {
System.out.print("*");
}else {
System.out.print(" ");
}
}
x--;
y++;
System.out.println("");
}
x+=2;
y-=2;
for(int i=1; i < legnth/2; i++) {
for(int j=1; j < legnth; j++) {
if(j==x || j==y) {
System.out.print("*");
}else {
System.out.print(" ");
}
}
x++;
y--;
System.out.println("");
}
}
}
output
**
* *
* *
* *
* *
* *
* *
* *
**
Here is another solution where you can create a string array for one fourth of the diamond and later manipulate that array to print it into diamond. It can take even or odd both to create a diamond like shape.
static int length=9;
static int n=length/2;
public static void main(String[] args) {
String []arr = new String[n];
for (int i = 0; i < n; i++) {
String space="";
int j = n - i;
for (int k = 0; k < j; k++) {
space=space+" ";
}
arr[i]=space+"X";
}
// To print upper half of diamond
for(int i=0;i<n;i++) {
String upper= arr[i] + arr[n -1 -i].substring(0,arr[n-1-i].length()-2)
+ arr[n -1 -i].substring(1,arr[n -1 -i].length());
System.out.println(upper);
}
// To print middle of the diamond
System.out.println("X" + arr[0].substring(0,arr[0].length()-1)
+arr[0].substring(0,arr[0].length()-1) + "X");
// To print down half of the diamond
for(int i=0;i<n;i++) {
String down= arr[n -1 -i]+arr[i].substring(0,arr[i].length()-2)
+arr[i].substring(1,arr[i].length());
System.out.println(down);
}
}
Here is another solution. It will work for any valid odd integer value given for length.
void createDiamond(int length) {
int n = length / 2;
for (int i = 0; i < length; i++) {
for (int j = 0; j <= length; j++) {
if ((i >= 0 && i <= n && (j == n - i || j == n + 1 + i))
|| (i > n && i < length && (j == i - n || j == length + n - i)))
System.out.print("X");
else
System.out.print(" ");
}
System.out.println();
}
}
Output:
createDiamond(9);
XX
X X
X X
X X
X X
X X
X X
X X
XX

Java: Sudoku- increase number of empty spaces causes my code to throw an exception

I'm trying to make a Sudoku game for my project but if i increase the number of empty spaces in the Sudoku Grid the code just throws an exception arrayoutofbounds but can't figure out where it's coming from. k is the number of empty spaces in the grid.
I haven't tried anything because can't figure out what can be done at this kind of problem
Here is the code:
package sudoku.puzzle;
import java.util.*;
public class SudokuPuzzle {
int[] mat[];
int N; // number of columns/rows.
int SRN; // square root of N
int K; // No. Of missing digits
// Constructor
SudokuPuzzle(int N, int K) {
this.N = N;
this.K = K;
// Compute square root of N
Double SRNd = Math.sqrt(N);
SRN = SRNd.intValue();
mat = new int[N][N];
}
// Driver code
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Select Level Of Difficulty \n 1.Easy\n 2.Medium\n 3.Hard");
String Choice = in .next(); in .close();
if ("1".equals(Choice) || "Easy".equals(Choice) || "easy".equals(Choice) || "e".equals(Choice) || "E".equals(Choice)) {
int N = 9, K = 40;
SudokuPuzzle sudoku = new SudokuPuzzle(N, K);
sudoku.fillValues();
sudoku.printSudoku();
}
if ("2".equals(Choice) || "Medium".equals(Choice) || "medium".equals(Choice) || "m".equals(Choice) || "M".equals(Choice)) {
int N = 9, K = 60;
SudokuPuzzle sudoku = new SudokuPuzzle(N, K);
sudoku.fillValues();
sudoku.printSudoku();
}
if ("3".equals(Choice) || "Hard".equals(Choice) || "hard".equals(Choice) || "h".equals(Choice) || "H".equals(Choice)) {
int N = 9, K = 72;
SudokuPuzzle sudoku = new SudokuPuzzle(N, K);
sudoku.fillValues();
sudoku.printSudoku();
}
}
// Sudoku Generator
public void fillValues() {
// Fill the diagonal of SRN x SRN matrices
fillDiagonal();
// Fill remaining blocks
fillRemaining(0, SRN);
// Remove Randomly K digits to make game
removeKDigits();
}
// Fill the diagonal SRN number of SRN x SRN matrices
void fillDiagonal() {
for (int i = 0; i < N; i = i + SRN)
// for diagonal box, start coordinates->i==j
fillBox(i, i);
}
// Returns false if given 3 x 3 block contains num.
boolean unUsedInBox(int rowStart, int colStart, int num) {
for (int i = 0; i < SRN; i++)
for (int j = 0; j < SRN; j++)
if (mat[rowStart + i][colStart + j] == num)
return false;
return true;
}
// Fill a 3 x 3 matrix.
void fillBox(int row, int col) {
int num;
for (int i = 0; i < SRN; i++) {
for (int j = 0; j < SRN; j++) {
do {
num = randomGenerator(N);
}
while (!unUsedInBox(row, col, num));
mat[row + i][col + j] = num;
}
}
}
// Random generator
int randomGenerator(int num) {
return (int) Math.floor((Math.random() * num + 1));
}
// Check if safe to put in cell
boolean CheckIfSafe(int i, int j, int num) {
return (unUsedInRow(i, num) &&
unUsedInCol(j, num) &&
unUsedInBox(i - i % SRN, j - j % SRN, num));
}
// check in the row for existence
boolean unUsedInRow(int i, int num) {
for (int j = 0; j < N; j++)
if (mat[i][j] == num)
return false;
return true;
}
// check in the row for existence
boolean unUsedInCol(int j, int num) {
for (int i = 0; i < N; i++)
if (mat[i][j] == num)
return false;
return true;
}
// A recursive function to fill remaining
// matrix
boolean fillRemaining(int i, int j) {
// System.out.println(i+" "+j);
if (j >= N && i < N - 1) {
i = i + 1;
j = 0;
}
if (i >= N && j >= N)
return true;
if (i < SRN) {
if (j < SRN)
j = SRN;
} else if (i < N - SRN) {
if (j == (int)(i / SRN) * SRN)
j = j + SRN;
} else {
if (j == N - SRN) {
i = i + 1;
j = 0;
if (i >= N)
return true;
}
}
for (int num = 1; num <= N; num++) {
if (CheckIfSafe(i, j, num)) {
mat[i][j] = num;
if (fillRemaining(i, j + 1))
return true;
mat[i][j] = 0;
}
}
return false;
}
// Remove the K no. of digits to
// complete game
public void removeKDigits() {
int count = K;
while (count != 0) {
int cellId = randomGenerator(N * N);
// System.out.println(cellId);
// extract coordinates i and j
int i = (cellId / N);
int j = cellId % 9;
if (j != 0)
j = j - 1;
// System.out.println(i+" "+j);
if (mat[i][j] != 0) {
count--;
mat[i][j] = 0;
}
}
}
// Print sudoku
public void printSudoku() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
System.out.print(mat[i][j] + " ");
System.out.println();
}
System.out.println();
}
}
What you got is probably a ArrayIndexOutOfBoundsException. That means at some point you try to access a field of an array outside its boundaries.
But I can´t see where K could be responsible for that. Can you provide more information about the error? E.g. at which value you get it or in which line.
EDIT: The variable i int the removeKDigits() function exceeds the boundaries of the array if the random generator spits out the value 81.

Max sum in a 2d array Java

I have a 2d array (a matrix) and I need to find the max sum that can be collected by starting at any position and going down right or down left until I reach an end. I must give an iterative solution.
This is my code
static int maxValue(double[][] field, int posR, int posC) {
int r = field.length;
int c = field[0].length;
int sum = 0;
double[][] temp = new double[r][c];
for (int i = posR; i < r; i++) {
for (int j = posC; j < c; j++) {
if (i == posR && j == posC) {
temp[i][j] = field[posR][posC];
posR++; posC++;
} else if (i == field.length-1) {
temp[i][j] = field[i][j];
break;
} else if (j == field.length-1) {
temp[i][j] = field[i][j];
break;
} else {
temp[i][j] = Math.max(field[i+1][j-1], field[i+1][j+1]);
}
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
sum += temp[i][j];
}
}
return sum;
}
}
Here is an idea for an iterative solution: You can slide down a row and at the bottom you will find the max. Sounds crazy? Let me explain with code:
public static double maxZicZacSum(double[][] matrix) {
double[] row = matrix[0]; // assign first row
int n = row.length;
for (int i = 1; i < matrix.length; i++) { // for each row, except the first
double[] nextRow = new double[n];
// special cases (left and right edge)
nextRow[0] = row[1] <= 0 ? matrix[i][0] : row[1] + matrix[i][0];
nextRow[n - 1] = row[n - 2] <= 0 ? matrix[i][n - 1] : row[n - 2] + matrix[i][n - 1];
for (int j = 1; j < n - 1; j++) { // for each column except the edges
double d = Math.max(row[j - 1], row[j + 1]); // which cell above is better?
// if d is > 0, then the sum is also better, otherwise use (i,j) as new start
nextRow[j] = d <= 0 ? matrix[i][j] : d + matrix[i][j];
}
row = nextRow; // finally assign nextRow to row for the next iteration
}
// the highest value in row is now the max sum
double max = row[0];
for (int i = 1; i < n; i++)
if (row[i] > max)
max = row[i];
return max;
}
Generally speaking, this is a dynamic programming problem.
The logic is (with row 0 being the top left)
F(row, col) = valueAt(row, col) + max(F(row + 1, col - 1), F(row + 1, col + 1)
Now, you'll notice this is a recursive definition, so no for loops are really needed.
So, in slight pseudocode
int maxValue(double[][] arr, int row, int col) {
if (outOfBounds) return 0;
int value = arr[row][col];
int leftDiag = maxValue(arr, row +1,col - 1);
int rightDiag = maxValue(arr, row + 1, col + 1);
return value + Math.max(leftDiag, rightDiag);
}
Starting at any position, you should be able to call the method and it'll recursively sum values and return the max path.

Lucene: - indexing and finding unique terms

I have written a code in lucene, which firsts indexes xml documents, and finds the number of unique terms in the index.
Say there are n number (no.) of unique terms.
I want to generate a matrix of dimensions nXn, where
m[i][j] = (co_occurrence value of terms (i, j))/ (occurrence value of term i)
co_occurence of terms (i, j) = no. of documents in which ith term and jth terms, both are occurring
occurence of term j is the no. of documents in which the term j is occurring.
My code is working fine. But its not efficient. for large no. of files, where no. of terms are more than 2000, its taking more than 10 minutes.
here is my code for finding co_occurence -
int cooccurrence(IndexReader reader, String term_one, String term_two) throws IOException {
int common_doc_no = 0, finaldocno_one = 0, finaldocno_two = 0;
int termdocid_one[] = new int[6000];
int termdocid_two[] = new int[6000];
int first_docids[] = new int[6000];
int second_docids[] = new int[6000];
int k = 0;
for (java.util.Iterator<String> it = reader.getFieldNames(
FieldOption.ALL).iterator(); it.hasNext();) {
String fieldname = (String) it.next();
TermDocs t = reader.termDocs(new Term(fieldname, term_one));
while (t.next()) {
int x = t.doc();
if (termdocid_one[x] != 1) {
finaldocno_one++;
first_docids[k] = x;
k++;
}
termdocid_one[x] = 1;
}
}
/*
* System.out.println("value of finaldoc_one - " + finaldocno_one); for
* (int i = 0; i < finaldocno_one; i++) { System.out.println("" +
* first_docids[i]); }
*/
k = 0;
for (java.util.Iterator<String> it = reader.getFieldNames(
FieldOption.ALL).iterator(); it.hasNext();) {
String fieldname = (String) it.next();
TermDocs t = reader.termDocs(new Term(fieldname, term_two));
while (t.next()) {
int x = t.doc();
if (termdocid_two[x] != 1) {
finaldocno_two++;
second_docids[k] = x;
k++;
}
termdocid_two[x] = 1;
}
}
/*
* System.out.println("value of finaldoc_two - " + finaldocno_two);
*
* for (int i = 0; i < finaldocno_two; i++) { System.out.println("" +
* second_docids[i]); }
*/
int max;
int search = 0;
if (finaldocno_one > finaldocno_two) {
max = finaldocno_one;
search = 1;
} else {
max = finaldocno_two;
search = 2;
}
if (search == 1) {
for (int i = 0; i < max; i++) {
if (termdocid_two[first_docids[i]] == 1)
common_doc_no++;
}
} else if (search == 2) {
for (int i = 0; i < max; i++) {
if (termdocid_one[second_docids[i]] == 1)
common_doc_no++;
}
}
return common_doc_no;
}
code for calculation of knowledge matrix: -
void knowledge_matrix(double matrix[][], IndexReader reader, double avg_matrix[][]) throws IOException {
ArrayList<String> unique_terms_array = new ArrayList<>();
int totallength = unique_term_count(reader, unique_terms_array);
int co_occur_matrix[][] = new int[totallength + 3][totallength + 3];
double rowsum = 0;
for (int i = 1; i <= totallength; i++) {
rowsum = 0;
for (int j = 1; j <= totallength; j++) {
int co_occurence;
int occurence = docno_single_term(reader,
unique_terms_array.get(j - 1));
if (i > j) {
co_occurence = co_occur_matrix[i][j];
} else {
co_occurence = cooccurrence(reader,
unique_terms_array.get(i - 1),
unique_terms_array.get(j - 1));
co_occur_matrix[i][j] = co_occurence;
co_occur_matrix[j][i] = co_occurence;
}
matrix[i][j] = (float) co_occurence / (float) occurence;
rowsum += matrix[i][j];
if (i > 1)
{
avg_matrix[i - 1][j] = matrix[i - 1][j] - matrix[i - 1][0];
}
}
matrix[i][0] = rowsum / totallength;
}
for (int j = 1; j <= totallength; j++) {
avg_matrix[totallength][j] = matrix[totallength][j]
- matrix[totallength][0];
}
}
Please anyone suggest me any efficient method to implement it.
I think you can put the find process of term_one and term_two in one for loop. And you can use two hashsets to save the docid that you have found. And then use termOneSet.retainAll(termTwoSet) to get the doc which have both term_one and term_two.

Shell sort Java example

Can anyone give me example about shell sort? I'm a new person in here who must learn about shell sort, but first I must find a Java shell sort example. I found one example in Google but it's too difficult.
Here, this code is very simple :
/**
* Shellsort, using Shell’s (poor) increments.
* #param a an array of Comparable items.
*/
public static <T extends Comparable<? super T>>
void shellsort( T [ ] a )
{
int j;
for( int gap = a.length / 2; gap > 0; gap /= 2 )
{
for( int i = gap; i < a.length; i++ )
{
T tmp = a[ i ];
for( j = i; j >= gap && tmp.compareTo( a[ j - gap ] ) < 0; j -= gap )
{
a[ j ] = a[ j - gap ];
}
a[ j ] = tmp;
}
}
}
I stole it from a book called Data Structures and Algorithm Analysis in Java. It is very good book easy to understand. I advise you to read it.
May be, this java code will help you.
public class ShellSort {
private long[] data;
private int len;
public ShellSort(int max) {
data = new long[max];
len = 0;
}
public void insert(long value){
data[len] = value;
len++;
}
public void display() {
System.out.print("Data:");
for (int j = 0; j < len; j++)
System.out.print(data[j] + " ");
System.out.println("");
}
public void shellSort() {
int inner, outer;
long temp;
//find initial value of h
int h = 1;
while (h <= len / 3)
h = h * 3 + 1; // (1, 4, 13, 40, 121, ...)
while (h > 0) // decreasing h, until h=1
{
// h-sort the file
for (outer = h; outer < len; outer++) {
temp = data[outer];
inner = outer;
// one subpass (eg 0, 4, 8)
while (inner > h - 1 && data[inner - h] >= temp) {
data[inner] = data[inner - h];
inner -= h;
}
data[inner] = temp;
}
h = (h - 1) / 3; // decrease h
}
}
public static void main(String[] args) {
int maxSize = 10;
ShellSort arr = new ShellSort(maxSize);
for (int j = 0; j < maxSize; j++) {
long n = (int) (java.lang.Math.random() * 99);
arr.insert(n);
}
arr.display();
arr.shellSort();
arr.display();
}
}
Shell sort improves insertion sort by comparing elements separated by a gap of several positions.
This lets an element take "bigger steps" toward its expected position. Multiple passes over the data are taken with smaller and smaller gap sizes. The last step of Shell sort is a plain insertion sort, but by then, the array of data is guaranteed to be almost sorted.
This code might help you in understanding the logic better.
package Sorts;
public class ShellSort extends Sorter{
#Override
public <T extends Comparable<? super T>> void sort(T[] a) {
int h = 1;
while((h*3+1) < a.length)
h = 3*h+1;
while(h > 0){
for(int i = h-1; i < a.length; i++){
T s = a[i];
int j = i;
for(j = i; (j>=h) && (a[j-h].compareTo(s) > 0); j-=h)
a[j] = a[j-h];
a[j] = s;
}
h /= 3;
}
}
}
Here is a visualization of shell sort for a python implementation:
def exch(a,i,j):
t = a[i]
a[i] = a[j]
a[j] = t
def shellsort(string):
print string
a = list(string)
N = len(a)
h = 1
i = 0
j = 0
k = 0
#determine starting stride length
while ( h < N/3 ):
h = 3*h + 1
print "STRIDE LENGTH: " + str(h)
while (h >=1):
i = h
while i < N:
j = i
k = j - h
while j >= h and a[j] < a[j-h]:
k = j - h
exch(a,j,k)
j -= h
i += 1
h = h/3
print "STRIDE LENGTH: " + str(h)
print ''.join(a)·
if __name__ == '__main__':
shellsort("astringtosortwithshellsort")
Here's an example:
public static void shellsort( Comparable [ ] a )
{
for( int gap = a.length / 2; gap > 0;
gap = gap == 2 ? 1 : (int) ( gap / 2.2 ) )
for( int i = gap; i < a.length; i++ )
{
Comparable tmp = a[ i ];
int j = i;
for( ; j >= gap && tmp.compareTo( a[ j - gap ] ) < 0; j -= gap )
a[ j ] = a[ j - gap ];
a[ j ] = tmp;
}
}
I find the easiest way to understand shell sort is to break it down into segments:
private static void shellsort() {
int[] theArray = {44,5,33,22,765,43,53,12,57,97};
//first section gets the Knuth's interval sequence (very efficient)
int h=1;
while(h<= theArray.length/3){
h = 3*h + 1; //h is equal to highest sequence of h<=length/3 (1,4,13,40...)
}
//next section
while(h>0){ //for array of length 10, h=4
//similar to insertion sort below
for(int i=0; i<theArray.length; i++){
int temp = theArray[i];
int j;
for(j=i; j>h-1 && theArray[j-h] >= temp; j=j-h){
a[j] = a[j-h];
}
a[j] = temp;
}
h = (h-1)/3;
}
}
Output: 5, 12, 22, 33, 43, 44, 53, 57, 97, 765
Classic primitive type implementation:
package math;
import java.util.Arrays;
public class Sorter{
public static void main(String []args){
int[] a = {9,8,7,6,5,4,3,2,1};//plz use sophisticated random number generator
System.out.println( Arrays.toString(a) );
System.out.println( Arrays.toString(shellSort(a)) );
}
//Performs a shell sort on an array of ints.
public static int[] shellSort(int[] array){
int h = 1;
while (h < array.length) h = 3*h + 1;
while (h > 0){
h = h/3;
for (int k = 0; k < h; k++){
for (int i = h+k; i < array.length; i+=h){
int key = array[i];
int j = i-h;
while (j>=0 && array[j] > key){
array[j+h] = array[j];
j-=h;
}
array[j+h] = key;
//-> invariant: array[0,h,2*h..j] is sorted
}
}
//->invariant: each h-sub-array is sorted
}
return array;
};
}
P.S.: Check this link for other sorting algorithms (they are in c++, though, easily portable to java).
package sort_tester;
public class ShellSorter extends Sorter {
private final int[] gapArray = {1750,701,301,132,57,23,10,4,1};
#Override
public void makeSort (boolean trace) {
int size = list.length;
int i,j, temp;
for ( int gap : gapArray ) {
i = gap;
while ( i < size ) {
temp = list[i];
j = i-gap;
while ( j >= 0 && list[j] > temp ) {
list[j + gap] = list[j];
j -= gap;
}
list[j + gap] = temp;
i ++;
}
}
}
}
list - is int[];
GapArray taken from arcticle of Marcin Ciura
http://sun.aei.polsl.pl/~mciura/publikacje/shellsort.pdf
Here is a video link: https://youtu.be/SCBf7aqKQEY
The guy has made a good video of shell sort!!
And a simple code:
int sort(int arr[])
{
int n = arr.length;
int gap = n/2;
int i,j;
while(gap>0)
{ for (i=0,j=i+gap; j<n; i++,++j)
{
if(arr[i]>arr[j]) //swap
{ int temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
gap=gap/2;
}
return 0;
}
Use this
public void shellSort(Integer[] arr) {
int interval = arr.length / 2;
while (interval != 0) {
for (int i = 0; i < interval; i++) {
for (int p = i + interval; p < arr.length; p += interval) {
int key = arr[p];
int j = p - interval;
while (j >= 0) {
if (key < arr[j]) {
arr[j + interval] = arr[j];
} else
break;
j -= interval;
}
arr[j + interval] = key;
}
}
interval /= 2;
}
}
Snippet with 3k+1 gap.
public void shellSort(Comparable arr[], int size, int h, int x) {
while (h >= 1) {
for (int i = 0; i <= size - h; i++) {
for (int j = i; j < size-h && (arr[j].compareTo(arr[j+h]) > 0); j += h)
swap(arr, j, j+h);
}
h = 3*(--x) + 1;
}
}

Categories