0

I am trying to code a sort functions program but I'm having a problem where the randomdata variable is being changed where I don't think it should be.

The code below won't run because the merge sort is causing the randomdata to be in a 2d list for no apparent reason...

Any help why python is doing this would be appreciated!

from random import randint
from math import ceil

size = int(input("Sample size: "))
maximum = int(input("Range of numbers: "))

randomdata = [randint(0, maximum) for _ in range(size)]
print("DATA", randomdata)


def bubble(data):
    comparisons = 0
    length = len(data)
    for _ in range(length - 1):
        for i in range(length - 1):
            if data[i] > data[i+1]:
                hold = data[i]
                del data[i]
                data.insert(i+1, hold)
            comparisons += 1
    return comparisons, data

def mergefunction(data1, data2):
    if type(data1) != type([]):
        data1 = [data1]
    if type(data2) != type([]):
        data2 = [data2]
    comparisons = 0
    merged = list()
    while len(data1) > 0 and len(data2) > 0:
        #print("DATA 1", data1)
        #print("DATA 2", data2)
        if data1[0] < data2[0]:
            merged.append(data1[0])
            del data1[0]
        else:
            merged.append(data2[0])
            del data2[0]
        comparisons += 1
    merged += data1
    merged += data2
    #print("COMPS", comparisons)
    #print("SORT", merged)
    return comparisons, merged

def merge(data):
    comparisons = 0
    while len(data) > 1:
        #print("DATA", data)
        for i in range(0, ceil(len(data)/2), 1):
            #print("I", i)
            try:
                compadd, merged = mergefunction(data[i], data[i+1])
                del data[i], data[i]
                data.insert(i, merged)
                comparisons += compadd
            except IndexError:
                pass
    return comparisons, data[0]

def insrt(data):
    sorteddata = [-10000000]
    comparisons = 0
    for item in data:
        print("ITEM", item)
        for i in range(len(sorteddata)):
            #print(item, sorteddata[i])
            if item > sorteddata[i]:
                sorteddata.insert(i+1, item)
                comparisons += 1
                break
            comparisons += 1
    del sorteddata[0]
    print(sorteddata)
    return comparisons, sorteddata

print("="*10,"BUBBLE","="*10)
print("RANDOM", randomdata)
comps, data = bubble(randomdata)
print("COMPARISONS", comps)
print("SORTED DATA", data)

print("="*10,"MERGE","="*10)
print("RANDOM", randomdata)
comps, data = merge(randomdata)
print("COMPARISONS", comps)
print("SORTED DATA", data)

print("="*10,"INSERT","="*10)
print("RANDOM", randomdata)
comps, data = insrt(randomdata)
print("COMPARISONS", comps)
print("SORTED DATA", data)

Python 3.6.1 I apologize that this code is littered with debuggers...

Matthew Miles
  • 727
  • 12
  • 26
  • 1
    Your code runs OK on my machine. It may help if you can provide an example that doesn't depend on random data. Then you can be sure that the output you're getting is the same output we'll get. – Kevin May 24 '19 at 13:05

1 Answers1

2

When you do comps, data = bubble(randomdata) you're passing a reference of randomdata to bubble(), as randomdata is a list, which means is mutable, then all the changes you do to data inside bubble() reflect in randomdata. You need to use a copy of randomdata instead. Something like: data = data[:] inside bublle()

lpozo
  • 598
  • 2
  • 9