2

I tried to find the smallest element in an integer array using what i understood about divide and conquor algorithm. I am getting correct results. But i am not sure if it is a conventional way of using divide and conquor algorithm. If there is any other smarter way of implementing divide and conquor algorithm than what i have tried then please let me know it.

public static int smallest(int[] array){
       int i = 0;
       int array1[] = new int[array.length/2];
       int array2[] = new int[array.length - (array.length/2)];
       for(int index = 0; index < array.length/2 ; index++){
               array1[index] = array[index];
        }
        for(int index = array.length/2; index < array.length; index++){
               array2[i] = array[index];
                i++;
        }
        if(array.length > 1){
            if(smallest(array1) < smallest(array2)){
                return smallest(array1);
            }else{
                 return smallest(array2);
              }
        }
        return array[0];
   }
3
  • 2
    Yes, this is the correct way to implement this divide and conquer algorithm. If you can try without creating new arrays in every call, that would be better. Commented Jul 28, 2015 at 16:31
  • 1
    Check out this previous post for some ideas: stackoverflow.com/questions/14949982/… Commented Jul 28, 2015 at 16:31
  • @karthik thanx. I will try as per your suggestion. Commented Jul 28, 2015 at 16:45

3 Answers 3

1

Your code is correct, but You can write less code using existing functions like Arrays.copyOfRange and Math.min

public static int smallest(int[] array) {
    if (array.length == 1) {
        return array[0];
    } 
    int array1[] = Arrays.copyOfRange(array, 0, array.length / 2);
    int array2[] = Arrays.copyOfRange(array, array.length / 2, array.length);
    return Math.min(smallest(array1), smallest(array2));        
}

Another point. Testing for the length == 1 at the beginning is more readable version. Functionally it is identical. From a performance point of view it creates less arrays, exiting as soon as possible from the smallest function.

It is also possible to use a different form of recursion where it is not necessary to create new arrays.

private static int smallest(int[] array, int from, int to) {
    if (from == to) {
        return array[from];
    }
    int middle = from + (to - from) / 2;
    return Math.min(smallest(array, from, middle), smallest(array, middle + 1, to));
}

public static int smallest(int[] array){
    return smallest(array, 0, array.length - 1);
}

This second version is more efficient because it doesn't creates new arrays.

Sign up to request clarification or add additional context in comments.

2 Comments

What's the difference between OP's code and your code?
I edited the answer to show that is possible to use existing functions.
1

I don't find any use in using a divide and conquer in this paticular program.

Anyhow you search for the whole array from 1 to N, but in two steps

1. 1 to N / 2

2. N / 2 + 1 to N

This is equivalent to 1 to N.

Also you program check for few additional checks after the loops which aren't actually required when you do it directly.

int min = a[0];
for(int i = 1; i < arr.length; i++)
if(min < a[i])
  a[i] = min;

This is considered most efficient in finding out the minimum value.

When do I use divide and conquer

A divide and conquer algorithm works by recursively breaking down a problem into two or more sub-problems, until these become simple enough to be solved directly.

Consider the Merge Sort Algorithm. enter image description here

Here, we divide the problem step by step untill we get smaller problem and then we combine them to sort them. In this case this is considered optimal. The normal runs in a O(n * n) and this runs in O(n log n).

But in finding the minimum the original has O(n). So this is good.

Divide And Conquer

The book

Data Structures and Algorithm Analysis in Java, 2nd edtition, Mark Allen Weiss

Says that a D&C algorithm should have two disjoint recursive calls. I.e like QuickSort. The above algorithm does not have this, even if it can be implemented recursively.

3 Comments

the OP is practicing in the use of divide and conquer programming technique, I don't think is interested into performance.
OP does not want to write the efficient algorithm, he wants to test his understand of Divide and Conquer paradigm. So what he did is the correct way to do it.
Correct Uma, but I suppose this is only a test to see how divide and conquer works. But I agree this is the best solution for this particular test
0

What you did here with code is correct. But there are more efficient ways of solving this code, of which i'm sure you're aware of.

Although divide and conquer algorithm can be applied to this problem, but it is more suited for complex data problem or to understand a difficult data problem by dividing it into smaller fragments. One prime example would be 'Tower of Hanoi'.

As far as your code is concerned, it is correct. Here's another copy of same code-

public class SmallestInteger {

public static void main(String[] args) {
    int small ;
    int array[] = {4,-2,8,3,56,34,67,84} ;
    small = smallest(array) ;
    System.out.println("The smallest integers is = " + small) ;
}

public static int smallest(int[] array) {
    int array1[] = new int[array.length/2];
    int array2[] = new int[array.length - (array.length/2)];

    for (int index = 0; index < array.length/2 ; index++) {
        array1[index] = array[index];
    }
    for (int index = array.length/2; index < array.length; index++) {
        array2[index - array.length/2] = array[index] ;
    }

    if (array.length > 1) {
        if(smallest(array1) < smallest(array2)) {
            return smallest(array1) ;
        }
        else {
            return smallest(array2) ;
        }
    }
    return array[0] ;

}

}

Result came out to be-

The smallest integers is = -2

2 Comments

The way you made the second 'for' loop look handsome by omitting use of one more variable is adorable.
Why do you call smallest twice? you can avoid computation time by storing it in a variable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.