how to print out the number of times a loop happened? [closed] - java

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm am trying to determine how many comparisons are made for both the linear and binary searching techniques. can someone tell me how to print out the number of times a loop happened in each case? for example, to find 5 in the first array the loop happens only one time.
public static void main(String[] args) {
// TODO code application logic here
int[] values = {5, 8, 6, 2, 1, 7, 9, 3, 0, 4, 20, 50, 11, 22, 32, 120};
int[] valuesSorted = {1, 2, 3, 4, 5, 8, 16, 32, 51, 57, 59, 83, 90, 104};
DisplayArray(values);
DisplayArray(valuesSorted);
int index;
index = IndexOf(1, values);
System.out.println("1 is at values location " + index);
index = IndexOf(120, values);
System.out.println("120 is at values location " + index);
index = BinaryIndexOf(104, valuesSorted);
System.out.println("104 is at values Sorted location " + index);
index = BinaryIndexOf(90, valuesSorted);
System.out.println("90 is at values Sorted location " + index);
}
public static int IndexOf(int value, int[] array)
{
for (int i=0; i < array.length; i++)
{
if(array[i] == value)
return i;
}
return -1;
}
public static int BinaryIndexOf(int value, int [] array)
{
int start = 0;
int end = array.length -1;
int middle;
while (end >= start)
{
middle = (start + end ) /2;
if (array[middle]== value)
return middle;
if (array[middle]< value)
start = middle + 1;
else
end = middle - 1;
}
return -1;
}
public static void DisplayArray(int[] array)
{
for (int a : array)
{
System.out.print(a + " ");
}
System.out.println();
}
}

For the linear search, you can do something like this:
public static int IndexOf(int value, int[] array)
{
for (int i=0; i < array.length; i++)
{
if(array[i] == value)
{
System.out.println("Linear search: Number of comparisons = " + (i + 1));
return i;
}
}
return -1;
}
For the Binary Search, do this:
public static int BinaryIndexOf(int value, int [] array)
{
int start = 0;
int end = array.length -1;
int middle;
int loopCount = 0;
while (end >= start)
{
loopCount++;
middle = (start + end ) /2;
if (array[middle]== value)
{
System.out.println("Binary search: Number of times looped = " + loopCount);
return middle;
}
if (array[middle]< value)
start = middle + 1;
else
end = middle - 1;
}
System.out.println("Binary search: Number of times looped = " + loopCount);
return -1;
}

One easy way is to make a counter variable inside your class, and manipulate it as needed, like this:
private static int _loopCounter;
public static void main(String[] args) {
// TODO code application logic here
int[] values = {5, 8, 6, 2, 1, 7, 9, 3, 0, 4, 20, 50, 11, 22, 32, 120};
int[] valuesSorted = {1, 2, 3, 4, 5, 8, 16, 32, 51, 57, 59, 83, 90, 104};
DisplayArray(values);
DisplayArray(valuesSorted);
_loopCounter = 0;
int index;
index = IndexOf(1, values);
System.out.println("1 is at values location " + index);
System.out.println(_loopCounter + " comparisons were made.")
_loopCounter = 0;
index = IndexOf(120, values);
System.out.println("120 is at values location " + index);
System.out.println(_loopCounter + " comparisons were made.")
_loopCounter = 0;
index = BinaryIndexOf(104, valuesSorted);
System.out.println("104 is at values Sorted location " + index);
System.out.println(_loopCounter + " comparisons were made.")
_loopCounter = 0;
index = BinaryIndexOf(90, valuesSorted);
System.out.println("90 is at values Sorted location " + index);
System.out.println(_loopCounter + " comparisons were made.")
}
public static int IndexOf(int value, int[] array)
{
for (int i=0; i < array.length; i++)
{
_loopCounter++;
if(array[i] == value)
return i;
}
return -1;
}
public static int BinaryIndexOf(int value, int [] array)
{
int start = 0;
int end = array.length -1;
int middle;
while (end >= start)
{
_loopCounter++;
middle = (start + end ) /2;
if (array[middle]== value)
return middle;
if (array[middle]< value)
start = middle + 1;
else
end = middle - 1;
}
return -1;
}
public static void DisplayArray(int[] array)
{
for (int a : array)
{
System.out.print(a + " ");
}
System.out.println();
}
I didn't test this code, but I'll bet you can modify it to work how you want.

