Sorts (merge, quick, bubble) in PythonMerge Sorting in PythonImplementation of merge sort and bubble sortComparing pivot choosing methods in quicksortMerge sort in PythonRust sorting algorithms (selection, bubble, quick, shell, merge)Merge Sort Algorithm PythonQuickMergeSort — The power of internal bufferingJava class for sorting arrays based upon QuickSort algorithmSelection and Bubble sortsPython merge sort

Identifying "long and narrow" polygons in with Postgis

Alignment of six matrices

Telemetry for feature health

Possible Eco thriller, man invents a device to remove rain from glass

Confusion over Hunter with Crossbow Expert and Giant Killer

Difference between shutdown options

Why is participating in the European Parliamentary elections used as a threat?

Adding up numbers in Portuguese is strange

Grepping string, but include all non-blank lines following each grep match

What does "tick" mean in this sentence?

How to test the sharpness of a knife?

The Digit Triangles

How to preserve electronics (computers, iPads and phones) for hundreds of years

Limit max CPU usage SQL SERVER with WSRM

How to make money from a browser who sees 5 seconds into the future of any web page?

"Oh no!" in Latin

When and why was runway 07/25 at Kai Tak removed?

Storage of electrolytic capacitors - how long?

What is the meaning of "You've never met a graph you didn't like?"

Can I cause damage to electrical appliances by unplugging them when they are turned on?

Echo with obfuscation

Is there anyway, I can have two passwords for my wi-fi

Can I say "fingers" when referring to toes?

How can I, as DM, avoid the Conga Line of Death occurring when implementing some form of flanking rule?



Sorts (merge, quick, bubble) in Python


Merge Sorting in PythonImplementation of merge sort and bubble sortComparing pivot choosing methods in quicksortMerge sort in PythonRust sorting algorithms (selection, bubble, quick, shell, merge)Merge Sort Algorithm PythonQuickMergeSort — The power of internal bufferingJava class for sorting arrays based upon QuickSort algorithmSelection and Bubble sortsPython merge sort













2












$begingroup$


I'm new to python, and I wrote the following code. Please review, critique and enhance.




