shell sort


See On Github

Data

Contributor

Generic placeholder thumbnail

by pravsingh

in java

Source Code

package github_contributions;

public class shell_sort {

	public static void shellSort(int[] a) {
		int increment = a.length / 2;
		while (increment > 0) {
			for (int i = increment; i < a.length; i++) {
				int j = i;
				int temp = a[i];
				while (j >= increment && a[j - increment] > temp) {
					a[j] = a[j - increment];
					j = j - increment;
				}
				a[j] = temp;
			}
			if (increment == 2) {
				increment = 1;
			} else {
				increment *= (5.0 / 11);
			}
		}
	}

}

package github_contributions;

import static org.junit.Assert.*;
import org.junit.Test;

/**
 * 
 * @author pravsingh
 * 
 *         Junit test cases around shell sort.
 * 
 */
public class shell_sort_unittest {

	@Test
	public void testHappyPath() {

		int data[] = { 1, 3, 2 };
		shell_sort.shellSort(data);

		assertEquals(data[0], 1);
		assertEquals(data[1], 2);
		assertEquals(data[2], 3);
	}

	@Test
	public void testEmptyArray() {

		int data[] = {};
		shell_sort.shellSort(data);

		assertEquals(data.length, 0);
	}

}