Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

adding global variable in colab cause indent error

I’m trying to add a global variable to quicksort algorithm in colab. It works fine without the global variable ‘r’ but when I introduce it, I receive nonsense error of indention. but indention is correct. pls help.

    # Quicksort Sort
r=0
def partition(array, low, high):
  global r
  pivot = array[high]
    i = low - 1
    for j in range(low, high):
        if array[j] <= pivot:
            i = i + 1
            (array[i], array[j]) = (array[j], array[i])
      r=r+1
    (array[i + 1], array[high]) = (array[high], array[i + 1])
  r=r+1
    return i + 1

def quickSort(array, low, high):
    if low < high:
        pi = partition(array, low, high)
        quickSort(array, low, pi - 1)
        quickSort(array, pi + 1, high)


data = [7,1,3,2,4,5,6]
print("Unsorted Array")
print(data)
size = len(data)
quickSort(data, 0, size - 1)
print('Sorted Array in Ascending Order:')
print(data,r)

   ```
the error:
    ``` File "<tokenize>", line 11
    r=r+1
    ^
IndentationError: unindent does not match any outer indentation level
    ```

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Indentation correction.

r=0
def partition(array, low, high):
    global r
    pivot=array[high]
    i=low-1
    for j in range(low, high):
        if array[j] <= pivot:
            i = i + 1
            (array[i], array[j]) = (array[j], array[i])
        r=r+1
    (array[i + 1], array[high]) = (array[high], array[i + 1])
    r=r+1
    return i + 1

def quickSort(array, low, high):
    if low < high:
        pi = partition(array, low, high)
        quickSort(array, low, pi - 1)
        quickSort(array, pi + 1, high)


data = [7,1,3,2,4,5,6]
print("Unsorted Array")
print(data)
size = len(data)
quickSort(data, 0, size - 1)
print('Sorted Array in Ascending Order:')
print(data,r)

Output:

Unsorted Array
[7, 1, 3, 2, 4, 5, 6]
Sorted Array in Ascending Order:
[1, 2, 3, 4, 5, 6, 7] 19
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading