-1

Trying to create a double pivot with user inputs, through recursion, although the recursion can be left out, how would a person get the inputs/ints into the list and then let it sort according to the quicksort formula shown.

def swap(lst, i, j):
    if min(i,j) >= 0 and max(i,j) < len(lst) and i != j:
        lst[i], lst[j] = lst[j], lst[i]

def dpquicksort(lst, left=0, right=None):
    if right is None:
        right = len(lst) - 1     
    if right - left >= 1:
        p = min(lst[left], lst[right])
        q = max(lst[left], lst[right])
        l = left + 1
        g = right - 1
        k = l        
        while k <= g:
            if lst[k] < p:
                swap(lst, k, l)
                l += 1
            elif lst[k] >= q:
                while lst[g] > q and k < g:
                    g -= 1
                swap(lst, k, g)
                g -= 1
                if lst[k] < p:
                    swap(lst, k, l)
                    l += 1
            k += 1
        l -= 1
        g += 1
        swap(lst, left, l)
        swap(lst, right, g)
        dpquicksort(lst, left, l-1)
        dpquicksort(lst, l+1, g-1)
        dpquicksort(lst, g+1, right)
        return right

def quickSortHelper(alist, first, last):
    if first<last:
        splitpoint= partition(alist, first, last)
        quickSortHelper(alist, first, splitpoint-1)
        quickSortHelper(alist, splitpoint+1, last)

def quicksort(lst):
    dpquicksort(lst, 0, len(lst)-1)
    print(lst)
lst = [54,26,93,17,77,31,44,55,20]    
#lst = int(input("enter integers: ")) 
quicksort(lst)
lst = [54,6,93,17,7,1,44,55,20]
#lst = [2, 4, 6, 8, 10, 12, 14, 16, 18]
quicksort(lst)
user2390182
  • 72,016
  • 6
  • 67
  • 89
GERRR
  • 5
  • 4
  • Your question is "how to let a user enter an array of ints"? – takendarkk Jul 27 '18 at 16:32
  • 1
    Possible duplicate of [Get a list of numbers as input from the user](https://stackoverflow.com/questions/4663306/get-a-list-of-numbers-as-input-from-the-user) – Marcus.Aurelianus Jul 27 '18 at 16:34
  • yes how can the user enter 8 or 10 different integers or even 6, then should I start at the top of the code and let user enter a list of integers or is it right where it is at the bottom of that function – GERRR Jul 27 '18 at 16:35
  • Does it help on you? – Marcus.Aurelianus Jul 27 '18 at 17:05
  • map & int built-in,now only displays the input but does not get it quicksorted anymore, so the quicksort is not activated anymore, I must be doing something incorrect here – GERRR Jul 27 '18 at 17:13
  • Just copy paste the code provided below and have a try again. – Marcus.Aurelianus Jul 27 '18 at 17:17
  • def quicksort(lst): dpquicksort(lst, 0, len(lst)-1) print(lst) lst = list(map(int,input("enter integers: ").split())) quicksort(lst) lst = [54,6,93,17,7,1,44,55,20] quicksort(lst) – GERRR Jul 27 '18 at 17:27
  • I tried to test with list and it quicksorts the lsit but no the inputdef quicksort(lst): dpquicksort(lst, 0, len(lst)-1) print(lst) lst = list(map(int,input("enter integers: ").split())) quicksort(lst) lst = [54,6,93,17,7,1,44,55,20] quicksort(lst) – GERRR Jul 27 '18 at 17:28
  • Type the input, split by space, donot include [ or ], and have a try. – Marcus.Aurelianus Jul 28 '18 at 02:18

1 Answers1

0

You can utilise map and int built-in,

>>> lst = list(map(int,input("enter integers: ").split()))
enter integers: 2 3 8 1 3 4 1 8 9 2
>>> lst
[2, 3, 8, 1, 3, 4, 1, 8, 9, 2]

Or list comprehension,

[int(num) for num in input("enter integers: ").split()]

It is up to you, put it top or bottom are both fine. Your code become,

def swap(lst, i, j):
    if min(i,j) >= 0 and max(i,j) < len(lst) and i != j:
        lst[i], lst[j] = lst[j], lst[i]

def dpquicksort(lst, left=0, right=None):
    if right is None:
        right = len(lst) - 1     
    if right - left >= 1:
        p = min(lst[left], lst[right])
        q = max(lst[left], lst[right])
        l = left + 1
        g = right - 1
        k = l        
        while k <= g:
            if lst[k] < p:
                swap(lst, k, l)
                l += 1
            elif lst[k] >= q:
                while lst[g] > q and k < g:
                    g -= 1
                swap(lst, k, g)
                g -= 1
                if lst[k] < p:
                    swap(lst, k, l)
                    l += 1
            k += 1
        l -= 1
        g += 1
        swap(lst, left, l)
        swap(lst, right, g)
        dpquicksort(lst, left, l-1)
        dpquicksort(lst, l+1, g-1)
        dpquicksort(lst, g+1, right)
        return right

def quickSortHelper(alist, first, last):
    if first<last:
        splitpoint= partition(alist, first, last)
        quickSortHelper(alist, first, splitpoint-1)
        quickSortHelper(alist, splitpoint+1, last)

def quicksort(lst):
    dpquicksort(lst, 0, len(lst)-1)
    print(lst)
lst = list(map(int,input("enter integers: ").split()))
quicksort(lst)
Marcus.Aurelianus
  • 1,520
  • 10
  • 22