Related

Jump search with Strings

i have been doing research on the jump search algorithm in an array. But i am confused on how you can use the search method with strings. Can anyone give me a link on where to look, or an example on here? Every example i have seen has been with numbers
public class JumpSearch
{
public static int jumpSearch(int[] arr, int x)
{
int n = arr.length;
int step = (int)Math.floor(Math.sqrt(n));
int prev = 0;
while (arr[Math.min(step, n)-1] < x)
{
prev = step;
step += (int)Math.floor(Math.sqrt(n));
if (prev >= n)
return -1;
}
while (arr[prev] < x)
{
prev++;
if (prev == Math.min(step, n))
return -1;
}
if (arr[prev] == x)
return prev;
return -1;
}
public static void main(String [ ] args)
{
int arr[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21,
34, 55, 89, 144, 233, 377, 610};
int x = 55;
// Find the index of 'x' using Jump Search
int index = jumpSearch(arr, x);
System.out.println("\nNumber " + x +
" is at index " + index);
}
}

shifting 0's or 1's to one side with less number of shifts

I have come across this test, where I need to move either 0's or 1's to one side with less number of shifts. For example, I have a list [1,1,1,1,0,1,0,1] by making 3 shifts we can move 0's to right end of the list and result is going to be [1,1,1,1,1,1,0,0].
The swap can happen between the adjacent only. I have tried and was not successful.
This is what I have tried:
public class Test {
public static void main(String[] args) {
List<Integer> arr = Arrays.asList(1, 1, 1, 1, 0, 1, 0, 1);
List<Integer> copy = new ArrayList<>(arr);
int noOfSwaps = 0;
while(true) {
int currentNoOfSwaps = noOfSwaps;
for (int i=1; i<arr.size()-1; i++){
if (arr.get(i-1)!=arr.get(i) && arr.get(i) != arr.get(i+1) && arr.get(i-1) == arr.get(i+1) ){
swap(copy, i);
currentNoOfSwaps++;
arr = new ArrayList<>(copy);
break;
}else if (arr.get(i-1) == arr.get(i) && arr.get(i) != arr.get(i+1) && arr.get(i-1) != arr.get(i+1)) {
swap(copy,i);
currentNoOfSwaps++;
arr = new ArrayList<>(copy);
break;
}
}
if (currentNoOfSwaps == noOfSwaps) {
break;
} else {
noOfSwaps = currentNoOfSwaps;
}
}
System.out.println(noOfSwaps);
}
private static void swap(List<Integer> arr, int i) {
int temp = arr.get(i+1);
arr.remove(i+1);
arr.add(i+1, arr.get(i));
arr.remove(i);
arr.add(i, temp);
}
}
Can someone please help me in fix this?
"Swap"
In order to solve the "swap" problem, you have to implement an algorithm that works as follows:
Let swapCount be 0
Let endOffset be list size - 1
Iterate over the list starting at i = 0 and check the number
If it is a 1, continue iteration (nothing to be done here)
If it is a 0, start a new iteration backward starting from j = endOffset and check the swapCandidate
If it is a 0, continue iteration (nothing to be done here)
If it is a 1
swap it with number
increment swapCount
let endOffset be j - 1
Implementation
public class SwapZeros {
private static int swapZerosToRight(List<Integer> list) {
final ArrayList<Integer> result = new ArrayList<>(list);
int swapCount = 0;
int endOffset = result.size() - 1;
for (int i = 0; i < result.size(); i++) {
int number = result.get(i);
if (number == 1) {
continue;
}
for (int j = endOffset; j > i; --j) {
int swapCandidate = result.get(j);
if (swapCandidate == 1) {
result.set(i, swapCandidate);
result.set(j, number);
endOffset = j;
swapCount++;
break;
}
}
}
return swapCount;
}
public static void main(String[] args) {
int swapCount = swapZerosToRight(Arrays.asList(1, 1, 1, 1, 0, 1, 0, 1));
System.out.println("Required " + swapCount + " swap(s)");
}
}
Output
Required 1 swap(s)
"Shift"
The shift algorithm is a little simpler, since you only need to iterate into one direction; no nested loop is required.
Let shiftCount be 0
Iterate over the list starting at i = 0 and check the number
If it is a 1, continue iteration (nothing to be done here)
If it is a 0,
Swap with the number at position i + 1
Let shiftCount be shiftCount + 1
Implementation
public class ShiftZeros {
private static int shiftZerosToRight(List<Integer> list) {
final ArrayList<Integer> copy = new ArrayList<>(list);
int shiftCount = 0;
for (int i = 0; i < copy.size() - 1; i++) {
int number = copy.get(i);
if (number == 1) {
continue;
}
copy.set(i, copy.get(i + 1));
copy.set(i + 1, number);
shiftCount++;
}
return shiftCount;
}
public static void main(String[] args) {
int shiftCount = shiftZerosToRight(Arrays.asList(1, 1, 1, 1, 0, 1, 0, 1));
System.out.println("Required " + shiftCount + " shift(s)");
}
}
Output
Required 3 shift(s)

