1

I have tried to do it but I can't figure it out. I have this Quick Sort that is searching at index 1 but how to I change the string at that index to ints?

def quick_sort(list):
    if not list:
        return list
    pivot = list[0]
    lesser = quick_sort([x for x in list[1:] if x[1] < pivot[1]], 1)
    greater = quick_sort([x for x in list[1:] if x[1] >= pivot[1]], 1)
    return lesser + [pivot] + greater

Any help out there?

1
  • Is it a list of strings? Or a list of integers? Commented Jan 15, 2014 at 23:37

2 Answers 2

2

Here try this out.

def quick_sort(list):
    if not list: 
        return list 
    pivot = list[0] 
    lesser = quick_sort([x for x in list[1:] if float(x[1]) < float(pivot[1])], 1) 
    greater = quick_sort([x for x in list[1:] if float(x[1]) >= float(pivot[1])], 1) 
    return lesser + [pivot] + greater
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the edit @jonrsharpe, I forgot that hanging return statment!
0

You don't need the index variable:

def quick_sort(list):
    if not list:
        return list
    pivot = list[0]
    lesser = quick_sort([x for x in list[1:] if x < pivot])
    greater = quick_sort([x for x in list[1:] if x >= pivot])
    return lesser + [pivot] + greater

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.