Interview Questions

How can we sort one unsorted int array with O(n) ....

Microsoft Interview Questions and Answers


(Continued from previous question...)

92. How can we sort one unsorted int array with O(n) ....

Question:
How can we sort one unsorted int array with O(n).

Unsorted : {4,2,6,1,5,5,1,2,45,444,44,45,4,1}
Sorted : {1,1,1,2,2,4,4,5,5,6,44,45,45,444}


maybe an answer:


static void sortUnsortedArray()
{
int[] arr = new int[] { 1,3,2,1,6,45,6,7,3,67,43,499,9 };
int i=0, j=0, count = 0;
int[] tempArr=new int[500];
for (i = 0; i < arr.Length - 1; i++)
{
j = arr[i];
tempArr[j] = tempArr[j] + 1;
}

arr = new int[12];
int intAdd=0;
for (i = 0; i < tempArr.Length; i++)
{
if (tempArr[i] > 0)
{
count = tempArr[i];
while (count != 0)
{
arr[intAdd] = i;
count -= 1;
intAdd += 1;
}
}
}
}

(Continued on next question...)

Other Interview Questions