I am not sure what is wrong with my quick select method

So I tried to write a code to select kth smallest element in a given input integer array using quick sort, but for some reason as you can see in the code below,
public static int partition(int[] input, int first, int end) {
int pivot = input[(first + end)/2];
int i = first - 1;
int j = end + 1;
while (true) {
do {
i++;
} while (input[i] < pivot);
do {
j--;
} while (input[j] > pivot);
if (i < j)
swap(input, i, j);
else
return j;
}
}
public static void swap(int[] input, int i, int j){
int temp = input[i];
input[i] = input[j];
input[j] = temp;
}
public static int select(int[] input, int k){
return mySelect(input, 0, input.length-1, k);
}
public static int mySelect(int[] input, int left, int right, int k){
// If k is smaller than number of elements in array
if (k > 0 && k <= right - left + 1) {
int pos = partition(input, left, right);
if (pos - left == k - 1)
return input[pos];
// If position is larger, recursive call on the left subarray
if (pos - left > k - 1)
return mySelect(input, left, pos-1, k);
// if smaller, recursive call on the right subarray
return mySelect(input, pos+1, right, k-pos+left-1);
}
System.out.println("Invalid k value");
return Integer.MAX_VALUE;
}
public static void main(String[] args){
test2 = new int[]{99, 44, 77, 22, 55, 66, 11, 88, 33};
int[] test2 = new int[]{99, 44, 77, 22, 55, 66, 11, 88, 33};
//testing for selecting kth min
System.out.println("The 1st smallest : " + select(test2, 1));
System.out.println("The 2nd smallest : " + select(test2, 2));
System.out.println("The 3rd smallest : " + select(test2, 3));
System.out.println("The 4th smallest : " + select(test2, 4));
System.out.println("The 6th smallest : " + select(test2, 6));
System.out.println("The 9th smallest : " + select(test2, 9));
}
but my 1st smallest element appears 22, 2nd smallest returns 11, while others values are normal.
can someone please help me find what is the mistake I made?
The problem in your code is in the partition. The do while is the culprit here. You are updating the positions before checking the conditions and that's causing the problem with the last swap operation.
Update your method to this and you will be good to go
public static int partition(int[] input, int first, int end) {
int pivot = input[(first + end)/2];
int i = first;
int j = end;
while (true) {
while (input[i] < pivot) {
i++;
}
while (input[j] > pivot) {
j--;
}
if (i < j)
swap(input, i, j);
else
return j;
}
}
I wrote this code while I was learning quick sort. The mistake I made was in not realizing that
'left' and 'right' are indices in array whereas,
'pivot' is one of the values stored in array
You can study my code below and determine what's wrong in your understanding of the algorithm!!
public class QuickSort
{
public static void main(String[] args)
{
int[] nums = {1,5,9,2,7,8,4,2,5,8,9,12,35,21,34,22,1,45};
quickSort(0,nums.length-1,nums);
//print nums to see if sort is working as expected,omit printing in your case
print(nums);
//now you can write code to select kth smallest number from sorted nums here
}
/**
* left and right are indices in array whereas
* pivot is one of the values stored in array
*/
static int partition(int left, int right , int pivot, int[] nums)
{
int leftIndex = left -1;
int rightIndex = right;
int temp = 0;
while(true)
{
while( nums[++leftIndex] < pivot );
while( rightIndex>0 && nums[--rightIndex] > pivot );
if( leftIndex >= rightIndex ) break;
else//swap value at leftIndex and rightIndex
{
temp = nums[leftIndex];
nums[leftIndex]= nums[rightIndex];
nums[rightIndex] = temp;
}
}
//swap value at leftIndex and initial right index
temp = nums[leftIndex];
nums[leftIndex]= nums[right];
nums[right] = temp;
return leftIndex;
}
static void quickSort( int leftIndex , int rightIndex ,int[] nums)
{
if( rightIndex-leftIndex <= 0 ) return;
else
{
int pivot = nums[rightIndex];
int partitionIndex = partition(leftIndex, rightIndex , pivot,nums);
quickSort(leftIndex,partitionIndex-1,nums);
quickSort(partitionIndex+1,rightIndex,nums);
}
}
static void print(int[] nums)
{
for( int i = 0 ; i < nums.length ; i++ )
{
System.out.print(nums[i]+", ");
}
}
}

