# Quick Sort Array python

in this program im trying to get the total number of swaps and the amount of time, it takes to sort the random array. The time part seems to be working correctly but the swap count is not. it just currently prints 4,3,3,2,2,1 and so on. Please help me to figure out why the swap counter is not working correctly.

``````import random
import time

def partition(nums, low, high):
swap = 0
pivot = nums[(low + high) // 2]
i = low - 1
j = high + 1
while True:
i += 1
while nums[i] < pivot:
i += 1
swap+=1

j -= 1
while nums[j] > pivot:
j -= 1
swap+=1

if i >= j:
return j

nums[i], nums[j] = nums[j], nums[i]
print('swaps',swap)

def quick_sort(nums):

def _quick_sort(items, low, high):
if low < high:

split_index = partition(items, low, high)
_quick_sort(items, low, split_index)
_quick_sort(items, split_index + 1, high)

_quick_sort(nums, 0, len(nums) - 1)

A = [random.randint(0, 1000) for x in range(8)]
print("Unsorted array", A)
start = time.time()
quick_sort(A)
end = time.time()
print(f"Runtime of the program is {end - start}")
print("Sorted array", A)
``````