public static int cycleSort(int[] array) {
		int writes = 0;
		// Loop through the array to find cycles to rotate.
		for (int cycleStart = 0; cycleStart < array.length; cycleStart++) {
			int item = array[cycleStart];
			int pos = cycleStart;
			
			for (int i = cycleStart + 1; i < array.length; i++) {
				if (array[i] < item)
					pos++;
			}
			// if item is already there, it is not a cycle
			if (pos == cycleStart)
				continue;
			
			// Otherwise, put the item there or right after any duplicates.
			item = putItemAfterDup(array, pos, item);
			writes++;
			
		    // Rotate the rest of the cycle.
			while (pos != cycleStart) {
				// Find where to put the item.
				pos = cycleStart;
				for (int i = cycleStart + 1; i < array.length; i++)
					if (array[i] < item)
						pos++;
				
				// Put the item there or right after any duplicates.
				item = putItemAfterDup(array, pos, item);
				writes++;
			}
		}
		return writes;
	}