Microsoft Interview Question: Sorting Algorithms Optimizati... | Glassdoor.co.in

Interview Question

Software Development Engineer In Test (SDET) Interview Bengaluru

Sorting Algorithms Optimizations from 2 loops to just 1 loop

Answer

Interview Answer

5 Answers

0

#include
#include
using namespace std;

int main()
{
    int list[10] = {5, 4, 3, 6, 7, 1, 2, 9, 8, 10};

    for (int i=0; i= 0 && next list[next])
            {
                int temp = list[i];
                list[i] = list[next];
                list[next] = temp;
                next--;
            }
            else next++;
        }
    }

    for (int i=0; i<10; i++)
    {
        cout << list[i] << " ";
    }
        return 0;
}

o1o1o111 on 09-Apr-2012
0

you still have two loops

#include
#include
using namespace std;

int main()
{
    int list[10] = {5, 4, 3, 6, 7, 1, 2, 9, 8, 10};

    for (int i=0;j=i; i<10; i++;j++)
    {
     I can't remember, but you use two indices within one loop to sort using bubble sort
    }

    for (int i=0; i<10; i++)
    {
        cout << list[i] << " ";
    }
        return 0;
}

musi on 19-Apr-2012
1

use quicksort. it has runtime of O(n.logn), or heapsort.
they both use one for loop and a heap/stack

moataz on 05-Oct-2012

One or more comments have been removed.
Please see our Community Guidelines or Terms of Service for more information.

Add Answers or Comments

To comment on this, Sign In or Sign Up.