0

I'm experimenting with some sorting algorithms (haven't seen anything about sorting yet, just trying to solve a question I saw on a google interview) for this I came up with two ways of sorting a randomly generated array, and would like to compare their times.For that I'm aware that on each execution I need both algorithms to sort the same array, the thing is, when I run the code below the algorithm passed to the second sorting function is the algorithm already sorted by the first one, even though I never modify the variable itself. I already tried assigning the parameter variable to a function variable and returning the function variable.

This is the code:

import random
import time

arraySize = random.randint(15, 200)
array = []
for spot in range(0, arraySize):
    array.append(random.randint(0, 400))


def linearSorting(arrayToSort):
    i = 0
    for pivot in range(i, len(arrayToSort)):
        for num in range(pivot + 1, len(arrayToSort)):
            if arrayToSort[num] < arrayToSort[pivot]:
                intermediate = arrayToSort[num]
                arrayToSort[num] = arrayToSort[pivot]
                arrayToSort[pivot] = intermediate
    return arrayToSort


def randomSorting(arrayToSort):
    i = random.randint(0, len(arrayToSort))
    # print(array)
    # print("Pivot: " + str(i))
    for pivot in range(i, len(arrayToSort)):
        for num in range(pivot + 1, len(arrayToSort)):
            if arrayToSort[num] < arrayToSort[pivot]:
                intermediate = arrayToSort[num]
                arrayToSort[num] = arrayToSort[pivot]
                arrayToSort[pivot] = intermediate
    for j in range(0, len(arrayToSort) - 1):
        if arrayToSort[j] > arrayToSort[j + 1]:
            randomSorting(arrayToSort)
    return arrayToSort


start = time.time()
print(array)
print("\n")
print(linearSorting(array))
end = time.time()
print("\n")
print("------------------------------------------------------")
print("Elapsed time linear Sorting: " + str(end-start))
print("------------------------------------------------------")
print('\n')
print("\n********************************************************************************")
# arraySize = random.randint(15, 200)
# array = []
# for spot in range(0, arraySize):
#     array.append(random.randint(0, 400))
print("\n")
print(array)
print("\n")
start = time.time()
print(randomSorting(array))
end = time.time()
print("\n")
print("------------------------------------------------------")
print("Elapsed time random sorting: {0:.6f}s".format(end-start))
print("------------------------------------------------------")

Result I'm getting:

[132, 354, 189, 122, 106, 197, 280, 345, 376, 344, 139, 230, 76, 6, 111, 304, 249, 326, 18, 220, 283, 19, 53, 346, 201, 59, 399, 316, 202, 366, 168, 7, 343, 103, 19, 283, 393, 293, 95, 204, 166, 102, 391, 204, 150, 20, 115, 93, 20, 244, 144, 129, 384, 149, 131, 90, 200, 288, 307, 276, 110, 150, 346, 150, 350, 213, 51, 131, 288, 40, 21, 209]


[6, 7, 18, 19, 19, 20, 20, 21, 40, 51, 53, 59, 76, 90, 93, 95, 102, 103, 106, 110, 111, 115, 122, 129, 131, 131, 132, 139, 144, 149, 150, 150, 150, 166, 168, 189, 197, 200, 201, 202, 204, 204, 209, 213, 220, 230, 244, 249, 276, 280, 283, 283, 288, 288, 293, 304, 307, 316, 326, 343, 344, 345, 346, 346, 350, 354, 366, 376, 384, 391, 393, 399]


------------------------------------------------------
Elapsed time linear Sorting: 0.0003523826599121094
------------------------------------------------------



********************************************************************************


[6, 7, 18, 19, 19, 20, 20, 21, 40, 51, 53, 59, 76, 90, 93, 95, 102, 103, 106, 110, 111, 115, 122, 129, 131, 131, 132, 139, 144, 149, 150, 150, 150, 166, 168, 189, 197, 200, 201, 202, 204, 204, 209, 213, 220, 230, 244, 249, 276, 280, 283, 283, 288, 288, 293, 304, 307, 316, 326, 343, 344, 345, 346, 346, 350, 354, 366, 376, 384, 391, 393, 399]


[6, 7, 18, 19, 19, 20, 20, 21, 40, 51, 53, 59, 76, 90, 93, 95, 102, 103, 106, 110, 111, 115, 122, 129, 131, 131, 132, 139, 144, 149, 150, 150, 150, 166, 168, 189, 197, 200, 201, 202, 204, 204, 209, 213, 220, 230, 244, 249, 276, 280, 283, 283, 288, 288, 293, 304, 307, 316, 326, 343, 344, 345, 346, 346, 350, 354, 366, 376, 384, 391, 393, 399]


------------------------------------------------------
Elapsed time random sorting: 0.000035s
------------------------------------------------------

Result I want:

[132, 354, 189, 122, 106, 197, 280, 345, 376, 344, 139, 230, 76, 6, 111, 304, 249, 326, 18, 220, 283, 19, 53, 346, 201, 59, 399, 316, 202, 366, 168, 7, 343, 103, 19, 283, 393, 293, 95, 204, 166, 102, 391, 204, 150, 20, 115, 93, 20, 244, 144, 129, 384, 149, 131, 90, 200, 288, 307, 276, 110, 150, 346, 150, 350, 213, 51, 131, 288, 40, 21, 209]


[6, 7, 18, 19, 19, 20, 20, 21, 40, 51, 53, 59, 76, 90, 93, 95, 102, 103, 106, 110, 111, 115, 122, 129, 131, 131, 132, 139, 144, 149, 150, 150, 150, 166, 168, 189, 197, 200, 201, 202, 204, 204, 209, 213, 220, 230, 244, 249, 276, 280, 283, 283, 288, 288, 293, 304, 307, 316, 326, 343, 344, 345, 346, 346, 350, 354, 366, 376, 384, 391, 393, 399]


------------------------------------------------------
Elapsed time linear Sorting: x
------------------------------------------------------



********************************************************************************


[132, 354, 189, 122, 106, 197, 280, 345, 376, 344, 139, 230, 76, 6, 111, 304, 249, 326, 18, 220, 283, 19, 53, 346, 201, 59, 399, 316, 202, 366, 168, 7, 343, 103, 19, 283, 393, 293, 95, 204, 166, 102, 391, 204, 150, 20, 115, 93, 20, 244, 144, 129, 384, 149, 131, 90, 200, 288, 307, 276, 110, 150, 346, 150, 350, 213, 51, 131, 288, 40, 21, 209]


[6, 7, 18, 19, 19, 20, 20, 21, 40, 51, 53, 59, 76, 90, 93, 95, 102, 103, 106, 110, 111, 115, 122, 129, 131, 131, 132, 139, 144, 149, 150, 150, 150, 166, 168, 189, 197, 200, 201, 202, 204, 204, 209, 213, 220, 230, 244, 249, 276, 280, 283, 283, 288, 288, 293, 304, 307, 316, 326, 343, 344, 345, 346, 346, 350, 354, 366, 376, 384, 391, 393, 399]


------------------------------------------------------
Elapsed time random sorting: x
------------------------------------------------------
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • make a copy or a deep copy of the object that variable references, then pass either and keep the other. – Skaperen Aug 04 '19 at 06:40

1 Answers1

0

To copy a list in python without having it affect the first list, you should try new_list = my_list[:]