I/P : Input array[] = {4.0, 6.5, 34.0, 2.0, 56.0, 73.0, 5.0, -5.0, 22.0, 45.0, 52.0, 42.0, 421.0}
O/P: Sorted array[] = {-5.0, 2.0, 4.0, 5.0, 6.5, 22.0, 34.0, 42.0, 45.0, 52.0, 56.0, 73.0, 421.0}

Sorted array using bubble sort.

public class BubbleSort {

double[] getBubbleSort(double[] values) {
return new double[] {};
}
}

I have to maintain the given code structure.
It is simple but I don't understand, how will I return a new double array of sorted values?
Please give me a hint for this type of return statement.

Recommended Answers

All 3 Replies

define a array of type double, store sorted value in it and the return the array. it will work.

Yes I know it, but I have to maintain this return statement, so, I can not return a double array which have already been declared previously in that method. So, please give me a hint how to create a double array initialized by a old array at the time of creation.

Try something like this:

double[] getBubbleSort(double[] values) {
  double [] newArray = new double[values.length];
  for (int i=0;i<values.length;i++) {
    newArray[i] = values[i];
  }
  //your code here
  return newArray;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.