quick sort swaps count

def _partition(input_list, begin, end):
    # For counting the number of swaps
    count = 0
    # Picks the last number in the list as the pivot
    pivot = input_list[end]

    i = begin
    j = begin
    while j < end:
        if input_list[j] < pivot:
            count += 1
            input_list[i], input_list[j] = input_list[j], input_list[i]
            i += 1
        j += 1

    count += 1
    input_list[i], input_list[end] = input_list[end], input_list[i]
    return i, count


def _quickSort(input_list, begin, end):
    count = 0
    if begin < end:
        pivot, count = _partition(input_list, begin, end)
        count += _quickSort(input_list, begin, pivot-1)
        count += _quickSort(input_list, pivot + 1, end)
    return count


def quickSort(input_list, begin=None, end=None):
    if begin == None:
        begin = 0
    if end == None:
        end = len(input_list) - 1
    return _quickSort(input_list, begin, end)


test = [1, 3, 5, 2, 4, 6, 7]
counter = quickSort(test)
print(counter)
print(test)

test2 = [3, 7, 6, 9, 1, 8, 10, 4, 2, 5]
counter1 = quickSort(test2)
print(counter1)
print(test2)

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source