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

This post has been removed.
Please see our Community Guidelines or Terms of Service for more information.

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
0

static void Main(string[] args)
        {
            int[] arr = new int[] { 8, 9, 6, 7, 4, 5, 2, 3, 1 };
            int i = 0;
            bool swapped = true;
            while (swapped || i arr[i + 1])
                {
                    int temp = arr[i+1];
                    arr[i + 1] = arr[i];
                    arr[i] = temp;
                    swapped = true;
                    i=0;
                }
                else
                {
                    i++;
                    swapped = false;
                }
            }
            foreach ( int element in arr)
            {
                Console.Write("{0} ",element);
            }
        }
    }

---------------------
contact.if.urgent@gmail.com

Shital Savekar on 03-Jun-2016

Add Answers or Comments

To comment on this, Sign In or Sign Up.