Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

[CHALLENGE] Sorting algorithm interview question

Name: Anonymous 2016-02-27 12:38

Sort a (linked) list of integers, min to max.
1. You're only allowed to compare the first and second elements.

2. You can swap the first and second elements.

3. You can move the first element to the end of the list.

This was an interview question and supposedly isn't on the internet anywhere. Here was my attempt: https://play.golang.org/p/_hsRXFFkNl and the line-by-line output: http://pastebin.com/raw/c2wZSEpR

Name: Anonymous 2016-02-27 15:27

>>7
It's "a" bubble sort in the sense that you are running the bubble sort algorithm on a representation of the data that is not a fixed-length, random-access array, correct.

Anyway, it's not impossible.
Keep track of the maximum and mimimum elements as you keep running through the list.
The final check for sortedness is then basically:
While first < second, chuck first to end.
Now, first > second, so make sure first == maximum and second == minimum. Chuck first to the end one last time.

I wouldn't take the job, anyway, if your interviewer came up with this problem without realising it was bubble sort.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List