## Microsoft Interview Question

Software Engineer Interns**Country:**United States

**Interview Type:**In-Person

We can use simple Bubble sort.

```
public void sort(Node head) {
cur = Head;
cur_next = cur.next;
count = 0;
while(cur != null) {
cur= cur.next;
count++;
}
for(int i = 0;i < count ;i++){
cur = head;
cur_next = cur.next;
for(int j = 0;j < i;j++) {
if(cur.value < cur_next.value){
swap_values(cur,cur_value);
}
}
}
}
```

//bubble sort approach

```
void bubble_sort(Node* head)
{
if(head == NULL || (head)->next == NULL)
{
return;
}
int count = 0;
Node* temp = head;
while(temp != NULL)
{
count++;
temp = temp->next;
}
Node *p = NULL;
Node *q = NULL;
bool flag = false;
for(int i = count-1; i > 0; --i)
{
p = head;
q = head->next;
flag = false;
for(int j = 0; j < i; j++)
{
if(p->data > q->data)
{
flag = true;
swap(p->data,q->data);
}
p = p->next;
q = q->next;
}
if(flag == false) break;
}
}
```

```
Item* mergesort(Item* top) {
if (!top || !top->next)
return top;
Item* middle = top;
for (Item* p = top; p->next && p->next->next; p = p->next->next)
middle = middle->next;
Item* left_last = middle;
middle = middle->next;
left_last->next=NULL;
Item* left = mergesort(top);
Item* right = mergesort(middle);
Item** curr = ⊤
while (left || right) {
if (!right ||
(left && left->data <= right->data)) {
*curr = left;
left = left->next;
} else {
*curr = right;
right = right->next;
}
curr = &(*curr)->next;
}
return top;
}
```

- Kavita June 17, 2014