This project compares quicksort, mergesort and bubblesort



  1. write/test the partition routine for quicksort. (20 points) The rest of the code is given.


  2. write/test the merging part of the mergesort. (20 points) The rest of the code is given.


  3. write/test bubblesort. (10 points) No code is given.


  4. compare the sorts using lists of 10 and 50 items (in-order, reverse order and random (hard coded).


Be sure to print out the initial and sorted arrays so I can see that your module sorted properly.




#1) write/test the partition routine for quicksort. 
def qS(items):
quickSortHelper(items,0,len(items)-1)

def quickSortHelper(items,first,last):
if first<last:

splitpoint = partition(items,first,last)

quickSortHelper(items,first,splitpoint-1)
quickSortHelper(items,splitpoint+1,last)


def partition(items,first,last):
pivotvalue = items[first]

leftside = first+1
rightside = last

done = False
while not done:

while leftside <= rightside and items[leftside] <= pivotvalue:
leftside = leftside + 1

while items[rightside] >= pivotvalue and rightside >= leftside:
rightside = rightside -1

if rightside < leftside:
done = True
else:
temp = items[leftside]
items[leftside] = items[rightside]
items[rightside] = temp

temp = items[first]
items[first] = items[rightside]
items[rightside] = temp
return rightside

# 2) write/test the merging part of the mergesort.
def mS(items):
#print("Splitting ",items)
if len(items)>1:
mid = len(items)//2
lefthalf = items[:mid]
righthalf = items[mid:]

mS(lefthalf)
mS(righthalf)

i=0
j=0
k=0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
items[k]=lefthalf[i]
i=i+1
else:
items[k]=righthalf[j]
j=j+1
k=k+1

while i < len(lefthalf):
items[k]=lefthalf[i]
i=i+1
k=k+1

while j < len(righthalf):
items[k]=righthalf[j]
j=j+1
k=k+1

# 3) write/test bubblesort. (10 points) No code is given.
def bS(items):
exchanges = True
passnum = len(items)-1
while passnum > 0 and exchanges:
exchanges = False
for i in range(passnum):
if items[i]>items[i+1]:
exchanges = True
temp = items[i]
items[i] = items[i+1]
items[i+1] = temp
passnum = passnum-1


# 4) compare the sorts using lists of 10 and 50 items (in-order, reverse #order
# and random (hard coded). Be sure
# to print out the initial and sorted arrays so I can see that #your module
# sorted properly. (10 points)

def mergeSort(L, ascending = True):
result = []
if len(L) == 1:
return L
mid = len(L) // 2

firsthalf = mergeSort(L[:mid])

secondhalf = mergeSort(L[mid:])

x, y = 0, 0
while x < len(firsthalf) and y < len(secondhalf):
if firsthalf[x] > secondhalf[y]: # < for descending
result.append(secondhalf[y])
y = y + 1

else:
result.append(firsthalf[x])
x = x + 1
result = result + firsthalf[x:]

result = result + secondhalf[y:]
if ascending == True :
return result
else:
result.reverse()
return result

def _quickSort(list):
if len(list) <= 1:
return list
smaller, equal, larger = [], [], []
pivot = random.choice(list)

for x in list:
if x < pivot: smaller.append(x)
elif x == pivot: equal.append(x)
else: larger.append(x)


return _quickSort(smaller) + equal + _quickSort(larger)

def quickSort(list, ascending=True):
if ascending:
return _quickSort(list)
else:
return _quickSort(list)[::-1]

def BubbleSortAsc(list):
swapped = True
sortedvalue=0
while swapped:
swapped = False
sortedvalue+=1
for i in range(0,len(list)-sortedvalue):
if list[i]>list[i+1]:
list[i], list[i+1], swapped = list[i+1], list[i], True

def BubbleSortDsc(list):
swapped = True
sortedvalue=0
while swapped:
swapped = False
sortedvalue+=1
for i in range (0,len(list)-sortedvalue):
if list[i]<list[i+1]:
list[i], list[i+1], swapped = list[i+1], list[i], True

list=[3,2,4,1,5,9,7,6]


# 6) write/test a variation on quicksort (vqS) that makes the following
# improvements:
# chooses pivot by taking a small sample size (3 items) and using
# median for pivot. (10 points)

def quicksort(array, l=0, r=-1):

if r == -1:
r = len(array)

# base case
if r-l <= 1:
return

# pick the median of 3 possible pivots
mid = int((l+r)*0.5)
pivot = 0
#pivots = [ l, mid, r-1]
if array[l] > array[mid]:
if array[r-1]> array[l]:
pivot = l
elif array[mid] > array[r-1]:
pivot = mid
else:
if array[r-1] > array[mid]:
pivot = mid
else:
pivot = r-1

i = l+1
array[l], array[pivot] = array[pivot], array[l]

for j in range(l+1,r):
if array[j] < array[l]:
array[i], array[j] = array[j], array[i]
i = i+1

array[l], array[i-1] = array[i-1], array[l]

quicksort(array, l, i-1)
quicksort(array, i, r)

return array
ls =[random.randrange(50) for _ in range(10)]




#7) write/test a variation on mergesort (vmS) that makes the following
# improvement:
# Use insertion sort for small arrays (10 items or less).


RUN = 10
def insertionSort(arr, left, right):

for i in range(left + 1, right+1):

temp = arr[i]
j = i - 1
while arr[j] > temp and j >= left:

arr[j+1] = arr[j]
j -= 1

arr[j+1] = temp
def hybridSort(arr, n):

# Sort individual subarrays of size RUN
for i in range(0, n, RUN):
insertionSort(arr, i, min((i+31), (n-1)))

# start merging from size RUN (or 10). It will merge

size = RUN
while size < n:
for left in range(0, n, 2*size):
mid = left + size - 1
right = min((left + 2*size - 1), (n-1))
merge(arr, left, mid, right)
size = 2*size

# utility function to print the Array
def printArray(arr, n):

for i in range(0, n):
print(arr[i], end = " ")
print()
# Driver program to test above function
if __name__ == "__main__":

arr = [5, 21, 7, 23, 19,25,2]
n = len(arr)



items = [55,27,90,18,78,30,44,56,21,67]









share|improve this question









New contributor




user195683 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$
















    2












    $begingroup$


    I'm new to python, and I wrote the following code. Please review, critique and enhance.




    This project compares quicksort, mergesort and bubblesort



    1. write/test the partition routine for quicksort. (20 points) The rest of the code is given.


    2. write/test the merging part of the mergesort. (20 points) The rest of the code is given.


    3. write/test bubblesort. (10 points) No code is given.


    4. compare the sorts using lists of 10 and 50 items (in-order, reverse order and random (hard coded).


    Be sure to print out the initial and sorted arrays so I can see that your module sorted properly.




    #1) write/test the partition routine for quicksort. 
    def qS(items):
    quickSortHelper(items,0,len(items)-1)

    def quickSortHelper(items,first,last):
    if first<last:

    splitpoint = partition(items,first,last)

    quickSortHelper(items,first,splitpoint-1)
    quickSortHelper(items,splitpoint+1,last)


    def partition(items,first,last):
    pivotvalue = items[first]

    leftside = first+1
    rightside = last

    done = False
    while not done:

    while leftside <= rightside and items[leftside] <= pivotvalue:
    leftside = leftside + 1

    while items[rightside] >= pivotvalue and rightside >= leftside:
    rightside = rightside -1

    if rightside < leftside:
    done = True
    else:
    temp = items[leftside]
    items[leftside] = items[rightside]
    items[rightside] = temp

    temp = items[first]
    items[first] = items[rightside]
    items[rightside] = temp
    return rightside

    # 2) write/test the merging part of the mergesort.
    def mS(items):
    #print("Splitting ",items)
    if len(items)>1:
    mid = len(items)//2
    lefthalf = items[:mid]
    righthalf = items[mid:]

    mS(lefthalf)
    mS(righthalf)

    i=0
    j=0
    k=0
    while i < len(lefthalf) and j < len(righthalf):
    if lefthalf[i] < righthalf[j]:
    items[k]=lefthalf[i]
    i=i+1
    else:
    items[k]=righthalf[j]
    j=j+1
    k=k+1

    while i < len(lefthalf):
    items[k]=lefthalf[i]
    i=i+1
    k=k+1

    while j < len(righthalf):
    items[k]=righthalf[j]
    j=j+1
    k=k+1

    # 3) write/test bubblesort. (10 points) No code is given.
    def bS(items):
    exchanges = True
    passnum = len(items)-1
    while passnum > 0 and exchanges:
    exchanges = False
    for i in range(passnum):
    if items[i]>items[i+1]:
    exchanges = True
    temp = items[i]
    items[i] = items[i+1]
    items[i+1] = temp
    passnum = passnum-1


    # 4) compare the sorts using lists of 10 and 50 items (in-order, reverse #order
    # and random (hard coded). Be sure
    # to print out the initial and sorted arrays so I can see that #your module
    # sorted properly. (10 points)

    def mergeSort(L, ascending = True):
    result = []
    if len(L) == 1:
    return L
    mid = len(L) // 2

    firsthalf = mergeSort(L[:mid])

    secondhalf = mergeSort(L[mid:])

    x, y = 0, 0
    while x < len(firsthalf) and y < len(secondhalf):
    if firsthalf[x] > secondhalf[y]: # < for descending
    result.append(secondhalf[y])
    y = y + 1

    else:
    result.append(firsthalf[x])
    x = x + 1
    result = result + firsthalf[x:]

    result = result + secondhalf[y:]
    if ascending == True :
    return result
    else:
    result.reverse()
    return result

    def _quickSort(list):
    if len(list) <= 1:
    return list
    smaller, equal, larger = [], [], []
    pivot = random.choice(list)

    for x in list:
    if x < pivot: smaller.append(x)
    elif x == pivot: equal.append(x)
    else: larger.append(x)


    return _quickSort(smaller) + equal + _quickSort(larger)

    def quickSort(list, ascending=True):
    if ascending:
    return _quickSort(list)
    else:
    return _quickSort(list)[::-1]

    def BubbleSortAsc(list):
    swapped = True
    sortedvalue=0
    while swapped:
    swapped = False
    sortedvalue+=1
    for i in range(0,len(list)-sortedvalue):
    if list[i]>list[i+1]:
    list[i], list[i+1], swapped = list[i+1], list[i], True

    def BubbleSortDsc(list):
    swapped = True
    sortedvalue=0
    while swapped:
    swapped = False
    sortedvalue+=1
    for i in range (0,len(list)-sortedvalue):
    if list[i]<list[i+1]:
    list[i], list[i+1], swapped = list[i+1], list[i], True

    list=[3,2,4,1,5,9,7,6]


    # 6) write/test a variation on quicksort (vqS) that makes the following
    # improvements:
    # chooses pivot by taking a small sample size (3 items) and using
    # median for pivot. (10 points)

    def quicksort(array, l=0, r=-1):

    if r == -1:
    r = len(array)

    # base case
    if r-l <= 1:
    return

    # pick the median of 3 possible pivots
    mid = int((l+r)*0.5)
    pivot = 0
    #pivots = [ l, mid, r-1]
    if array[l] > array[mid]:
    if array[r-1]> array[l]:
    pivot = l
    elif array[mid] > array[r-1]:
    pivot = mid
    else:
    if array[r-1] > array[mid]:
    pivot = mid
    else:
    pivot = r-1

    i = l+1
    array[l], array[pivot] = array[pivot], array[l]

    for j in range(l+1,r):
    if array[j] < array[l]:
    array[i], array[j] = array[j], array[i]
    i = i+1

    array[l], array[i-1] = array[i-1], array[l]

    quicksort(array, l, i-1)
    quicksort(array, i, r)

    return array
    ls =[random.randrange(50) for _ in range(10)]




    #7) write/test a variation on mergesort (vmS) that makes the following
    # improvement:
    # Use insertion sort for small arrays (10 items or less).


    RUN = 10
    def insertionSort(arr, left, right):

    for i in range(left + 1, right+1):

    temp = arr[i]
    j = i - 1
    while arr[j] > temp and j >= left:

    arr[j+1] = arr[j]
    j -= 1

    arr[j+1] = temp
    def hybridSort(arr, n):

    # Sort individual subarrays of size RUN
    for i in range(0, n, RUN):
    insertionSort(arr, i, min((i+31), (n-1)))

    # start merging from size RUN (or 10). It will merge

    size = RUN
    while size < n:
    for left in range(0, n, 2*size):
    mid = left + size - 1
    right = min((left + 2*size - 1), (n-1))
    merge(arr, left, mid, right)
    size = 2*size

    # utility function to print the Array
    def printArray(arr, n):

    for i in range(0, n):
    print(arr[i], end = " ")
    print()
    # Driver program to test above function
    if __name__ == "__main__":

    arr = [5, 21, 7, 23, 19,25,2]
    n = len(arr)



    items = [55,27,90,18,78,30,44,56,21,67]









    share|improve this question









    New contributor




    user195683 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$














      2












      2








      2


      0



      $begingroup$


      I'm new to python, and I wrote the following code. Please review, critique and enhance.




      This project compares quicksort, mergesort and bubblesort



      1. write/test the partition routine for quicksort. (20 points) The rest of the code is given.


      2. write/test the merging part of the mergesort. (20 points) The rest of the code is given.


      3. write/test bubblesort. (10 points) No code is given.


      4. compare the sorts using lists of 10 and 50 items (in-order, reverse order and random (hard coded).


      Be sure to print out the initial and sorted arrays so I can see that your module sorted properly.




      #1) write/test the partition routine for quicksort. 
      def qS(items):
      quickSortHelper(items,0,len(items)-1)

      def quickSortHelper(items,first,last):
      if first<last:

      splitpoint = partition(items,first,last)

      quickSortHelper(items,first,splitpoint-1)
      quickSortHelper(items,splitpoint+1,last)


      def partition(items,first,last):
      pivotvalue = items[first]

      leftside = first+1
      rightside = last

      done = False
      while not done:

      while leftside <= rightside and items[leftside] <= pivotvalue:
      leftside = leftside + 1

      while items[rightside] >= pivotvalue and rightside >= leftside:
      rightside = rightside -1

      if rightside < leftside:
      done = True
      else:
      temp = items[leftside]
      items[leftside] = items[rightside]
      items[rightside] = temp

      temp = items[first]
      items[first] = items[rightside]
      items[rightside] = temp
      return rightside

      # 2) write/test the merging part of the mergesort.
      def mS(items):
      #print("Splitting ",items)
      if len(items)>1:
      mid = len(items)//2
      lefthalf = items[:mid]
      righthalf = items[mid:]

      mS(lefthalf)
      mS(righthalf)

      i=0
      j=0
      k=0
      while i < len(lefthalf) and j < len(righthalf):
      if lefthalf[i] < righthalf[j]:
      items[k]=lefthalf[i]
      i=i+1
      else:
      items[k]=righthalf[j]
      j=j+1
      k=k+1

      while i < len(lefthalf):
      items[k]=lefthalf[i]
      i=i+1
      k=k+1

      while j < len(righthalf):
      items[k]=righthalf[j]
      j=j+1
      k=k+1

      # 3) write/test bubblesort. (10 points) No code is given.
      def bS(items):
      exchanges = True
      passnum = len(items)-1
      while passnum > 0 and exchanges:
      exchanges = False
      for i in range(passnum):
      if items[i]>items[i+1]:
      exchanges = True
      temp = items[i]
      items[i] = items[i+1]
      items[i+1] = temp
      passnum = passnum-1


      # 4) compare the sorts using lists of 10 and 50 items (in-order, reverse #order
      # and random (hard coded). Be sure
      # to print out the initial and sorted arrays so I can see that #your module
      # sorted properly. (10 points)

      def mergeSort(L, ascending = True):
      result = []
      if len(L) == 1:
      return L
      mid = len(L) // 2

      firsthalf = mergeSort(L[:mid])

      secondhalf = mergeSort(L[mid:])

      x, y = 0, 0
      while x < len(firsthalf) and y < len(secondhalf):
      if firsthalf[x] > secondhalf[y]: # < for descending
      result.append(secondhalf[y])
      y = y + 1

      else:
      result.append(firsthalf[x])
      x = x + 1
      result = result + firsthalf[x:]

      result = result + secondhalf[y:]
      if ascending == True :
      return result
      else:
      result.reverse()
      return result

      def _quickSort(list):
      if len(list) <= 1:
      return list
      smaller, equal, larger = [], [], []
      pivot = random.choice(list)

      for x in list:
      if x < pivot: smaller.append(x)
      elif x == pivot: equal.append(x)
      else: larger.append(x)


      return _quickSort(smaller) + equal + _quickSort(larger)

      def quickSort(list, ascending=True):
      if ascending:
      return _quickSort(list)
      else:
      return _quickSort(list)[::-1]

      def BubbleSortAsc(list):
      swapped = True
      sortedvalue=0
      while swapped:
      swapped = False
      sortedvalue+=1
      for i in range(0,len(list)-sortedvalue):
      if list[i]>list[i+1]:
      list[i], list[i+1], swapped = list[i+1], list[i], True

      def BubbleSortDsc(list):
      swapped = True
      sortedvalue=0
      while swapped:
      swapped = False
      sortedvalue+=1
      for i in range (0,len(list)-sortedvalue):
      if list[i]<list[i+1]:
      list[i], list[i+1], swapped = list[i+1], list[i], True

      list=[3,2,4,1,5,9,7,6]


      # 6) write/test a variation on quicksort (vqS) that makes the following
      # improvements:
      # chooses pivot by taking a small sample size (3 items) and using
      # median for pivot. (10 points)

      def quicksort(array, l=0, r=-1):

      if r == -1:
      r = len(array)

      # base case
      if r-l <= 1:
      return

      # pick the median of 3 possible pivots
      mid = int((l+r)*0.5)
      pivot = 0
      #pivots = [ l, mid, r-1]
      if array[l] > array[mid]:
      if array[r-1]> array[l]:
      pivot = l
      elif array[mid] > array[r-1]:
      pivot = mid
      else:
      if array[r-1] > array[mid]:
      pivot = mid
      else:
      pivot = r-1

      i = l+1
      array[l], array[pivot] = array[pivot], array[l]

      for j in range(l+1,r):
      if array[j] < array[l]:
      array[i], array[j] = array[j], array[i]
      i = i+1

      array[l], array[i-1] = array[i-1], array[l]

      quicksort(array, l, i-1)
      quicksort(array, i, r)

      return array
      ls =[random.randrange(50) for _ in range(10)]




      #7) write/test a variation on mergesort (vmS) that makes the following
      # improvement:
      # Use insertion sort for small arrays (10 items or less).


      RUN = 10
      def insertionSort(arr, left, right):

      for i in range(left + 1, right+1):

      temp = arr[i]
      j = i - 1
      while arr[j] > temp and j >= left:

      arr[j+1] = arr[j]
      j -= 1

      arr[j+1] = temp
      def hybridSort(arr, n):

      # Sort individual subarrays of size RUN
      for i in range(0, n, RUN):
      insertionSort(arr, i, min((i+31), (n-1)))

      # start merging from size RUN (or 10). It will merge

      size = RUN
      while size < n:
      for left in range(0, n, 2*size):
      mid = left + size - 1
      right = min((left + 2*size - 1), (n-1))
      merge(arr, left, mid, right)
      size = 2*size

      # utility function to print the Array
      def printArray(arr, n):

      for i in range(0, n):
      print(arr[i], end = " ")
      print()
      # Driver program to test above function
      if __name__ == "__main__":

      arr = [5, 21, 7, 23, 19,25,2]
      n = len(arr)



      items = [55,27,90,18,78,30,44,56,21,67]









      share|improve this question









      New contributor




      user195683 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      I'm new to python, and I wrote the following code. Please review, critique and enhance.




      This project compares quicksort, mergesort and bubblesort



      1. write/test the partition routine for quicksort. (20 points) The rest of the code is given.


      2. write/test the merging part of the mergesort. (20 points) The rest of the code is given.


      3. write/test bubblesort. (10 points) No code is given.


      4. compare the sorts using lists of 10 and 50 items (in-order, reverse order and random (hard coded).


      Be sure to print out the initial and sorted arrays so I can see that your module sorted properly.




      #1) write/test the partition routine for quicksort. 
      def qS(items):
      quickSortHelper(items,0,len(items)-1)

      def quickSortHelper(items,first,last):
      if first<last:

      splitpoint = partition(items,first,last)

      quickSortHelper(items,first,splitpoint-1)
      quickSortHelper(items,splitpoint+1,last)


      def partition(items,first,last):
      pivotvalue = items[first]

      leftside = first+1
      rightside = last

      done = False
      while not done:

      while leftside <= rightside and items[leftside] <= pivotvalue:
      leftside = leftside + 1

      while items[rightside] >= pivotvalue and rightside >= leftside:
      rightside = rightside -1

      if rightside < leftside:
      done = True
      else:
      temp = items[leftside]
      items[leftside] = items[rightside]
      items[rightside] = temp

      temp = items[first]
      items[first] = items[rightside]
      items[rightside] = temp
      return rightside

      # 2) write/test the merging part of the mergesort.
      def mS(items):
      #print("Splitting ",items)
      if len(items)>1:
      mid = len(items)//2
      lefthalf = items[:mid]
      righthalf = items[mid:]

      mS(lefthalf)
      mS(righthalf)

      i=0
      j=0
      k=0
      while i < len(lefthalf) and j < len(righthalf):
      if lefthalf[i] < righthalf[j]:
      items[k]=lefthalf[i]
      i=i+1
      else:
      items[k]=righthalf[j]
      j=j+1
      k=k+1

      while i < len(lefthalf):
      items[k]=lefthalf[i]
      i=i+1
      k=k+1

      while j < len(righthalf):
      items[k]=righthalf[j]
      j=j+1
      k=k+1

      # 3) write/test bubblesort. (10 points) No code is given.
      def bS(items):
      exchanges = True
      passnum = len(items)-1
      while passnum > 0 and exchanges:
      exchanges = False
      for i in range(passnum):
      if items[i]>items[i+1]:
      exchanges = True
      temp = items[i]
      items[i] = items[i+1]
      items[i+1] = temp
      passnum = passnum-1


      # 4) compare the sorts using lists of 10 and 50 items (in-order, reverse #order
      # and random (hard coded). Be sure
      # to print out the initial and sorted arrays so I can see that #your module
      # sorted properly. (10 points)

      def mergeSort(L, ascending = True):
      result = []
      if len(L) == 1:
      return L
      mid = len(L) // 2

      firsthalf = mergeSort(L[:mid])

      secondhalf = mergeSort(L[mid:])

      x, y = 0, 0
      while x < len(firsthalf) and y < len(secondhalf):
      if firsthalf[x] > secondhalf[y]: # < for descending
      result.append(secondhalf[y])
      y = y + 1

      else:
      result.append(firsthalf[x])
      x = x + 1
      result = result + firsthalf[x:]

      result = result + secondhalf[y:]
      if ascending == True :
      return result
      else:
      result.reverse()
      return result

      def _quickSort(list):
      if len(list) <= 1:
      return list
      smaller, equal, larger = [], [], []
      pivot = random.choice(list)

      for x in list:
      if x < pivot: smaller.append(x)
      elif x == pivot: equal.append(x)
      else: larger.append(x)


      return _quickSort(smaller) + equal + _quickSort(larger)

      def quickSort(list, ascending=True):
      if ascending:
      return _quickSort(list)
      else:
      return _quickSort(list)[::-1]

      def BubbleSortAsc(list):
      swapped = True
      sortedvalue=0
      while swapped:
      swapped = False
      sortedvalue+=1
      for i in range(0,len(list)-sortedvalue):
      if list[i]>list[i+1]:
      list[i], list[i+1], swapped = list[i+1], list[i], True

      def BubbleSortDsc(list):
      swapped = True
      sortedvalue=0
      while swapped:
      swapped = False
      sortedvalue+=1
      for i in range (0,len(list)-sortedvalue):
      if list[i]<list[i+1]:
      list[i], list[i+1], swapped = list[i+1], list[i], True

      list=[3,2,4,1,5,9,7,6]


      # 6) write/test a variation on quicksort (vqS) that makes the following
      # improvements:
      # chooses pivot by taking a small sample size (3 items) and using
      # median for pivot. (10 points)

      def quicksort(array, l=0, r=-1):

      if r == -1:
      r = len(array)

      # base case
      if r-l <= 1:
      return

      # pick the median of 3 possible pivots
      mid = int((l+r)*0.5)
      pivot = 0
      #pivots = [ l, mid, r-1]
      if array[l] > array[mid]:
      if array[r-1]> array[l]:
      pivot = l
      elif array[mid] > array[r-1]:
      pivot = mid
      else:
      if array[r-1] > array[mid]:
      pivot = mid
      else:
      pivot = r-1

      i = l+1
      array[l], array[pivot] = array[pivot], array[l]

      for j in range(l+1,r):
      if array[j] < array[l]:
      array[i], array[j] = array[j], array[i]
      i = i+1

      array[l], array[i-1] = array[i-1], array[l]

      quicksort(array, l, i-1)
      quicksort(array, i, r)

      return array
      ls =[random.randrange(50) for _ in range(10)]




      #7) write/test a variation on mergesort (vmS) that makes the following
      # improvement:
      # Use insertion sort for small arrays (10 items or less).


      RUN = 10
      def insertionSort(arr, left, right):

      for i in range(left + 1, right+1):

      temp = arr[i]
      j = i - 1
      while arr[j] > temp and j >= left:

      arr[j+1] = arr[j]
      j -= 1

      arr[j+1] = temp
      def hybridSort(arr, n):

      # Sort individual subarrays of size RUN
      for i in range(0, n, RUN):
      insertionSort(arr, i, min((i+31), (n-1)))

      # start merging from size RUN (or 10). It will merge

      size = RUN
      while size < n:
      for left in range(0, n, 2*size):
      mid = left + size - 1
      right = min((left + 2*size - 1), (n-1))
      merge(arr, left, mid, right)
      size = 2*size

      # utility function to print the Array
      def printArray(arr, n):

      for i in range(0, n):
      print(arr[i], end = " ")
      print()
      # Driver program to test above function
      if __name__ == "__main__":

      arr = [5, 21, 7, 23, 19,25,2]
      n = len(arr)



      items = [55,27,90,18,78,30,44,56,21,67]






      python beginner sorting mergesort quick-sort






      share|improve this question









      New contributor




      user195683 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      user195683 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 11 mins ago









      200_success

      130k17154419




      130k17154419






      New contributor




      user195683 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 5 hours ago









      user195683user195683

      111




      111




      New contributor




      user195683 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      user195683 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      user195683 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );






          user195683 is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215852%2fsorts-merge-quick-bubble-in-python%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          user195683 is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          user195683 is a new contributor. Be nice, and check out our Code of Conduct.












          user195683 is a new contributor. Be nice, and check out our Code of Conduct.











          user195683 is a new contributor. Be nice, and check out our Code of Conduct.














          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215852%2fsorts-merge-quick-bubble-in-python%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

          格濟夫卡 參考資料 导航菜单51°3′40″N 34°2′21″E / 51.06111°N 34.03917°E / 51.06111; 34.03917ГезівкаПогода в селі 编辑或修订