-3
package com.sort;

public class ArraySel {

    private Long[] a;
    private int nElems;

    public ArraySel(int max)
    {
        a = new Long[max];
        nElems = 0;
    }

    public void insert(long max)
    {
        a[nElems] = max;
        nElems++;
    }

    public void display()
    {
        for(int j = 0; j < nElems; j++)
        {
            System.out.print(a[j]+ "  ");
        }
        System.out.println();
    }

    public void insertionSort()
    {
        int in , out, flag = 0;
        long temp;
        for(out = 1; out < nElems; out++)
        {
            temp = a[out];
            in = out;
            while(in > 0 && a[in - 1] >= temp )
            {
                if(a[in] == a[in - 1 ])
                {
                    flag++;
                    in--;
                }
                else
                {
                    a[in] = a[in-1];
                    in--;
                }
            }
            a[in] = temp;
        }
    }

}

This code takes an unsorted array and sorts it using Insertion Sort. When duplicates are arranged together in unsorted array then due to multiple shifting complexity raises to O(N^2) , which i tried to make it O(N) by making sure no item moved more than once in case of duplicates arranged together.

But when duplicates are not arranged together complexity remains O(N^2). Can we make the complexiy O(N) in this case too ?

akki_java
  • 605
  • 7
  • 17
  • "When duplicates are arranged together in unsorted array then due to multiple shifting complexity raises to O(N2) , which i tried to make it O(N) by making sure no item moved more than once in case of duplicates arranged together." - If the elements in the input array are not sorted, the complexity will still be O(n^2), irrespective of whether duplicates follow each other or not.. example - 5,2,2,1,6 will still have complexity of O(n^2) – TheLostMind Jun 23 '14 at 09:46
  • Insertion sort time complexity is always O(N^2) if I'm not wrong – Kartik_Koro Jun 23 '14 at 09:46
  • @Kartik_Koro - if the input array is sorted, then the best case complexity of insertion sort becomes O(n) – TheLostMind Jun 23 '14 at 09:49
  • @TheLostMind I know, but we are talking about big O notation, i.e. upper bound right? – Kartik_Koro Jun 23 '14 at 09:56
  • @Kartik_Koro yes.. The time complexity for a sorted array is O(n). Which means for a sorted array, the time cannot go beyond (someConstant * n). We are still setting the upper bound. – TheLostMind Jun 23 '14 at 10:02

2 Answers2

2

Complexity isn't given by the number of moves but by the number of operations overall, in this case comparisons as well.

Insertion sort is O(n^2) average complexity, you can't make it faster than that. In works in O(n) only in best case scenario, when the input string is already sorted (http://en.wikipedia.org/wiki/Insertion_sort).

rhobincu
  • 906
  • 1
  • 7
  • 22
1

Without further information about the underlying data, the best time complexity you can achieve with sorting algorithms is O(n log n) (n being the number of elements).

Sorting algorithms like insertion sort, bubble sort, selection sort, etc., all have a time complexity of O(n²) due to their double loops. In fact they sometimes tend to work better, when getting an already sorted list of elements. Insertion sort for example has a time complexity of O(n) for a completely sorted list.

There is nothing you can do to change the inherent time complexity of those algorithms. The only thing you can do is short-cutting the algorithm when finding pre-sorted regions in the incoming list.

Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66