Maximum Sum SubArray

I am trying to find the contiguous subarray within an array which has the largest sum. So, for the array
{5, 15, -30, 10, -5, 40, 10}
the maximum sum possible using those numbers contiguously would be 55, or (10 + (-5) + 40 + 10) = 55. The program below outputs the maximum sum of 55, however, the problem I am trying to figure out is how to print the sequence that produces this 55. In other words, how can I print out the 10, -5, 40, and 10?
public static void main(String[] args) {
int[] arr = {5, 15, -30, 10, -5, 40, 10};
System.out.println(maxSubsequenceSum(arr));
}
public static int maxSubsequenceSum(int[] X) {
int max = X[0];
int sum = X[0];
for (int i = 1; i < X.length; i++) {
sum = Math.max(X[i], sum + X[i]);
max = Math.max(max, sum);
}
return max;
}
I was thinking of creating an ArrayList to store the sum values at every index of i, so the ArrayList would look like (5, 20, -10, 10, 5, 45, 55). And then I was planning on clearing the ArrayList from index 0 to the first negative number in the list, however, this only solves the problem for this specific example, but if I change the original array of numbers, this solution won't work.
You can replace Math.Max functions by if statements and update start and end index of the best subarray. Pascal version:
if X[i] > sum + X[i] then begin
sum := X[i];
start := i;
end
else
sum := sum + X[i];
if max < sum then begin
max := sum;
finish := i;
end;
You can track the starting and ending indexes of the current best subarray in your loop. Instead of using max() to compute sumand max, just do the following :
int sum_start = 0, sum_end = 0, start = 0, end = 0;
// In the for loop
if (X[i] > sum + X[i]) {
sum = X[i];
sum_start = i;
sum_end = i;
} else {
++sum_end;
}
if (sum > max) {
start = sum_start;
end = sum_end;
max = sum;
}
there is an o(n) solution, a single for loop through the array and reset your sub-sequence whenever your current total is below 0.
{5, 15, -30, 10, -5, 40, 10}
5 + 15 = 20
20 - 30 = -10 (reset sub-sequence)
10 -5 +40 +10 = 55
end. 55 is max sub-sequence
edit: to get subsequence...
whenever you change max, update your subsequence
current left index changes only when u reset
current right index changes every iteration
new max -> save current left and right index...
It can be done by capturing the start and end while identifying maximum sub-array as follows:
Code
package recursion;
import java.util.Arrays;
public class MaximumSubArray {
private static SubArray maxSubArray(int[] values, int low, int high) {
if (low == high) {
// base condition
return new SubArray(low, high, values[low]);
} else {
int mid = (int) (low + high) / 2;
// Check left side
SubArray leftSubArray = maxSubArray(values, low, mid);
// Check right side
SubArray rightSubArray = maxSubArray(values, mid + 1, high);
// Check from middle
SubArray crossSubArray = maxCrossSubArray(values, low, mid, high);
// Compare left, right and middle arrays to find maximum sub-array
if (leftSubArray.getSum() >= rightSubArray.getSum()
&& leftSubArray.getSum() >= crossSubArray.getSum()) {
return leftSubArray;
} else if (rightSubArray.getSum() >= leftSubArray.getSum()
&& rightSubArray.getSum() >= crossSubArray.getSum()) {
return rightSubArray;
} else {
return crossSubArray;
}
}
}
private static SubArray maxCrossSubArray(int[] values, int low, int mid,
int high) {
int sum = 0;
int maxLeft = low;
int maxRight = high;
int leftSum = Integer.MIN_VALUE;
for (int i = mid; i >= low; i--) {
sum = sum + values[i];
if (sum > leftSum) {
leftSum = sum;
maxLeft = i;
}
}
sum = 0;
int rightSum = Integer.MIN_VALUE;
for (int j = mid + 1; j <= high; j++) {
sum = sum + values[j];
if (sum > rightSum) {
rightSum = sum;
maxRight = j;
}
}
SubArray max = new SubArray(maxLeft, maxRight, (leftSum + rightSum));
return max;
}
static class SubArray {
private int start;
private int end;
private int sum;
public SubArray(int start, int end, int sum) {
super();
this.start = start;
this.end = end;
this.sum = sum;
}
public int getStart() { return start; }
public void setStart(int start) { this.start = start; }
public int getEnd() { return end; }
public void setEnd(int end) { this.end = end; }
public int getSum() { return sum; }
public void setSum(int sum) { this.sum = sum; }
#Override
public String toString() {
return "SubArray [start=" + start + ", end=" + end + ", sum=" + sum + "]";
}
}
public static final void main(String[] args) {
int[] values = { 5, 15, -30, 10, -5, 40, 10 };
System.out.println("Maximum sub-array for array"
+ Arrays.toString(values) + ": " + maxSubArray(values, 0, 6));
}
}
Output
Maximum sub-array for array[5, 15, -30, 10, -5, 40, 10]: SubArray [start=3, end=6, sum=55]
Solution can be downloaded from https://github.com/gosaliajigar/Programs/blob/master/src/recursion/MaximumSubArray.java
Two subarray are
[1, 2, 3]
and [4, 9] excluding the negative number
The max sub array here is [ 4, 5]
so the output is 9
Here is the code
public class MaxSubArray{
static void sumM(int a[], int n){
int s1 = Integer.MAX_VALUE;
int k = Integer.MAX_VALUE;
int sum = 0;
int s2 = 0;
for(int i=0;i<n;i++){
if(a[i]<s1){
if(a[i]<0){
k = Math.min(a[i],s1);
}
}
if(a[i]>k){
sum+=a[i];
}
if(a[i]<k){
if(a[i]<0){
continue;
}
s2+=a[i];
}
}
if(sum>s2){
System.out.println(sum);
}
else{
System.out.println(s2);
}
}
public static void main(String[] args){
int a[] = {1,2,3,-7,4,5};
int n = a.length;
sumM(a,n);
}
}
public static int kadane(int[] A) {
int maxSoFar = 0;
int maxEndingHere = 0;
// traverse the given array
for (int i: A) {
// update the maximum sum of subarray "ending" at index `i` (by adding the
// current element to maximum sum ending at previous index `i-1`)
maxEndingHere = maxEndingHere + i;
// if the maximum sum is negative, set it to 0 (which represents
// an empty subarray)
maxEndingHere = Integer.max(maxEndingHere, 0);
// update the result if the current subarray sum is found to be greater
maxSoFar = Integer.max(maxSoFar, maxEndingHere);
}
return maxSoFar;
}
var maxSequence = function(arr){
// ...
if (arr.every((ele) => ele >= 0)) {
return arr.reduce((sum, ele) => sum + ele, 0);
} else if (arr.every((ele) => ele < 0)) {
return 0;
//for me the maximum would be the biggest negative number
//arr.reduce((max, elm) => (max > elm ? max : elm))
} else if (arr === [0]) {
return 0;
} else {
let maxSum = [];
let currentSum = 0;
for (let i = 0; i < arr.length; i++) {
currentSum = Math.max(arr[i], currentSum + arr[i]);
maxSum.push(currentSum);
}
return maxSum.reduce((max, elm) => (max > elm ? max : elm));
}
}
you need to sum all possible sub array. to do that, you can do this code
public static int maxSubsequenceSum(int[] X) {
int max = 0;
boolean max_init = false;
int max_from=0;
int max_to=0; // this is not included
for (int i = 0; i < X.length; i++) {
for (int j = i + 1; j < X.length + 1; j++) {
int total = 0;
for (int k = i; k < j; k++) {
total += X[k];
}
if (total > max || !max_init){
max = total;
max_init = true;
max_from = i;
max_to = j;
}
}
}
for (int i=max_from;i<max_to;i++)
System.out.print(X[i]+",");
System.out.println();
return max;
}

