Sort a list of number with sort function and without sort function. Explain it with a dry run of the code

Question

Unsorted_list = [2,3,1,4,5]

Sorted_list = [5,4,3,2,1]

Make sure you comment your code or explain the dry run in the answer section

in progress 0
TheDataMonk 3 years 8 Answers 684 views Grand Master 0

Answers ( 8 )

  1. unsorted_list = [2,3,1,4,5]
    for i in range (0,len(unsorted_list)):
    for j in range (i+1,len(unsorted_list)):
    if unsorted_list[i]<unsorted_list[j]:
    unsorted_list[i], unsorted_list[j]= unsorted_list[j],unsorted_list[i]
    print(unsorted_list)

  2. Using the Sort function:

    unsorted_list=[2,3,1,4,5]
    unsorted_list=[2,3,1,4,5]

    Output:
    [5, 4, 3, 2, 1]

    Without using the sort function:

    unsorted_list=[2,3,1,4,5]
    x=len(unsorted_list)
    sorted_list=[]
    i=1
    while i<=x:
    sorted_list.append(max(unsorted_list))
    unsorted_list.remove(max(unsorted_list))
    i=i+1
    print(sorted_list)

    output:
    [5, 4, 3, 2, 1]

  3. Forgot to explain my code.

    Here goes:

    1. I used the max function to identify the maximum number in the list and appended it to a separate blank list
    2. I removed the maximum number from the original Unsorted list.
    3. I iterated this process using a while loop to the extent of the number of elements in the list (This is arrived at by using the len() function on the list)

  4. Using Sort function-
    #We have used the build-in sort function and arranged the elements in descending order using reverse= True
    Unsorted_list = [2,3,1,4,5]
    Unsorted_list.sort(reverse= True)
    print(Sorted_list)

    Without Sort function-
    Unsorted_list = [2,3,1,4,5]
    Sorted_list = []
    #Running a while loop which compares each element with the max value and appends in the sorted list created above
    while Unsorted_list:
    max = Unsorted_list[0]
    for x in Unsorted_list:
    if x > max:
    max = x
    Sorted_list.append(max)
    Unsorted_list.remove(max)
    print(Sorted_list)

  5. Without function-

    import numpy as np
    Unsorted_list = [5,4,3,2,1]
    sorted_list=[]
    for i in np.arange(len(Unsorted_list)-1,-1,-1):
    sorted_list.append(Unsorted_list[i])
    print(sorted_list)

    With function –

    Unsorted_list = [5,4,3,2,1]
    Unsorted_list.sort()

  6. Unsorted_list= [2,3,1,4,5]

    for i in range(len(Unsorted_list)):
    for j in range(i + 1,len(Unsorted_list)):
    if(Unsorted_list[i] < Unsorted_listt[j]):
    #Swapping the numbers if they are greater:
    temp = Unsorted_list[i]
    Unsorted_list[i] = Unsorted_list[j]
    Unsorted_list[j] = temp

    sorted_list = Unsorted_list
    print(sorted_list)

  7. Using Sort function-
    Unsorted_list = [2,3,1,4,5]
    Unsorted_list.sort(reverse= True)
    print(Sorted_list)

    Without Sort function-
    Unsorted_list = [2,3,1,4,5]
    Sorted_list = []
    while Unsorted_list:
    max = Unsorted_list[0]
    for x in Unsorted_list:
    if x > max:
    max = x
    Sorted_list.append(max)
    Unsorted_list.remove(max)
    print(Sorted_list)

  8. SORTING A LIST WITH SORT() FUCNTION:

    INPUT::

    list=[3,6,2,8,6,5,9]
    list.sort()
    print(list)

    OUTPUT::

    [2, 3, 5, 6, 6, 8, 9]

    SORTING A LIST WITHOUT SORT() FUCNTION:

    INPUT::

    list=[3,6,2,8,6,5,9]

    for i in range(0,len(list)):
    for j in range(i+1,len(list)):
    if list[i]>list[j]:
    list[i],list[j]=list[j],list[i]
    print(list)

    OUTPUT::

    [2, 3, 5, 6, 6, 8, 9]

Leave an answer

Browse
Browse