Question 6-2

[5 pts] Consider the following method.

public static void sortBadly(double[] to_sort) {
  for(int i = 0; i < to_sort.length; i++) {
    int item = to_sort[i];
    int dest = 0;
    while(to_sort[dest] < item) {
      dest++;
    }
    for(int j = i; j > dest; j--) {
      to_sort[j] = to_sort[j - 1];
    }
    to_sort[dest] = item;
  }
}
Let n represent the length of to_sort. In terms of n, how much time does the above method take? Use Big-O notation to express your answer, and provide the tightest bound that you can.

Solution

O(n2)

Back to 8:00 Quiz 6