A recursive algorithm to find every possible sum given an integer array?

So given an array for example [3, 5, 7, 2] I want to use recursion to give me all the possible combinations of sums, for example: 3, 5, 7, 2, 8(3+5),10(3+7),5(3+5)... 15(3+5+7) etc. I'm not exactly sure how to go about this using java.
You have two choice with each number in the array.
Use the number
Don't use the number
void foo(int[] array, int start, int sum) {
if(array.length == start) return;
int val = sum + array[start];
//print val;
foo(array, start + 1, val); //use the number
foo(array, start + 1, sum); //don't use the number
}
the initial call is foo(a, 0, 0)
An recursive algorithm for this could work as follows:
All the sums for a list equals the union of:
The first number plus the sums of the sublist without the first number
The sums of the sublist without the first number
Eventually your recursive call will hit the stopping condition of an empty list, which has only one sum(zero)
Here's one way of doing it in pseudo code:
getAllPossibleSums(list)
if(list.length == 1)
return list[0];
otherSums = getAllPossibleSums(list[1:end])
return union(
otherSums, list[0] + otherSums);
public static void main(String[] args) {
findAllSums(new int[] {3, 5, 7, 2}, 0, 0);
}
static void findAllSums(int[] arrayOfNumbers, int index, int sum) {
if (index == arrayOfNumbers.length) {
System.out.println(sum);
return;
}
findAllSums(arrayOfNumbers, index + 1, sum + arrayOfNumbers[index]);
findAllSums(arrayOfNumbers, index + 1, sum);
}
You have two branches, one in which you add the current number and another in which you don't.
public static void main(String[] args) {
int [] A = {3, 5, 7, 2};
int summation = recursiveSum(A, 0, A.length-1);
System.out.println(summation);
}
static int recursiveSum(int[] Array, int p, int q) {
int mid = (p+q)/2; //Base case
if (p>q) return 0; //Base case
else if (p==q) return Array[p];
**else
return recursiveSum(Array, p, mid) + recursiveSum(Array, mid + 1, q);**
}
Simplest way ever:
private int noOfSet;
Add below method:
private void checkSum() {
List<Integer> input = new ArrayList<>();
input.add(9);
input.add(8);
input.add(10);
input.add(4);
input.add(5);
input.add(7);
input.add(3);
int targetSum = 15;
checkSumRecursive(input, targetSum, new ArrayList<Integer>());
}
private void checkSumRecursive(List<Integer> remaining, int targetSum, List<Integer> listToSum) {
// Sum up partial
int sum = 0;
for (int x : listToSum) {
sum += x;
}
//Check if sum matched with potential
if (sum == targetSum) {
noOfSet++;
Log.i("Set Count", noOfSet + "");
for (int value : listToSum) {
Log.i("Value", value + "");
}
}
//Check sum passed
if (sum >= targetSum)
return;
//Iterate each input character
for (int i = 0; i < remaining.size(); i++) {
// Build list of remaining items to iterate
List<Integer> newRemaining = new ArrayList<>();
for (int j = i + 1; j < remaining.size(); j++)
newRemaining.add(remaining.get(j));
// Update partial list
List<Integer> newListToSum = new ArrayList<>(listToSum);
int currentItem = remaining.get(i);
newListToSum.add(currentItem);
checkSumRecursive(newRemaining, targetSum, newListToSum);
}
}
Hope this will help you.

Categories

Resources