0

I'm working on a very simple class in java that allow the client to create an integer array and insert, remove, and check whether an element is in this array or not. One of the class restrictions is that the array should be sorted in ascending order at all times. Is it efficient for the insert(int x) method to call a private sort method each time an element is inserted or is there another approach?

1
  • Array size is fix. Better to use ArrayList. Commented Mar 30, 2013 at 17:55

5 Answers 5

1

If it has to be an array, the best way to do it is to do a binary search to find the insertion point, move the higher value elements up by one place, then put the new element in the gap that opens up.

That moves on average half the array elements for each insert.

If you have the option of using a different data structure, I suggest a TreeMap in which the key is the Integer you are inserting, and the value is a count of the number of times that element has been inserted.

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

Comments

0

I suggest you to use a sorted collection like a SortedSet< Integer > (implementation class TreeSet).

Always sorted and remove duplicates.

If you absolutely want an int[], java.util.Arrays utility contains search and sort.

Comments

0

A Java array has fixed size, if you need to do removals and insertion you probably want a List (ArrayList or LinkedList)

Some alternatives:

  1. A List; do a binary search for each insert to find the insertion point.

  2. A List plus a Map; you keep in the map that tells you the (first) index of each integer in the list.

  3. A List; do an insert plus a Collections.sort(list)

1 is the less straightforward, but most instructive. 2 is the most efficient in speed, but wastes space. 3 is the most straightforward and pragmatic.

Comments

0

How to sort an array each time an element is inserted

Do like this

 Arrays.sort(array);

Sorts the specified array of ints into ascending numerical order. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.

Read the java docs here.

and grow the item at runtime instead of Array use ArrayList.

Do like this in arrayList.

List<String> unsortList = new ArrayList<String>();
unsortList.add("111");
unsortList.add("333");
unsortList.add("222");

//before sort
System.out.println("ArrayList is unsort");
for(String temp: unsortList){
    System.out.println(temp);
}

//sort the list
Collections.sort(unsortList);

//after sorted
System.out.println("ArrayList is sorted");
for(String temp: unsortList){
    System.out.println(temp);
}

4 Comments

But an array size is fixed, does not allow to insert new elements.
@leonbloy I gave the answer on basis of his question.
The OP must be meaning a "conceptual" array, not a Java array. The OP is asking how to insert elements and keep them sorted. A Java array does not allow to insert elements, so I don't see how this answer might conceivably be useful for the problem.
i am having an issue with the Array.sort(a) I have performed the following operations insert 15 insert 3 insert 33 however the result I got was {0, 33}.. where are the 15 and 3? (the 0 is because of dynamic size manipulation for the array whenever this action is needed)
0

Arrays might not the best way of achieving what you are after, but this depends on what it is that you want from your class, and which methods it is important are fast.

To achieve what you are asking with an array, the best results you will be able to get are this (assuming the array is of infinite size / you are only storing a max number of integers):

  • insertion: Worst Case: O(n), Average Case: O(n)
  • removal: Worst Case: O(n), Average Case: O(n)
  • getting element by index: Always: O(1)
  • checking if element exists: Worst Case: O(log n) [Binary Search]

If this is the sort of efficiency and behaviour you are after, then here is the answer to your question:

So you are asking if this is efficient:

  • Add new integer to end of the array: efficiency: O(1)
  • Sort entire array: efficiency: O(n * log(n))

Total Efficiency: O(n * log(n))

There is a much more efficient way of doing it, this would involve doing the following:

  • Perform Binary Search to find the point in the array where you need to insert the new integer. O( log n )
  • Move everything to the right of this point along 1 (to make room in correct place). O(n)
  • Insert new integer: O(1)

Total Efficiency: O( n )

Different data structures:

If this is not the kind of behaviour that you are after, then you could maybe look into some different kinds of data structures. For example, something based on trees, like TreeSet will give very different efficiency:

  • insertion: O( log n )
  • removal: O( log n )
  • getting element by index: (not possible i believe? not familiar with this class's complete implementation)
  • checking if element exists: O( log n )

Comments

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.