Made a code for insertion sort, declared a sorted and an unsorted array, but the unsorted array is output as a sorted array at the end
NickName:EmergencyEgg Ask DateTime:2022-10-20T02:42:21

Made a code for insertion sort, declared a sorted and an unsorted array, but the unsorted array is output as a sorted array at the end

def InsSort(myList):
    UBound = len(myList)

    for i in range(1,UBound):
        NextItem = myList[i]
        position = i - 1

        while position>=0 and myList[position]>NextItem:
            myList[position+1] = myList[position]
            position = position - 1
        myList[position+1] = NextItem


UnsortList = []
n = int(input("Enter number of elements in the List:\n"))
for j in range(0,n):
    element = int(input("Input element:\n"))
    UnsortList.append(element)
print(UnsortList)

myList = UnsortList

InsSort(myList)

print(str(UnsortList)+" is now sorted --> "+str(myList))

I let the user create their own array "UnsortList" using their input, I then copy it to "myList", and call the funtion InSort(myList) to sort the "myList" array, but when it excecutes print(str(UnsortList)+" is now sorted --> "+str(myList)), str(UnsortList) shows up as the sorted "myList" as well

An example output:

First it outputs the unsorted array via print(UnsortList)

[-9, 3, 6, 1, -3, 55, 22, -18]

Then where it is supposed to print the Unsorted and sorted array via print(str(UnsortList)+" is now sorted --> "+str(myList)), this is what it outputs:

[-18, -9, -3, 1, 3, 6, 22, 55] is now sorted --> [-18, -9, -3, 1, 3, 6, 22, 55]

The output I want is:

[-9, 3, 6, 1, -3, 55, 22, -18] is now sorted --> [-18, -9, -3, 1, 3, 6, 22, 55]

What could be the solution to this?

Thanks to OldBill, I realised myList = UnsortList does not create a copy of the list as I intended, the correct syntax is supposed to be myList = UnsortList.copy()

Copyright Notice:Content Author:「EmergencyEgg」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/74130382/made-a-code-for-insertion-sort-declared-a-sorted-and-an-unsorted-array-but-the

More about “Made a code for insertion sort, declared a sorted and an unsorted array, but the unsorted array is output as a sorted array at the end” related questions

Made a code for insertion sort, declared a sorted and an unsorted array, but the unsorted array is output as a sorted array at the end

def InsSort(myList): UBound = len(myList) for i in range(1,UBound): NextItem = myList[i] position = i - 1 while position>=0 and myList[position]>NextItem: ...

Show Detail

Made a code for insertion sort, declared a sorted and an unsorted array, but the unsorted array is output as a sorted array at the end

def InsSort(myList): UBound = len(myList) for i in range(1,UBound): NextItem = myList[i] position = i - 1 while position>=0 and myList[position]>NextItem: ...

Show Detail

Find all unsorted pairs in partially sorted array

I have to find (or atleast count) all pairs of (not necessarily adjacent) unsorted elements in a partially sorted array. If we assume the sorting to be ascending, the array [1 4 3 2 5] has the foll...

Show Detail

merging a sort array and unsorted array

Merge a sorted array with unsorted array to give a final sorted array. Can it be better done, the not obvious way. final_sorted_array=merge(sort(unsorted_array), sorted_array) I am assuming merge...

Show Detail

sorting a partially sorted array with insertion sort

I hava an array with the first until the N element are sorted and N+1 until elemnt N+M unsorted (the array consist of N+M elements). what is the complexity of sorting this array using insertion sor...

Show Detail

Marge array in a function both sorted and unsorted

I have a function where I pass arrays that are both sorted and unsorted. The function working when both arrays are sorted but when I pass unsorted then it doesn't sort the array and marge it. Example

Show Detail

Unsorted Array and Sorted Array are returned with their elements in the same order

I'm not sure what's going on here, I've got two array variables, one of them has its elements unsorted, while the other one is meant to be a copy in order to maintain both the unsorted and sorted a...

Show Detail

Printing sorted and unsorted list in python

I have a code which generates a list of numbers and then sorts it. The sorting function works but I need to print the unsorted list after sorting. That's why I put the print function to print the

Show Detail

The output of PriorityQueue varies for sorted and unsorted array

I am implementing Priority Queue and I am not able to understand why are there different outputs for sorted and unsorted array. The code and the output is shown below : Sorted Array import java....

Show Detail

sorting positive integers in unsorted array

This was given to me in a recent programming interview. I am given an unsorted array of integers with negative and positive values, and required to sort them but only for the positive values. I was

Show Detail